9 Replies Latest reply on Sep 13, 2011 3:13 AM by Kshitiz Garg

    programmatic topic creation using core API

    Kshitiz Garg Newbie

      Hi,

       

      I want to create a topic architecture programmatically using core API.

       

      To create a topic , say "hornetQTopic" In JMS, we say:

       

      Topic topic = org.hornetq.api.jms.HornetQJMSClient.createTopic("hornetQTopic")

       

      so we only need a topic name in for a topic creation. And then we create consumers like this:

       

      MessageConsumer messageConsumer = session.createConsumer(topic);

       

      I want to achieve the above using core API. As I understand, there is no need to explicitly create a topic in core API and we can use the 'address' parts in queue creation to create a 'topic' like architecture. So I will NOT create any topic . When the consumer wants to subscribe to a particular topic, say "hornetQTopic" , I will a create a queue on the behalf of that consumer like this:

       

      session.createQueue(getSimpleString("hornetQTopic"), getSimpleString("consumerQueue"), true);

       

      and messages will be published like this:

       

      messageProducer.send(getSimpleString("hornetQTopic"), clientMessage);

       

      In the above method, there is one extra parameter "consumerQueue" to be used as the actual queue name to associate it with the address "hornetQTopic".

       

      The problem is that in JMS, there was no such string needed. My problem is that how do I get this queueName since my consumers won't give this name. Do I have to create an unique string on their behalf like this:

       

      private static AtomicInteger counter = new AtomicInteger();

      public static String createQueueName() {

           return "sub_queue" + counter.addAndGet(1);

      }

       

      Isn't it complicated, is there anything which I am missing here ? I could not find any example of topic architecture using core APIs in the bundled examples.

       

      Regards,

      Kshitiz

        • 1. Re: programmatic topic creation using core API
          Andy Taylor Master

          There is no concept of topics in core, just addresses and queues, think of it this way:

           

          A queue is:

           

          address1 -> queue1 -> consumer1, consumer2 etc

           

          where as a topic is

           

          address1 -> queue1 -> consumer1

                            -> queue2 -> consumer2

          1 of 1 people found this helpful
          • 2. Re: programmatic topic creation using core API
            Kshitiz Garg Newbie

            Hi Andy,

             

            We are moving from JMS to core. In JMS,  we just had tro create a topic and create consumers on them. HornetQ JMS wrapper would have been generating queues for these consumers internally. Now our API is still same and we want to use core API. so clients would not be passing the queueNames (which HornetQ JMS wrapper would have been generating internally). How do I generate queueNames for them as I have to create queues to map to the JMS agnostic HornetQ architecture.

             

            Regards,

            Kshitiz

            • 3. Re: programmatic topic creation using core API
              Andy Taylor Master

              We are moving from JMS to core. In JMS,  we just had tro create a topic and create consumers on them. HornetQ JMS wrapper would have been generating queues for these consumers internally. Now our API is still same and we want to use core API. so clients would not be passing the queueNames (which HornetQ JMS wrapper would have been generating internally). How do I generate queueNames for them as I have to create queues to map to the JMS agnostic HornetQ architecture.

              Thats really an application issue, you can call them anything you want, its really up to you. Im not sure i see the issue!

              • 4. Re: programmatic topic creation using core API
                Clebert Suconic Master

                You can compose your queue names the same way the JMS layer would do. client_id ++ subscription name.

                 

                 

                If you had that in your application before, you can just use that to concatenate the queue name. (what will give you the same function)

                1 of 1 people found this helpful
                • 5. Re: programmatic topic creation using core API
                  Kshitiz Garg Newbie

                  Based on your suggestions and our current application state,  I am now making the queue names as:

                   

                   

                  private static AtomicLong counter = new AtomicLong();

                   

                  public <T> String subscribe(String topicName){

                       .....

                       .....

                       String queueName = topicName + "queue" + counter.addAndGet(1);

                       clientSession.createQueue(topicName, queueName);

                       return queueName;

                  }

                   

                   

                  and returning queueName as the subscriberID. Now when a client wants to publish a message on that topic, I would say

                  ....

                   

                  messageProducer.send(topicName, message)

                  ....

                   

                  Should be fine now...

                   

                  Thanks !

                  • 6. Re: programmatic topic creation using core API
                    Kshitiz Garg Newbie

                    Hi Clebert,

                     

                    The approach mentioned by me above would not work when clients from multiple JVMs will ask my code to create unique subscriptions as the line private static AtomicLong counter = new AtomicLong();  will initiate the counter as 0 in all those JVMs and this counter will not help me creating unique queue names. So I am stuck again.

                     

                    Let me explain the real problem:

                    JMS:

                    In our application when clients were calling createTopic(topicName) we were creating topics on HornetQ like this: org.hornetq.api.jms.HornetQJMSClient.createTopic(topicName);

                    When subsribers were coming to subscribe on that topic , they were calling subscribe(topicName, subscriberlistener) and we were doing

                    javax.jms.MessageConsumer consumer = session.createConsumer(topic);

                    consumer.setMessageListener(subscriberlistener);

                    While running the above code, I assume that the hornetQJMS wrapper was creating queues on behalf of the subscribers which were bounded with the address 'topicName'. Those queues created by hornetQ should have been unique even if  createTopic and the subscribe were happening from different JVMs. No worries!

                     

                    Core API:

                    When clients call createTopic(topicName) , I can not create a topic as I was doing with JMS. However, I will just create a queue named ["dummyQueue"+topicName] to maintain the uniqueness of that address in our application. [I guess HornetQ JMS wrappers would have been doing something similar as well, please let me know if that's not correct]. Now when a subscriber calls subscribe(topicName, subscriberlistener) , this is the time I have to create a queue and bind that queue to the address topicName so that if somebody publishes later on that address (=topicName), it will be pushed to the subscriber's queue. The problem is that I have to generate this unique queueName for that subscriber across multiple JVMs which HornetQJMS wrappers were doing for me in JMS implementation.

                     

                    I am using singleton instances of ClientSession in each JVM. Is there any property in this object or in any other hornetQ core library objects which is unique for a JVM. I can use that unique property to generate unique queue names when my subscribers are coming from different JVMs.

                    When you said that I can create queueNames like 'client_id ++ subscription name' , what are these 2 parameters ? will they be unique across JVMs?

                     

                    Please suggest...

                     

                    Regards,

                    Kshitiz Garg

                    • 7. Re: programmatic topic creation using core API
                      Andy Taylor Master

                      This is really an issue for your application, its up to you how you create the queue name it doesnt really matter as long as its unique. Try and forget about topics and just think about address -> queue -> consumer.

                       

                      If you are interested in how we create a globally unique name take a look at our source

                      • 8. Re: programmatic topic creation using core API
                        Kshitiz Garg Newbie

                        I have downloaded the source code but not able to build it as there is no build.sh . Can you please point me to the code where HornetQJMS warpper creates that unique name when a consumer is created?

                        • 9. Re: programmatic topic creation using core API
                          Kshitiz Garg Newbie

                          HI,

                           

                          I used topicName + UUID.randomUUID() to create queue names .

                           

                          Thanks,

                          Kshitiz Garg