3 Replies Latest reply on Apr 12, 2010 6:21 PM by Leos Bitto

    Pool of JMS objects

    Leos Bitto Novice



      this is my proposal for solution of the Jira issue https://jira.jboss.org/jira/browse/HORNETQ-329


      I could create an implementation of ConnectionFactory, which would manage one shared Connection (created by subordinate ConnectionFactory). The Connection would have an ExceptionListener registered, which would ensure that when there is any problem with this Connection, it gets closed and another shared Connection created instead (lazily when createConnection gets called).


      My implementation of ConnectionFactory could return my implementation of Connection (wrapping the real Connection), which would take care of proper caching of the created Sessions. Similary the created Sessions could cache the created MessageConsumers. At the first attempt I would not cache the MessageProducers - they are mutable, which would make their caching complicated. I hope that creating MessageProducers is actually cheap for most JMS providers - unlike MessageConsumers, which usually have some cache of Messages attached, which makes their creation more expensive.


      At the end the clients would need to keep only the ConnectionFactory, which they would simply use to create Connection, Session and MessageConsumer or MessageProducer, process few messages (maybe only one) and close the Connection - like they would do if they would be using the JCA adapter.


      I hope that my description is understandable. If it is not, maybe that the code could be easier to understand.. so how should I provide the source code? Attach the files to this forum, to the Jira issue, or somewhere else?

        • 1. Re: Pool of JMS objects
          Leos Bitto Novice

          Please check the attached file src.zip - that is my first attempt to provide proper pooling of the JMS objects. I have not tested it properly yet. It should work with any JMS 1.1 compliant provider, with one additional requirement: its implementation of javax.jms.Destination must implement reasonable equals(). HornetQ should be fine for this.


          If you would like to use Spring, here is an example:


              <bean id="connectionFactoryFromJNDI" class="some.package.ConnectionFactoryFromJNDI">
                  <constructor-arg type="java.lang.String">
                  <constructor-arg type="java.util.Hashtable">
                          <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
                          <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
                          <prop key="java.naming.provider.url">jnp://localhost:1099</prop>
              <bean id="sharedConnectionFactory" class="some.package.SharedConnectionFactory" destroy-method="close">
                  <constructor-arg type="javax.jms.ConnectionFactory" ref="connectionFactoryFromJNDI" />
              <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                  <constructor-arg type="javax.jms.ConnectionFactory" ref="sharedConnectionFactory" />


          Obviously you do not need to use JmsTemplate, you can use the pure JMS interface only, starting with the provided ConnectionFactory implementation.

          • 2. Re: Pool of JMS objects
            Leos Bitto Novice

            Because the behaviour of equals() on javax.jms.Destination is not defined in the JMS specification, I changed my code to avoid using it - I use Queue#getQueueName() and Topic#getTopicName() instead. Check the attached file src2.zip.

            • 3. Re: Pool of JMS objects
              Leos Bitto Novice

              Closing of MessageConsumers with MessageListeners left something to be desired, so here is another version: src3.zip


              Any comments?