我正在尝试使用Websphere Application Server中的JNDI查找JMS TopicConnectionFactory。我已在服务器中正确完成了jndi设置。我已在服务器中部署了我的应用程序。
我为Sender / Reciever / Listener设计了3个java类。然后在服务器中部署WAR之后,我首先尝试将Receiver Code作为独立的Java应用程序运行。但它失败了,错误如下:
线程“main”中的异常javax.naming.NoInitialContextException:无法使用javax.naming.spi.NamingManager.getInitialContext(NamingManag er.java:242)中哈希表中指定的工厂创建InitialContext
在下面附上我的代码段:
/**
* SENDER CLASS
*/
package com.jms.topic;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author db2admin
*
*/
public class JMSTopicSender {
/**
* @param args
* @throws JMSException
* @throws NamingException
*/
public static void main(String[] args) throws JMSException, NamingException {
// TODO Auto-generated method stub
InitialContext ctx=new InitialContext();
TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup("myTopicConnectionFactory");
TopicConnection con=f.createTopicConnection();
con.start();
TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic t=(Topic)ctx.lookup("myTopic");
TopicPublisher publisher=ses.createPublisher(t);
TextMessage msg=ses.createTextMessage();
msg.setText("Hello World");
publisher.publish(msg);
System.out.println("Message successfully sent.");
}
}
/**
* RECEIVER CLASS
*/
package com.jms.topic;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSubscriber;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
/**
* @author db2admin
*
*/
public class JMSTopicReciever {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//1) Create and start connection
InitialContext ctx=new InitialContext();
TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup("TCF");
TopicConnection con=f.createTopicConnection();
con.start();
//2) create topic session
TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Topic object
Topic t=(Topic)ctx.lookup("jndi/myTopic");
//4)create TopicSubscriber
TopicSubscriber receiver=ses.createSubscriber(t);
//5) create listener object
JMSTopicListener listener=new JMSTopicListener();
//6) register the listener object with subscriber
receiver.setMessageListener(listener);
System.out.println("Subscriber1 is ready, waiting for messages...");
System.out.println("press Ctrl+c to shutdown...");
while(true){
Thread.sleep(1000);
}
}catch(Exception e){
System.out.println(e);
}
}
}
/**
* LISTENER CLASS
*/
package com.jms.topic;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* @author db2admin
*
*/
public class JMSTopicListener implements MessageListener {
/**
* @param args
*/
@Override
public void onMessage(Message m) {
// TODO Auto-generated method stub
try{
TextMessage msg=(TextMessage) m;
System.out.println("following message is received:"+msg.getText());
}catch(JMSException e){
System.out.println(e);
}
}
}
这是我的JNDI连接详细信息:
我在哪里犯了错误?
答案 0 :(得分:3)
在J2SE中,您需要指定WAS使用的初始上下文工厂的类,如下所示:
Properties env = new Properties();
env.put(Context.PROVIDER_URL,"iiop://<your WAS server>:<port usually 2809>");
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext jndi = new InitialContext(env);
答案 1 :(得分:0)
已解决INITIAL_CONTEXT_LOOKUP。