6 Replies Latest reply on Jul 6, 2011 3:20 AM by into_java

    Anti -Pattern for creating Session/ Producer

    into_java

      I was going through the User Guide and Docs for Hornetq and in the Hornetq User Manual in chapter Performance tuning its been written that

       

      Creating new sessions and new consumer and new producer.....is an anti-pattern "Re-useconnections/sessions/consumers/producers.Probablythemostcommonmessaging anti-pattern we see is users who create a new connection/session/producer for every message they send or every message they consume. This is a poor use of resources. These objects take time to create and may involve several network round trips. Always re-use them."

       

       

      I would like to know from community if there are good example of doing this in the examples provided with installation

      or anywhere

      or someone can help me with links on this topic.

       

      Thanks and appreciate it.

        • 1. Re: Anti -Pattern for creating Session/ Producer
          gaohoward

          I think the doc says it clearly. Every example in hornetq should follow the rule, although many of them only send/receive one message. I found there is a 'perf' example that sends mulitple messages. You can take a look.

           

          Howard

          • 2. Re: Anti -Pattern for creating Session/ Producer
            into_java

            Hi Yong,

            I checked up the perf example and i see it does send messages twice but there is a anti-pattern used there itself as the code says whenever we are sending message a new factory and producer is created. I would like to know how can we avoid this antipattern of creating new producers everytime

            i have a new message in a new thread.

             

            To put it in different manner. Each of my threads generate a object which i need to put in queue. Right now i am creating a new session for each of them(I have a single instance of factory for all of them-- from which i create session). But the book says its an anti-pattern to create a session for each message. How can this be avoided. The problem is if i create one session for all of these threds then each session can cater to max of one thread at a time so all other threads would be blocking.

            I hope you understand where i am going.

             

            Thanks

            • 3. Re: Anti -Pattern for creating Session/ Producer
              clebert.suconic

              You just need to cache the object accordingly. How you do it, it will really depend on what you are doing.

               

               

              If you have a single thread living, keep it there.

              • 4. Re: Anti -Pattern for creating Session/ Producer
                into_java

                I will try and cache the session object. My only concern is the same session object will now be used across multiple threads to send data. Will try it out and see how the performce is.Thanks

                • 5. Re: Anti -Pattern for creating Session/ Producer
                  clebert.suconic

                  You have to safely reuse it.

                   

                  Example, a cached instance on a Servlet, but make its usage thread safe.

                  • 6. Re: Anti -Pattern for creating Session/ Producer
                    into_java

                    The performce improved drastically on using the same Session object for multiple producers. I am still using one Anti-pattern of creating multiple producer from single session....but the performance is better than multiple session. I am not sure if i can make the producer thread safe..but it seems ok for now..will update the thread is needed.