2 Replies Latest reply on Jul 13, 2014 11:54 PM by gkopff

    Is 'ClientSession' only safe to use from the thread that creates it?

    gkopff

      The Javadoc for ClientSession says: "A ClientSession is a single-thread object required for producing and consuming messages."

       

      My question is: does this mean that it can only be used by the thread that created it?  Or, does it mean that it must only be used by a single thread at a time?

       

      By extension, does the same then apply to the consumer and/or producer created by the session?

       

      The documentation makes it clear that producers are intended to be re-used:

       

      Please note that JMS connections, sessions, producers and consumers are designed to be re-used.

      It is an anti-pattern to create new connections, sessions, producers and consumers for each message you produce or consume.

       

      If I have a thread-pool of service handlers (think: servlet thread pool) and wish to publish a message, what would be the correct approach to take?  Must I either commit the anti-pattern sin and create a new producer each time?  Or can I use a synchronisation control to merely arbitrate access by many threads to a single producer?  (The final option I see is to enqueue the request and have a single publisher read from the queue and publish, but this is more complexity that I would have hoped for).

       

      Thanks.

       

      --

      Greg.

        • 1. Re: Is 'ClientSession' only safe to use from the thread that creates it?
          jbertram

          My question is: does this mean that it can only be used by the thread that created it?

          No.

           

          Or, does it mean that it must only be used by a single thread at a time?

          Yes.

           

          By extension, does the same then apply to the consumer and/or producer created by the session?

          Yes.

           

          If I have a thread-pool of service handlers (think: servlet thread pool) and wish to publish a message, what would be the correct approach to take?

          If you are in a Java EE application server (e.g. JBoss AS or WildFly) then the best option would be to use a <pooled-connection-factory> and "create" the connection, session, and producer each time you need to send a message (or batch of messages).  This is because the pooled-connection-factory maintains a pool of connections behind the scenes so "creating" a connection just grabs one from the pool.  Aside from this you can implement your own pooling strategy to avoid anti-patterns.  You are certainly never obligated to employ an anti-pattern.

          • 2. Re: Is 'ClientSession' only safe to use from the thread that creates it?
            gkopff

            Hi Justin.

             

            Thank you for clearing that up.

             

            Regarding your EE comment, I'm not running in a container - but now I understand the synchronisation requirements I'm good to proceed.  Thank you again.

             

            Off-topic: I believe you saw I sent a pull request to enhance some documentation a bit - if there's anything I should be doing differently in terms of submissions, please let me know.

             

            Kindest regards,

             

            --

            Greg.