How do I use JMS efficiently

 FAQ > JMS > How do I use JMS efficiently

How do I use JMS efficiently?

JMS is designed for high performance. In particular its design is such that you are meant to create a number of objects up front on the startup of your application and then resuse them throughout your application. e.g. its a good idea to create upfront and then reuse the following

  • Connection
  • Session
  • MessageProducer
  • MessageConsumer

The reason is that each create & destroy of the above objects typically requires an individual request & response with the JMS broker to ensure it worked. e.g. creating a connection, session, producer, then sending a message, then closing everything down again - could result in 7 request-responses with the server!

Note a little trick - you can reuse the same MessageProducer for sending messages to different destinations; just create it with a null destination and specify it on the send method.

MessageProducer instances that are created with a null destination are anonymous producers and as such the broker cannot fire an advisory for producer when these are created. This means that when the pooled MessageProducer is in use no advisories for producers will ever be sent.

Using Pooling with JMS

To use JMS efficiently we recommend you use Camel to hide the JMS API and bind the messaging to your beans.

Alternatively try using Spring’s MessageListenerContainer for consuming messages and JmsTemplate for sending - but be aware of the gotchas

Other performance tips

Also see

Java Connector Architecture

Java Connector Architecture supports the pooling of JMS connections, sessions and MessageListeners, parallel message processing, thread pooling and transaction & exception handling, through the use of a JCA Resource Adapter.

All of these benefits are also available through Spring’s MessageListenerContiner except for the XA transactions. If you need XA support then you must use JCA

Typically JCA is used in a J2EE container via MDBs; though there is a POJO Spring based JCA Container you can use, which is simple and lightweight and easy to embed inside Tomcat or any J2SE application.

Apache, ActiveMQ, Apache ActiveMQ, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2024, The Apache Software Foundation. Licensed under Apache License 2.0.