11 Replies Latest reply on Apr 6, 2007 10:07 PM by Anne Forumer

    createTopic Error!

    Leio Wu Newbie

      the error part is :


       private Context ic = null;
       private TopicConnectionFactory factory = null;
       private TopicConnection connection = null;
       private TopicSession session = null;
       private MessageProducer producer = null;
       private Topic topic = null;
       try{
       ic = new InitialContext();
       }catch(NamingException e)
       {
       e.printStackTrace();
       logger.warn("error in InititialContext()");
       return;
       }
       //connect to factory
       try{
       factory = (TopicConnectionFactory)
       ic.lookup("ConnectionFactory");
       }catch(NamingException e)
       {
       logger.warn("Error in lookup Connectionfactory");
       e.printStackTrace();
       return;
       }
      
       //get connection
       try
       {
       connection = factory.createTopicConnection();
       session = connection.createTopicSession(false, Session. AUTO_ACKNOWLEDGE);
      
       
       topic = session.createTopic("jms/MyTopic");
      
       ...
      
       }catch(Exception e)
       {
       e.printStackTrace();
       }
      


      topic and session are predefined as Topic and TopicSession type ,and session has been created successfully before,but when i run to the line in red listed above, error occurs as:


      javax.jms.JMSException: This destination does not exist !


      but the destination has already been looked up as ConnectionFactory,here i wonder if i should use TopicConnectionFactory?but i can't acess to TopicConnectionFactory! is that due to the possibility that i have missed some jar included in my project?

      btw: why i can't initialize using the string "java:comp/env/jms/ConnectionFactory",is there any difference between the two method?

      thank all the warm-hearted persons here.

        • 1. Re: createTopic Error!
          jaikiran pai Master

           

          "lovejesus" wrote:


          topic = session.createTopic("jms/MyTopic");





          Instead, do the following:

          topic = session.createTopic("topic/MyTopic");


          • 2. Re: createTopic Error!
            Leio Wu Newbie


            the same error still occurs.

            i read the spec saying : better creating topic or queue using jmx-console than
            programming, also i found a very complicated example to add a topic to the system.but is there any easy way to do this like
            topic = session.createTopic("topic/MyTopic")
            to create a new topic in jboss?

            thanks. hope more guys could be involved in this discussion.

            • 3. Re: createTopic Error!
              jaikiran pai Master

              Have you configured the topic in jboss? Something similar to:

              <mbean code="org.jboss.mq.server.jmx.Topic"
               name="jboss.mq.destination:service=Topic,name=MyTopic">
               <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
               </mbean>


              have a look at jbossmq-destinations-service under server/default/jms for sample topic configuration


              • 4. Re: createTopic Error!
                Hubert Iwaniuk Newbie

                If you have MDB that listens on Queue/Topic that doesn't exist at deployment time,
                Queue/Topic is created by JBoss.

                • 5. Re: createTopic Error!
                  jaikiran pai Master

                  jmx-console is the best place to see whether your topic exists or not:

                  http://localhost:8080/jmx-console

                  Then under:

                  jboss.mq.destination

                  see if your topic is present

                  • 6. Re: createTopic Error!
                    Leio Wu Newbie


                    If you have MDB that listens on Queue/Topic that doesn't exist at deployment time,Queue/Topic is created by JBoss.


                    this is exactly what i want!

                    for example , topic/MyTopic does'nt exist at the deploy time,so i want to create one topic named MyTopic at the run time.

                    but when the Queue/Topic already exists ,createTopic works well ! ( but i don't want to create an old one!),but when the name doesn't exist , error occurs.

                    how can i create a topic dynamically?


                    • 7. Re: createTopic Error!
                      Patrick Newbie

                      I implemented the following to be able to create dynamic queues. I am sure it can easily be adapted for a Topic:

                       /**
                       * Gets the queue from JNDI. This is synchronized as if the queue does not
                       * exist, a new queue is created.
                       * @param iniCtx
                       * @param destination
                       * @return
                       * @throws Exception
                       */
                       public synchronized Queue getQueue(InitialContext iniCtx,String destination) throws Exception{
                       Queue queue=null;
                       try {
                       queue= (Queue)iniCtx.lookup(destination);
                       } catch (NameNotFoundException e){
                       System.out.println("Queue "+destination+" does not exist.");
                       }
                       if (queue==null)
                       queue=createQueue(iniCtx,destination);
                       return queue;
                       }
                      
                       /**
                       * Creates a new queue using a JbossMQ (DestinationManagerMBean) only method,
                       * thus breaking the JMS specifications.
                       * @param iniCtx
                       * @param destination
                       * @return
                       * @throws Exception
                       */
                       private Queue createQueue(InitialContext iniCtx,String destination) throws Exception{
                       Queue queue=null;
                      
                       // Get the MBean server
                       MBeanServer server = (MBeanServer)
                       MBeanServerFactory.findMBeanServer(null).iterator().next();
                      
                       // Invoke the MBean
                       server.invoke(
                       new ObjectName("jboss.mq:service=DestinationManager"),
                       "createQueue",
                       new Object[] { destination, destination },
                       new String[] { String.class.getName(), String.class.getName()}
                       );
                      
                       queue=(Queue)iniCtx.lookup(destination);
                       System.out.println("Queue "+destination+" has been created.");
                       return queue;
                       }
                      


                      With JBossMQ, if you have messages in the queue and your system dies, the next time you startup and you create the queue dynamically like this, the messages are still there and you will start off from where you left off before you system crashed.

                      • 8. Re: createTopic Error!
                        Hubert Iwaniuk Newbie

                         

                        "lovejesus" wrote:

                        for example , topic/MyTopic does'nt exist at the deploy time,so i want to create one topic named MyTopic at the run time.

                        If it doesn't exist at deployment time it is created by JBossAS, not you.
                        "lovejesus" wrote:

                        but when the Queue/Topic already exists ,createTopic works well ! ( but i don't want to create an old one!),but when the name doesn't exist , error occurs.

                        Thats how createTopic should work.
                        I think you should read JMS spec.
                        I looks like you are missing the point of createTopic.

                        • 9. Re: createTopic Error!
                          digby lock Newbie

                          once a new topic is created programatically, does anyone know if it is possible to programatically deploy an MDB to subscribe to the new queue? Am trying to figure out a way of using MDB to subscribe to a topic which is not yet created at deploy time.

                          • 10. Re: createTopic Error!
                            Hubert Iwaniuk Newbie

                             

                            "digbylock" wrote:
                            once a new topic is created programatically, does anyone know if it is possible to programatically deploy an MDB to subscribe to the new queue? Am trying to figure out a way of using MDB to subscribe to a topic which is not yet created at deploy time.

                            If at MDB deployment time no topic/queue, that mdb listens on, is present it is created automaticaly.

                            • 11. Re: createTopic Error!
                              Anne Forumer Newbie

                              I'd love to know how queues created dynamically, as shown by hargitt, can be used so I can put them to appropriate uses.

                              Can an MDB be made to listen to such a queue?

                              I understand that such a queue is longer lasting than a temporary queue.

                              Thanks