JMS: simple program (urgent please)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JMS: simple program (urgent please)

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    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.

  2. #2
    Join Date
    Mar 2005
    Posts
    1

    the solution

    i had the same problem last month,if u will get the book : Sams Teach Yourself j2Ee In 21 Days it will solve the problem. a chapter of this book is found in:
    so read this article : http://www.informit.com/articles/art...74364&seqNum=3

    to make it easier the only soultion of the 3 solutions offerd that did really solve the problem is :

    JNDI Properties Files

    but i after i solve this problem i am stuck with this error :

    javax.naming.NoInitialContextException: Cannot instantiate class:
    com.sun.jndi.cosnaming.CNCtxFactory [Root exception is java.lang.ClassNotFoundExcept
    ion: com/sun/jndi/cosnaming/CNCtxFactory

    the sam article says that its a classpath problem that it doesnt contain the jndi classes but i included the j2ee in the classpath so does anyone have a clue?please help!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center