1 Reply Latest reply on Aug 29, 2002 4:01 AM by Joel Vogt

    any body success useing JMS on jboss3.01?

    lizhi2002 Newbie

      I download the example of jms?
      when run HelloPublisher.java
      it alway show error ,is any place I make mistake?

      /*
      * JBoss, the OpenSource EJB server
      *
      * Distributable under LGPL license.
      * See terms of license at gnu.org.
      */

      //package org.jboss.docs.jms.client;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      import javax.jms.TopicConnectionFactory;
      import javax.jms.TopicConnection;
      import javax.jms.TopicSession;
      import javax.jms.TopicPublisher;
      import javax.jms.Topic;
      import javax.jms.TextMessage;
      import javax.jms.Session;
      import javax.jms.JMSException;
      /**
      * Simple JMS client, publish text messages to testTopic Topic.
      *
      *
      * NOTEThis code is showcase only. It may not provide a stable production example.
      * @author Peter Antman
      * @version $Revision: 1.1 $
      */

      public class HelloPublisher {

      /**
      * Topic connection, hold on to this so you may close it.
      */
      TopicConnection topicConnection;

      /**
      * Topic session, hold on to this so you may close it. Also used to create messages.
      */
      TopicSession topicSession;

      /**
      * Use this to publish messages.
      */
      TopicPublisher topicPublisher;

      /**
      * Destination to publish to
      */
      Topic topic;


      /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the topic connection factory to look up.
      * @param topicJNDI name of the topic destination to look up
      */
      public HelloPublisher(String factoryJNDI, String topicJNDI) throws JMSException, NamingException {
      // Get the initial context
      System.err.println("1");
      //java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      //java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
      //java.naming.provider.url=localhost
      //System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      //System.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
      //System.setProperty("java.naming.provider.url", "localhost");

      System.err.println("11");
      Context context = new InitialContext();
      System.err.println("2");
      // Get the connection factory
      TopicConnectionFactory topicFactory =
      (TopicConnectionFactory)context.lookup(factoryJNDI);
      System.err.println("3");
      // Create the connection
      topicConnection = topicFactory.createTopicConnection();
      System.err.println("4");
      // Create the session
      topicSession = topicConnection.createTopicSession(
      // No transaction
      false,
      // Auto ack
      Session.AUTO_ACKNOWLEDGE);
      System.err.println("5");
      // Look up the destination
      topic = (Topic)context.lookup(topicJNDI);
      System.err.println("6");
      // Create a publisher
      topicPublisher = topicSession.createPublisher(topic);
      System.err.println("7");
      }

      /**
      * Publish the given String as a JMS message to the testTopic topic.
      */
      public void publish(String msg) throws JMSException {

      // Create a message
      TextMessage message = topicSession.createTextMessage();
      message.setText(msg);

      // Publish the message
      topicPublisher.publish(topic, message);
      }

      /**
      * Close session and connection. When done, no publishing is possible any more.
      */
      public void close() throws JMSException {
      topicSession.close();
      topicConnection.close();
      }

      /**
      * Run an example publishing 10 messages to testTopic. Only works up to and including JBoss 2.4.x
      */
      public static void main(String[] args) {
      try {

      //Create the HelloPublisher, giving it the name of the TopicConnection
      //Factory and the Topic destination to use in lookup.
      System.err.println("11");
      HelloPublisher publisher = new HelloPublisher(
      // Name of ConnectionFactory
      "TopicConnectionFactory",
      // Name of destination to publish to
      "topic/testTopic"
      );
      System.err.println("12");
      // Publish 10 messages
      for (int i = 1; i < 11; i++) {
      String msg = "Hello World no. " + i;
      System.out.println("Publishing message: " + msg);
      publisher.publish(msg);
      }

      // Close down your publisher
      publisher.close();

      }catch(Exception ex) {
      System.err.println("An exception occured while testing HelloPublisher: " + ex);
      ex.printStackTrace();
      }

      }

      } // HelloPublisher