About to use JMS? Consider these crucial points!

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([6]). 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” ([1], 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 ([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 ([2]). 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.

Consider JMSXDeliveryCount

With version 2.0 it is mandatory to define JMSXDeliveryCount. JMSXDeliveryCount defines for an application how often a message should be redelivered ([3]). 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” ([4]).

Think about which JMS Message Types to use

JMS provides 5 different types. Look at the documentation for more information ([5]) :

  • 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 [7]. 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 ([1], 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.

References

[1] Yusuf, Kareem (2004): Enterprise Messaging Using JMS and IBM WebSpehre, Prentice Hall PTRUpper Saddle River, NJ, USA2004

[2] http://www.oracle.com/technetwork/articles/java/jms20-1947669.html

[3] http://www.oracle.com/technetwork/articles/java/jms2messaging-1954190.html

[4] http://docs.oracle.com/cd/E12839_01/web.1111/e13814/jmstuning.htm#PERFM308

[5] http://docs.oracle.com/javaee/5/api/javax/jms/Message.html

[6] https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.3/pdf/Development_Guide/JBoss_Enterprise_Application_Platform-6.3-Development_Guide-en-US.pdf

[7] http://www.precisejava.com/javaperf/j2ee/JMS.htm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s