Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JMS Message Tuning

DZone's Guide to

JMS Message Tuning

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

This is a continuation to my earlier article – Java Message Service(JMS) Explained. Here we will discuss how to tune the message in the producer and consumer.

Let’s take the session creation statement in message producer.

Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

We can send messages in a batch by making the first argument TRUE. In this case we need to explicitly commit after sending the message. Before committing if any exception is thrown, then JMS will roll-back all the messages. Second argument is not valid in message producer, it can be 0.

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

By default all messages send on activemq are persistent. That means if a consumer is not available to pick up the message, then the message will remain in the queue indefinitely. We can set a message non-persistent by setting the delivery mode in the producer.

producer.setPriority(7);
			producer.setTimeToLive(2000);

Also we can set the priority for a message, but there is no guarantee that the message will be received at the consumer based on that priority. Another property is time to live. By setting this property we can tell the queue to destroy the message while the time to live is expired. In this case also there is no guarantee that queue will remove the message as soon as the time is expired.

Now let’s look at session creation in message consumer.

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

We can pass three different parameters into the session creation method.

  • SESSION.AUTO_ACKNOWLEDGE
  • SESSION.CLIENT_ACKNOWLEDGE
  • Session.DUPS_OK

Once the message is received, JMS automatically acknowledge the messages if we enable SESSION.AUTO_ACKNOWLEDGE and the message will be removed from the queue.

In case of Session.CLIENT_ACKNOWLEDGE, we need to manually acknowledge the messages. 

message.acknowledge();

This is applicable only in the case of explicitly receiving the message using consumer.receive() method, not in the case of  listener.

Session.DUPS_OK automatically acknowledge the message. Doesn't have to acknowledge immediately, even though the processing of that message is completed. Improve performance, JMS can batch several messages and acknowledge all together. There is a risk, that if server restart, all the messages that are not acknowledged will be send again.

Instead of false , if we use true then all the messages will be received in a transaction and need to commit(Session.commit()). This is applicable only in the case of consumer.receive() method , not in the case of  listener.

Explicitly receive message without using message listener.

connection.start();
		Message message = consumer.receive();

Using receive() method we can explicitly grab message from the queue. In this case the consumer will wait for a message indefinitely. Also we can pass the time for which the consumer must wait for the message from queue. See code snippet below.

connection.start();
		Message message = consumer.receive(2000);

In this case consumer will wait for 2 sec to get the message from message broker(Queue). If no message is available, it will return null. Need to provide minimum half a second delay.

These are some of the ways to tune the JMS messages in producer and consumer.


Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}