1 Reply Latest reply on May 17, 2007 8:47 AM by jaikiran pai

    Simple JMS Topic "lookup" problem

    thoste Newbie

      I am trying to implement a simple (most simple) JMS Topic.
      Unfortunately I am getting an error when running it:

      Exception in thread "main" javax.naming.NameNotFoundException: Topic not bound

      What should I change ?
      Thomas


      The (short) source code is as follows:

      package test.jms.topics;

      import java.util.Properties;

      import javax.jms.Message;
      import javax.jms.Topic;
      import javax.jms.TopicConnection;
      import javax.jms.TopicConnectionFactory;
      import javax.jms.TopicSubscriber;
      import javax.jms.TopicPublisher;
      import javax.jms.TopicSession;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import javax.naming.Context;
      import javax.naming.InitialContext;

      public class TopicPubSubClient
      {

      public static void main(String[] args) throws Exception
      {
      log.info("Creating jndi context - alternatively use a jndi.properties");
      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");
      InitialContext ctx = new InitialContext(properties);

      log.info("Looking up connection factory");
      TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("UIL2ConnectionFactory");

      log.info("Looking up Topic mytopic");
      Topic mytopic = (Topic) ctx.lookup("Topic/testTopic");

      log.info("Creating connection");
      TopicConnection tc = tcf.createTopicConnection();
      try
      {
      log.info("Creating session");
      TopicSession ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

      log.info("Creating sender");
      TopicPublisher sender = ts.createPublisher(mytopic);

      log.info("Creating message");
      TextMessage message = ts.createTextMessage("hello");

      log.info("Sending message");
      sender.send(message);

      log.info("Creating receiver");
      TopicSubscriber receiver = ts.createSubscriber(mytopic);

      log.info("Try to receive message, it will not work");
      Message received = receiver.receiveNoWait();
      if (received != null)
      throw new RuntimeException("Should not get a message if the connection is not started!");

      log.info("You have to start the connection before receiving messages");
      tc.start();

      log.info("This receive will work");
      received = receiver.receiveNoWait();

      log.info("Got message: " + received);
      }
      finally
      {
      tc.close();
      }
      }

      public static class log
      {
      public static void info(String message)
      {
      System.out.println(message);
      }
      public static void error(String message, Throwable t)
      {
      System.err.println(message);
      t.printStackTrace();
      }
      }
      }