JMS 2.0 Message Consumer Features

JMS 2.0 comes with a new API (simplified API) and few extra features. Following are the two main features for message consuming clients.

  • Shared Topic Subscription
  • JMSXDeliveryCount

Shared Topic Subscription

Introduction

JMS supports two message communication models, point-to-point and publish/subscribe. In point-to-point model a data structure abstraction called Queue is used, while in publish/subscribe Topics are used.

Whenever a message consuming client wants to consume messages from a topic a “subscription” should be created. A subscription will receive a copy of every message (a filer can be set, message selector) sent to the Topic and then the subscription will forward message its consumers. Each copy of the original message will be independent from each other.

According to the requirements there are different types of subscriptions are defined, such as durable/non-durable and shared/unshared. After combining all those together, there are four subscription types.

  • Unshared non-durable subscription
  • Shared non-durable subscription
  • Unshared durable subscription
  • Shared durable subscription

Shared subscription was introduced in JMS 2.0, before that the both subscription types of durable and non-durable were unshared.

How it Works

Shared subscription was introduced to increase the scalability of Java applications. A shared subscription will forward the copy of the original message that it receives from the topic to only one message consuming client. The method of message distribution (how the messages are distributed among shared consumers) is not defined by the JMS API therefor it should be handled by the JMS Provider.

Shared Topic Subscription

Comparison

Durable, non-durable, shared and non-shared

Sample Tryout

Following is a simple example of Shared Topic Subscription on JMS 2.0 supported broker (HornetQ 2.4.0).

If you need to run this, please use this hornetq-jms.xml configuration file on <HornetQ_HOME>/config/stand-alone/non-clustered/ directory.

And use these two jars in the classpath of the Java source code. jms-2.0-api.jar and hornetq-all.jar

JMSXDeliveryCount

Introduction

In JMS messages there is a property defined by the JMS-API called JMSXDeliveryCount. The purpose of it is to maintain a counter to indicate the number of times a message has been (re)delivered to a particular JMS consumer client. Eventhough this property existed in versions prior to JMS 2.0, setting it was not mandatory for the JMS providers. But from JMS 2.0 it has been mentioned as a mandatory property.

If the message was received on the first attempt the JMSXDeliveryCount value will be 1 and it will be 2 or more if the message happened to be redelivered.

Sample Tryout

After receiving the TextMessage, JMSXDeliveryCount value can be retrieved as follows,

TextMessage messageReceived = (TextMessage)messageConsumer.receive();
int deliveryCount = messageReceived.getIntProperty("JMSXDeliveryCount");