Browse DevX
Sign up for e-mail newsletters from DevX


Advanced JMS Messaging with OpenJMS : Page 2

Learn to use the more advanced features of OpenJMS to filter messages, create clients that can receive messages sent even when they aren't running, and create persistent messages that can survive a provider failure.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using Message Selectors
Here's an example that incorporates message properties, a message priority, and message selectors. Here's the scenario. A producer application publishes messages to a topic (see Figure 1). The messages have different JMS priorities and different message properties. One consumer application for this topic is finicky; it specifies to the JMS provider that it wants to filter the messages it will receive by using a message selector.

This example uses OpenJMS, an open source implementation of Sun Microsystem's Java Message Service API 1.0.2 specification as the JMS provider. You can learn more about the OpenJMS server at http://www.devx.com/Java/Article/20903.

The code fragment shown below (see the file PublishToTopic.java in the downloadable sample code for the full listing) is from the producer application.

TextMessage messageOne = topicSession.createTextMessage(); message = "Message #1 - Sport: Basketball " + hour12 + ":" + min + ":" + sec; messageOne.setText(message); messageOne.setStringProperty("Sport", "Basketball"); topicPublisher.publish(messageOne, DeliveryMode.PERSISTENT, 9, 999999);

Figure 1: The message producer publishes messages to the topic with different "Sport" property values.
Note that you need to have the consumer (which we'll cover in just a bit) running before you run PublishToTopic. You can mitigate this timing dependency with durable subscriptions which I'll cover later in this article.

The PublishToTopic application sends four different messages to the topic named "topic1." The publisher sets the "Sport" property to different values for different messages and also sets different priorities for each message using the publish method.

void publish(Message message, int deliveryMode, int priority, long timeToLive)

Using the publish method, you can specify a JMSPriority (from 0-9, with 9 being the highest priority and 4 being the default). It's important to note that if you use the setPriority method of the MessageProducer interface, you set the priority level of all the messages sent by the producer; whereas, by default, if you do not set a priority level, the default level is 4. To set the priority of a particular message, use the publish(Message, int, int , long) method. The publish method also lets you specify a delivery mode and a "time to live" for the message. By default, a message never expires. If a message being sent will become obsolete after a certain period of time, you can specify its time to live via the fourth argument of the publish method. You specify the time to live in milliseconds. The delivery mode can be either the value deliveryMode.PERSISTENT or deliveryMode.NON_PERSISTENT.

The consumer application, ConsumerUsingFilters.java (a section of which is shown below) listens asynchronously to the topic. You can see that the subscriber filters its consumption of messages via the SQL92 string: "Sport in ('Basketball','Football')"

// create a topic subscriber and associate // a filtering criterion String filterQuery = "Sport in ('Basketball','Football')"; topicSubscriber = topicSession.createSubscriber(topic, filterQuery, false);

The code shown above states that the client only wants messages where the property "Sport" is set to either "Basketball" or "Football." Messages having the property set to "Hockey" and "Baseball" will not be consumed because they will be filtered out by the subscribing application. This can be seen in Figure 2, which shows the consumer application running:

Figure 2: The Consumer application selectively filters messages based on the value of the "Sport" property.
You can also filter using message priority. For example, you can change the filter query to:

String filterQuery = "JMSPriority > 6"; topicSubscriber = topicSession.createSubscriber(topic, filterQuery, false);

Now when you run the client application, Figure 3 shows that the application consumes only messages with a JMSPriority higher than 6. Everything else was filtered.

Figure 3: The modified consumer application filters messages based on the JMSPriority.

Thanks for your registration, follow us on our social networks to keep up-to-date