日期:2014-05-20  浏览次数:20970 次

Myeclipse+Jboss+mysql JNDI调用出错
package com.jms.jmsService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Properties;

import org.jnp.interfaces.TimedSocketFactory;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnJms {

/**
* 连接store空间(发送方)
* @param args
*/
public static void main(String[] args) {
connJboss();//1
jbossClient();//2
}

//JBOSS JNDI连接方式

public static Context getJbossContext(){

final String INIT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
final String SERVER_URL = "jnp://127.0.0.1:8080";
Context ctx = null;
try{

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INIT_FACTORY);
env.put(Context.PROVIDER_URL,SERVER_URL);
ctx = new InitialContext(env);

}catch(Exception e){

e.printStackTrace();
}
return ctx;

}



public static void jbossClient(){

final String INIT_FACTORY="org.jnp.interfaces.NamingContextFactory";
final String SERVER_URL="jnp://127.0.0.1:8080";
Hashtable map = new Hashtable();
map.put(Context.INITIAL_CONTEXT_FACTORY,INIT_FACTORY);
map.put(Context.PROVIDER_URL, SERVER_URL);

Properties properties = new Properties();  
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");  
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");  
properties.setProperty(Context.PROVIDER_URL,"localhost:1099");  
try {
Context ctx = new InitialContext(properties);
DataSource ds = (DataSource)ctx.lookup("java:/DefaultDS");
java.sql.Connection con = ds.getConnection();
Statement stat = con.createStatement();
ResultSet qu = stat.executeQuery("select * from mysql.user");
while(qu.next()){

System.out.println("------:"+qu.getString("User"));
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


1:调用jbossClient()方法时报错:log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.
javax.naming.NameNotFoundException: DefaultDS not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)