The Java messaging service is a messaging oriented middleware (MOM) which allows your application to receive and send messages. The goal: A reliable and asynchronous communication(). Many developers who try to connect their application to a messaging provider, need to understand the specific messaging provider`s API. Especially when the API provides a huge amount of “options that can prove fatal in the hands of an inexperienced developer” (, p.12). The following aspects may help you to use JMS more easily.
Which JMS version to use
JMS is a component of Java EE. The latest version of JMS (2.0) is compatible with Java EE7. If you choose the newest version, take into account that your application server does have to support Java EE 7. For example you are not able to use JMS 2.0 with JBoss EAP 6.3 as it supports Java EE 6 (, p.16).
Close Connection, Context, Producer and Consumer when finished
The version 2.0. provides new Interfaces (JMSContext, JMSProducer, JMSConsumer). All those interfaces implement Autoclosable (). So you do not need to define an extra finally block to close for example your JMSContext. Just use the try-with-resources block, a feature of Java 7. But if you use an older JMS version you do have to close Producer, Consumer and the Context explicitly. The Connection doesn’t implement Autoclosable, not even with version 2.0. So when the connection is not used anymore, do explicitly close the connection.
With version 2.0 it is mandatory to define JMSXDeliveryCount. JMSXDeliveryCount defines for an application how often a message should be redelivered (). Do define that number even when using older JMS versions. When your application fails to send your message by -let’s say- at least the third time, it indicates strongly that something is wrong. You may notice the problem earlier by setting that number.
Set the memory wisely for your Messages
Messages have a lifetime. But the message may not be needed forever. Think about defining an expiry date for your messages, by declaring the time-to- live (MessageProducer.setTimeToLive(long timeToLive)).
Use Message Buffer Size option to define the amount of memory that shall be used for storing the message body before paging then out todisk. Be aware that the size is normally one third of the heap size of the JVM but 512MB at most. The higher the Message Buffer Size parameter is set, “the more memory JMS will consume when many messages are waiting on queues or topics” ().
Think about which JMS Message Types to use
JMS provides 5 different types. Look at the documentation for more information () :
- BytesMessage -Stream of uninterpreted bytes
- MapMessage – Representing name(String)/value(java primitive) pairs
- ObjectMessage – Message containing a single serializable Java object.
- StreamMessage- Stream of uninterpreted bytes representing only Java primitive types
- TextMessage- Single String which may contain XML content
When choosing your message type take into consideration that BytesMessage and TextMessage are the only Message Types which don’t restrict the message by java primitives or java objects.
Don’t create connections too soon
When coding don’t create objects or elements if they are not needed in that moment. With JMS you may think about a sender and a receiver client. You will start a connection within your sender and another one within your receiver client. Don’t start a connection within your receiver client before starting a connection within your sender . You cannot receive anything if the sender isn’t able to send a message.
Application Server Facilities
Application Server Facilities enable JEE servers to implement JMS related processing functions. So they ARE targeted at the JEE server and IS an optional part of JMS. So they MAY be supported by your JMS provider. As it only MAY be supported, it is not recommended to directly use ASF in your JMS client (, 26). Even if your JMS provider supports ASF now, what if you change the JMS provider in the future? So you rather shouldn’t use ASF in your Client.
 Yusuf, Kareem (2004): Enterprise Messaging Using JMS and IBM WebSpehre, Prentice Hall PTRUpper Saddle River, NJ, USA2004