Introduction
In this tutorial I am going to tell you how to set priority of messages in JMS (Java Messaging Service). So it is possible to control the order of the message before sending to a destination by setting a priority level. It forces the JMS provider to deliver high priority messages first.
JMS API has message priority levels from 0 (lowest priority) to 9 (highest priority). The default priority level is 4 if you do not specify any priority level for a message.
How to set Message Priority
You can use message priority levels to instruct the JMS provider to deliver urgent messages first. In a JMS application, priority can be set in either of the following ways:
- By using
setPriority(int value)
method ofMessageProducer
interface. - By using the overloaded
publish()
method. The third argument will be the priority.
The priority using the publish() method can be set in the following way:
topicPublisher.publish(message, DeliveryMode.NON_PERSISTENT, 3, 20000)
ActiveMQ observes three distinct levels of “Priority”:
- Default (JMSPriority == 4)
- High (JMSPriority > 4 && <= 9)
- Low (JMSPriority > 0 && < 4)
JMS Message Expiry
By default a message never expires. In some cases message will become obsolete after a particular time period. In such situation it is desirable to set expiration time. After the message expires, it will not be delivered.
You can set the expiration time in program in two ways:
a) Using setTimeToLive()
method of MessageProducer
interface to set the expiry time of messages from that producer.
Example: setTimeToLive(10000)
The above statement sets the expiry time 10000 milliseconds (10 seconds).
b) Using the overridden method publish()
Example : topPublisher.publish(message, DeliveryMode.PERSISTENT, 3, 10000)
The fourth argument gives the expiry time as 10000 milliseconds.
A JMS provider tries to deliver higher priority messages before lower priority messages but not necessarily to be delivered in exact order of priority.
That’s all about setting message priority in JMS.
need Program , Where we will impliment it.