6 Replies Latest reply on Jan 7, 2012 6:38 AM by Andy Taylor

    Create destination at runtime

    Lukáš Sojka Newbie

      Hi,

      I still have not understood how create queue or topic at runtime (programmatically). I read documentation a it is horrible. Can somebody put this short code how create queue or topic programmatically, please?

        • 1. Re: Create destination at runtime
          william cheng Newbie

          Hi,

          The example is to explain how to dynamic create the queue.

           

          public static void main(final String[] args)
          {

           

          HornetQBootstrapServer hornetQ = null;
          try
          {

           

          // Step 1. Start the server
          // hornetQ = new HornetQBootstrapServer("./server0/hornetq-beans.xml");
          // hornetQ.run();

           

          // Step 2. As we are not using a JNDI environment we instantiate
          the objects directly
          ServerLocator serverLocator =
          HornetQClient.createServerLocatorWithoutHA(new
          TransportConfiguration(NettyConnectorFactory.class.getName()));
          ClientSessionFactory sf = serverLocator.createSessionFactory();

           

          // Step 3. Create a core queue
          ClientSession coreSession = sf.createSession(false, false,false);

           

          final String queueName = "queue.exampleWilliamQueue";

           

          coreSession.createQueue(queueName, queueName, true);

           

          coreSession.close();

           

          ClientSession session = null;

           

          try
          {

           

          // Step 4. Create the session, and producer
          session = sf.createSession();

           

          ClientProducer producer = session.createProducer(queueName);

           

          // Step 5. Create and send a message
          ClientMessage message = session.createMessage(false);

           

          final String propName = "myprop";

           

          message.putStringProperty(propName, "William Hello sent at " +new Date());

           

          System.out.println("Sending the message.");

           

          producer.send(message);

          ... ...

          }

          • 2. Re: Create destination at runtime
            Lukáš Sojka Newbie

            Thanks,

            I have tried this, but I have got this error: HornetQException[errorCode=105 message=User: null doesn't have permission='CREATE_DURABLE_QUEUE' on address MyQueue]. I do not understand this, because in hornetq-users.xml is defaultuser defined and in hornetq-configuration.xml are permissions for him defined:

              <security-settings>

                  <security-setting match="#">

                     <permission type="createNonDurableQueue" roles="guest"/>

                     <permission type="deleteNonDurableQueue" roles="guest"/>

                     <permission type="consume" roles="guest"/>

                     <permission type="send" roles="guest"/>

                  </security-setting>

               </security-settings>

            Any suggestions?

            • 3. Re: Create destination at runtime
              william cheng Newbie

              u r welcome!

               

              The solution may refer to hornetq-2.2.5.Final-src\examples\core\microcontainer\server0\hornetq-configuration.xml.

               

              1.  remove <security-settings> in the hornetQ-configuration.xml

               

              2.  then add the bleow configuration in the hornetQ-configuration.xml

               

                 <security-enabled>false</security-enabled>

                

                 <persistence-enabled>false</persistence-enabled>

              • 4. Re: Create destination at runtime
                Lukáš Sojka Newbie

                Thank you very much, it works, but this code create core queue, I would need jms queue and topic.Is it some way how to make it?  

                • 5. Re: Create destination at runtime
                  Lukáš Sojka Newbie

                  Any suggestions? How create jms queue and topic at runtime?

                  • 6. Re: Create destination at runtime
                    Andy Taylor Master

                    read the manual, all this is explained in the management section, aldo there is a management example