|
-
JMS: simple program (urgent please)
I am quite new with the JMS. I am trying to have a simple program running in order to start from it towards the application I want to build.
I tried to run the sample program below:
//////////////////////////////////////////////////////////////////////////////////////////////////////
package chat;
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Properties;
public class Chat implements javax.jms.MessageListener {
private TopicSession pubSession;
private TopicSession subSession;
private TopicPublisher publisher;
private TopicConnection connection;
/* Constructor. Establish JMS publisher and subscriber */
public Chat() throws Exception {
// Obtain a JNDI connection
Properties env = new Properties();
// ... specify the JNDI properties specific to the vendor
InitialContext jndi = new InitialContext(env);
// Look up a JMS connection factory
TopicConnectionFactory conFactory =
(TopicConnectionFactory) jndi.lookup("TopicConnectionFactory");
// Create a JMS connection
TopicConnection connection =
conFactory.createTopicConnection();
// Create two JMS session objects
TopicSession pubSession =
connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
TopicSession subSession =
connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
// Look up a JMS topic
Topic chatTopic = (Topic) jndi.lookup("anytopic");
// Create a JMS publisher and subscriber
TopicPublisher publisher =
pubSession.createPublisher(chatTopic);
TopicSubscriber subscriber =
subSession.createSubscriber(chatTopic);
// Set a JMS message listener
subscriber.setMessageListener(this);
// Intialize the Chat application
set(connection, pubSession, subSession, publisher);
// Start the JMS connection; allows messages to be delivered
connection.start();
}
/* Initialize the instance variables */
public void set(TopicConnection con, TopicSession pubSess,
TopicSession subSess, TopicPublisher pub) {
this.connection = con;
this.pubSession = pubSess;
this.subSession = subSess;
this.publisher = pub;
}
/* Receive message from topic subscriber */
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/* Create and send message using topic publisher */
protected void writeMessage(String text) throws JMSException {
TextMessage message = pubSession.createTextMessage();
publisher.publish(message);
}
/* Close the JMS connection */
public void close() throws JMSException {
connection.close();
}
/* Run the Chat client */
public static void main(String[] args) {
try {
Chat chat = new Chat();
// Read from command line
BufferedReader commandLine = new
java.io.BufferedReader(new
InputStreamReader(System.in));
// Loop until the word "exit" is typed
while (true) {
String s = commandLine.readLine();
if (s.equalsIgnoreCase("exit")) {
chat.close(); // close down connection
System.exit(0); // exit program
} else {
chat.writeMessage(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
But at the line
“TopicConnectionFactory conFactory =
(TopicConnectionFactory) jndi.lookup("TopicConnectionFactory");”
I got this exception:
/////////////
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at chat.Chat.<init>(Chat.java:24)
at chat.Chat.main(Chat.java:94)
/////////////
I realize that the problem is in the jndi setup. But, the problem is that I do not know where to go now and what should I do. I do not know how can I get the jndi.properties file and where and how to put it.
I found a lot of replies on different forums, but no one was very simple to follow. Please, kindly advise from your experience. I would appreciate clear steps to have this program running properly. Otherwise, if you do have a sample program that is already running, please provide it to me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks