Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

openJMS chat example

567558May 24 2007 — edited Jan 4 2009
How do i run this chat example on openjms?
package com.ociweb.jms;
  
  import java.io.*;
  import javax.jms.*;
  import javax.naming.*;
  
  /**
   * Simple console-based JMS chat room client. 
   */
  public class ChatRoom implements MessageListener {
  
      private String name;
  
      private TopicConnection connection;
      private TopicSession subscriberSession;
      private TopicSession publisherSession;
      private TopicPublisher publisher;
      private TopicSubscriber subscriber;
  
      /**
       * Creates a chat room client.
       * @param name the chat room user
       * @param filter ignore own messages if true
       */
      public ChatRoom(String name, boolean filter) throws Exception {
          // set user's name (for chat room display)
          this.name = name;
  
          // look up jms connection factory from JNDI
          Context context = new InitialContext();
          TopicConnectionFactory factory = (TopicConnectionFactory) 
              context.lookup("JmsTopicConnectionFactory");
          // create connection to message server
          connection = factory.createTopicConnection();
  
          // look up chat topic from JNDI
          Topic topic = (Topic) context.lookup("ChatTopic");
  
  
          // create chat message publisher
          publisherSession = 
              connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
          publisher = publisherSession.createPublisher(topic);
  
          // create chat message subscriber
          subscriberSession =
              connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
          String nameFilter = filter ? name : "";
          String selector = "name <> '" + nameFilter + "'";
          subscriber = 
              subscriberSession.createSubscriber(topic, selector, false);
          subscriber.setMessageListener(this);
  
          // join chat room
          connection.start();
      }
      
      /**
       * Invoked when someone posts a message to the chat room.
       * @see javax.jms.MessageListener#onMessage(javax.jms.Mess  age)
       */
      public void onMessage(Message message) {
          try {
              TextMessage chatMessage = (TextMessage) message;
              String msgSender = chatMessage.getStringProperty("name");
              String msgBody = chatMessage.getText();
              System.out.println(msgSender + ": " + msgBody);
          } catch (JMSException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * Publishes a message to the chat room.
       * @param text the message body
       * @throws JMSException
       */
      public void postMessage(String text) throws JMSException {
          // create and publish message
          Message message = publisherSession.createTextMessage(text);
          message.setStringProperty("name", name);
          publisher.publish(message);
      }
  
      /**
       * Closes the JMS connection.
       * @throws JMSException
       */
      public void close() throws JMSException {
          if (connection != null) {
              connection.close();
          }
      }
  
      public static void main(String[] args) throws Exception {
          if (args.length == 0) {
              System.out.println("Usage: ChatRoom <name> [(true|false)]");
              System.exit(0);
          }
  
          String name = args[0];
          boolean filter = args.length > 1 
                           ? "true".equalsIgnoreCase(args[1])
                           : false;
          ChatRoom chat = new ChatRoom(name, filter);
          boolean done = false;
          String text;
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          while (!done) {
              text = in.readLine().trim();
              if ("bye".equalsIgnoreCase(text)) done = true;
              chat.postMessage(text);
          }
          chat.close();
          System.exit(0);
      }
  }
cos
D:\JAVA_P~1\CHAT3>java -cp C:\openjms\openjms\openjms-0.7.7-beta-1.jar;.; ChatRo
om kasia false
Exception in thread "main" java.lang.NoClassDefFoundError: ChatRoom (wrong name:
com/ociweb/jms/ChatRoom)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
doesnt work

thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 1 2009
Added on May 24 2007
8 comments
991 views