2 Replies Latest reply on Jun 8, 2010 5:47 AM by Alexander Cherpicka

    HornetQ createTopic programmatically

    Alexander Cherpicka Newbie

      Hello.

      I'm using JBoss 5.1.0.GA and HornetQ.

      When I'm deploy topic from hornetq-jms.xml

      ...
      <topic name="JOdbPingerTopic">
             <entry name="/topic/JOdbPingerTopic"/>
      </topic>
      ...
      

      everithing allright.

       

      But I need to create topic programmatically.

       

      void subscribe()
          {
              try
              {
                  MessageConsumer consumer = session.createConsumer(topic);
                  consumer.setMessageListener( new MessageListener() {
                      @Override
                      public void onMessage(final Message message) {
                          System.out.println("New message!");
                      }
                  } );
              }
              catch ( Exception ex ) {
                  ex.printStackTrace();
              }
          }

       

              Properties p = new Properties();
              p.put("java.naming.factory.initial", "org.jboss.naming.NamingContextFactory");
              p.put("java.naming.provider.url", "localhost");
              p.put("java.naming.factory.url.pkgs", "org.jboss.naming");
      
              ctx = new InitialContext( p );
      
              ConnectionFactory connectionFactory = (ConnectionFactory)ctx.lookup("ConnectionFactory");
              try{
                     //Connecting to topic if exist
                  topic = (Destination)ctx.lookup("testTopic");
              }catch(Exception e){
                     //Create new topic if does not exist
                  topic = (Destination)HornetQJMSClient.createTopic("testTopic");
              }
      
              Connection conn = connectionFactory.createConnection();
              session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
              conn.start();
      
              subscribe()
      

       

      I'm get exception

       

      javax.jms.InvalidDestinationException: Topic testTopic3 does not exist
      at org.hornetq.jms.client.HornetQSession.createConsumer(HornetQSession.java:539)
              at org.hornetq.jms.client.HornetQSession.createConsumer(HornetQSession.java:375)
              at org.hornetq.jms.client.HornetQSession.createConsumer(HornetQSession.java:345)
              at jmstest.Main.subscribe(Main.java:this string -> MessageConsumer consumer = session.createConsumer(topic);)