Reputation: 1610
I am trying to lookup a JMS TopicConnectionFactory using JNDI in Websphere Application Server. I have done the jndi setup correctly in the server. I have deployed my application in the server.
I have designed 3 java classes for Sender/Reciever/Listener. Then after deploying the WAR in the server, I have first tried to run the Receiver Code as a standalone java application. However it failed with error as :
Exception in thread "main" javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:242)
Attaching my code snippet below :
/**
* 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);
}
}
}
Here is my JNDI Connection details :
Where am I making the mistake?
Upvotes: 2
Views: 1366
Reputation: 2977
In J2SE, you need to specify the class of the initial context factory use by WAS like this :
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);
Upvotes: 3