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 we do not specify any priority level for a message.

In a JMS application, priority can be set in either of the following ways:

1) By using setPriority(int value) method of MessageProducer interface.

2) By using the overloaded publish() method. The third argument  will be the priority.

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.

We 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.

Thanks for reading.


I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *