10 Replies Latest reply on Feb 5, 2013 12:38 PM by jbertram

    Create queues and topics at run-time

    st.bolli

      Hi all,

            I am a newbie with HornetQ 2.1.2.Final on Jboss 5.1.0AS

       

      I need to dynamically create queues (and eventually topics) from the Java code at run-time. After their creation it is necessary for my purpose to register the created queues on JNDI.

       

      Surfing the forum I find the following solution, but it is not clear to me how to bind the created queue to the JNDI bind("jndiString", what object?)

       

              ClientSessionFactory sf = new ClientSessionFactoryImpl(new TransportConfiguration(NettyConnectorFactory.class.getName()));

              ClientSession coreSession = sf.createSession(false, false, false);

              String queueName = "jms.queue.example00";

              coreSession.deleteQueue(queueName);

              coreSession.createQueue(queueName, queueName, true);

       

      How can be binded the created queue to JNDI?

      Are there any other possible solution for the same purpose?

       

      Thanks!

        • 2. Re: Create queues and topics at run-time
          st.bolli

          Ok, the docs link work correctly writing the followng example...

           

          public class CreateRuntimeQueueJMS1 {
          
              private static final String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi";
              
              public static void main(String[] args) throws Throwable {
              
                   System.out.println("Start.");
                  
                   JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap());
                   MBeanServerConnection mbsc = connector.getMBeanServerConnection();
                   
                   ObjectName name=new ObjectName("org.hornetq:module=JMS,type=Server");
                   JMSServerControl control = (JMSServerControl)MBeanServerInvocationHandler.newProxyInstance(mbsc,name,JMSServerControl.class,false);
                   control.createQueue("testQ1","queue/testQ1");
                   
                   System.out.println("End.");
          
              }
          }
          

           

          ...but in this way it is necessary to enable the JMX Console remote management adding the following option to JVM_OPT

           

          set "JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote 
                                     -Dcom.sun.management.jmxremote.port=3000
                                     -Dcom.sun.management.jmxremote.ssl=false 
                                     -Dcom.sun.management.jmxremote.authenticate=false"
          

           

           

          In my deployment environment the JMX console is disable and cannot be activated for security reason (neither considering ssl and authentication).

          Is there another way to create and bind to JNDI a new queue at run-time without using the JMX?

           

          Thanks!

          1 of 1 people found this helpful
          • 3. Re: Create queues and topics at run-time
            jmesnil

            Stefano Bolli wrote:

             

            Is there another way to create and bind to JNDI a new queue at run-time without using the JMX?

            You can use JMS to send management messages: http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/management.html#management.jms

             

            HornetQ comes with a "management" example which illustrate this.

            • 4. Re: Create queues and topics at run-time
              st.bolli

              Ok, the queue is correctly created, but qhen I restart the JBoss server it is not binded in JNDI.

               

              The same problem is presented here:

              http://community.jboss.org/post.jspa?container=2066&containerType=14&thread=153260&message=560972&reply=true

               

              Do you have any suggestion about this issue?

               

              Thanks !

              • 5. Re: Create queues and topics at run-time
                jmesnil

                Stefano Bolli wrote:

                 

                Ok, the queue is correctly created, but qhen I restart the JBoss server it is not binded in JNDI.

                 

                The same problem is presented here:

                http://community.jboss.org/post.jspa?container=2066&containerType=14&thread=153260&message=560972&reply=true

                 

                Do you have any suggestion about this issue?

                 

                Thanks !

                this bug's been fixed: http://community.jboss.org/message/561152#561152

                • 6. Re: Create queues and topics at run-time
                  lukhash

                  Hi

                       I want to use HornetQ 2.2.13 on JBoss AS 7.1.1 and a don't know how can I create topics dynamically.

                       I found many solution to older version HornetQ and JBoss but it's dont work with HornetQ 2.2.13.

                  Can anyone write a simple method to create topics at runtime?

                  Thanks!!!

                  • 7. Re: Create queues and topics at run-time
                    ataylor

                    Hi

                         I want to use HornetQ 2.2.13 on JBoss AS 7.1.1 and a don't know how can I create topics dynamically.

                         I found many solution to older version HornetQ and JBoss but it's dont work with HornetQ 2.2.13.

                    Can anyone write a simple method to create topics at runtime?

                    Thanks!!!

                    you need to use the AS7 CLI (command line interface), take a look at teh AS7 docs.

                    • 8. Re: Create queues and topics at run-time
                      lukhash

                      Thanks for quick response

                       

                      yes, I can use CLI but I want create topic programmatically from my web application

                      • 9. Re: Create queues and topics at run-time
                        ataylor

                        as far as i am aware you can call the CLI programmatically from a servlet. There would be more in the AS7 docs, its not really a HornetQ question.

                        • 10. Re: Create queues and topics at run-time
                          jbertram

                          Here's a simple example of how to create a JMS queue via the JBoss AS7 CLI  programmatically:

                           

                          import org.jboss.as.controller.client.ModelControllerClient;
                          import org.jboss.as.controller.client.helpers.ClientConstants;
                          import org.jboss.dmr.ModelNode;
                          
                          
                          import java.net.InetAddress;
                          
                          
                          public class ManagementExample
                          {
                             public static void main(final String[] args) throws Exception
                             {
                                ModelControllerClient client = null;
                                ModelNode op = new ModelNode();
                                ModelNode address = op.get(ClientConstants.OP_ADDR);
                                address.add("subsystem", "messaging");
                                address.add("hornetq-server", "default");
                          
                          
                                // the name of the queue
                                String queue = "myQueue";
                                address.add("jms-queue", queue);
                          
                          
                                // the JNDI entries
                                ModelNode entries = op.get("entries");
                                entries.add("jms/queue/" + queue);
                                entries.add("jboss/exported/jms/queue/" + queue);
                          
                          
                                op.get(ClientConstants.OP).set(ClientConstants.ADD);
                          
                          
                                try
                                {
                                   client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999);
                                   ModelNode result = client.execute(op);
                                   System.out.println(result);
                                }
                                finally
                                {
                                   if (client != null)
                                   {
                                      try
                                      {
                                         client.close();
                                      }
                                      catch (Exception e)
                                      {
                                         // no-op
                                      }
                                   }
                                }
                             }
                          }