`

Advanced JMS Messaging with OpenJMS

阅读更多
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.

by Kulvir Singh Bhogal
n a previous article I covered some of the basics of JMS using OpenJMS as the JMS provider. In this article, you'll see some more advanced OpenJMS topics such as message selectors, durable subscriptions, and message persistence along with code examples that show you how to implement these concepts programmatically.

Message Selectors
Sometimes you don't want message consumers to receive every message sent by a sender or publisher. Instead you want to be able to filter received messages. The JMS API provides this facility through message selectors. The message consumer specifies which messages it is interested in. The JMS provider then selectively sends just those messages to the client. Note that the onus of filtering messages is left up to the JMS provider, not the consuming application. The provider can scrutinize the header values and message properties with message selectors, but unfortunately, OpenJMS cannot analyze the incoming message bodies. I'll show you an example message selector in just a bit, but first here are a couple of topics (no JMS pun intended) to set the stage.

Message Header Fields
When a JMS provider sends a message it generates the following header fields:

    * JMSMessageID—A unique message identifier
    * JMSDestination—The queue or topic to which the message is sent.
    * JMSRedelivered—States whether the message has been resent

The client sending application can set a number of header fields. Refer to the JMS API documentation for a full breakdown of what headers the client can set. Each header field has getter and setter methods. Some of the fields the client can set include:

    * JMSPriority—You can set a message priority from 0 to 9 (9 being the highest priority and 4 being the default). However, note that the there is no guarantee that higher priority messages will be delivered before lower priority ones.
    * JMSType—String identifying contents of a message
    * JMSReplyTo—Where responses should be sent
    * JMSExpiration—An expiration time for the message

Message Properties
While the preceding message header fields are predefined, clients can add more information to JMS messages via the extensibility of properties. These properties are name/value pairs which the client application defines. The values for properties can be of type boolean, byte, short, int, long, float, double, or string (each type has a corresponding Message.setProperty method). For example, using the syntax below, you could set a property named "Sport" to "Basketball"

Message.setStringProperty("Sport","Basketball");
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.
分享到:
评论

相关推荐

    Enterprise Messaging Using JMS and IBM WebSphere

    This book offers a customer-focused perspective on building messaging solutions based on JMS and the leading messaging provider, IBM WebSphere Application Server. The author, Kareem Yusuf, uses his ...

    Reactive Messaging Patterns with the Actor Model

    Reactive Messaging Patterns with the Actor Model: Applications and Integration in Scala and Akka

    Enterprise Messaging Using JMS and IBM® WebSphere®

    This book offers a customer-focused perspective on building messaging solutions based on JMS and the leading messaging provider, IBM WebSphere Application Server. The author, Kareem Yusuf, uses his ...

    Java EE Web Application Primer Building Bullhorn A Messaging App with JSP, epub

    Java EE Web Application Primer Building Bullhorn A Messaging App with JSP, Servlets, JavaScript, Bootstrap and Oracle 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请...

    Instant messaging with voip

    好东西..即时通整合网络电话的详细文档。

    Spring Boot Messaging

    After reading this book, you will come away with a case study application walk-through and will be able to use it as a template for building your own Spring messaging applications or messaging ...

    基于JMS(Java Messaging Service)开发JAVAMAIL详解

    在本文中,我们将深入探讨基于JMS(Java Messaging Service)开发JAVAMAIL的相关知识点。首先,我们需要理解JMS和JAVAMAIL这两个技术在Java开发中的作用。 JMS,全称为Java Messaging Service,是Java平台上的标准...

    Messaging with Rails

    《Messaging with Rails》这篇博客文章深入探讨了在Ruby on Rails框架中实现消息传递系统的相关技术。Rails是一个流行的开源Web应用程序框架,它基于Ruby语言,强调简洁性和生产力。在这个主题中,我们将关注如何在...

    spring-messaging源码

    它为处理消息传递提供了统一的抽象,支持多种消息传递协议,如JMS(Java Message Service)、AMQP(Advanced Message Queuing Protocol)以及STOMP(Simple Text Oriented Messaging Protocol)。通过这个模块,...

    GoldenGate With JMS

    - 进入`Services -> Messaging -> JMS Module -> jmsResources -> ConnectionFactory`。 - 建立子部署(默认名称即可:ODIConnectionFactory)。 4. **配置消息目的地(Destinations)**: - 进入`Services -> ...

    spring-messaging-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-messaging-5.0.8.RELEASE.jar; 赠送原API文档:spring-messaging-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-messaging-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...

    JMS 简介以及Weblogic配置JMS图解

    Java Messaging Service(JMS)是一种标准的API,用于在Java应用程序之间交换消息,特别是在分布式环境中。JMS提供了一种与平台无关的方式,使得应用程序可以与各种消息中间件提供商进行交互,就像JDBC允许Java应用...

    Data Analytics with Spark Using Python

    • Extend, accelerate, and optimize Spark routines with advanced API platform constructs, including shared variables, RDD storage, and partitioning • Efficiently integrate Spark with both SQL and ...

    Informatica Ultra Messaging概述

    Informatica Ultra Messaging支持多种编程语言和消息传递协议,包括Java、JMS、.NET和C等。它支持任何服务质量,包括可靠、有保证和一次且仅一次的消息传递。此外,它还支持多种消息语义,包括发布/订阅、请求/响应...

    flex-messaging系列jar包

    Flex Messaging系列JAR包是Adobe Flex与Java之间进行通信的核心组件,主要负责建立富互联网应用程序(RIA)与服务器之间的消息传递。这些JAR文件包含了多种服务和协议的支持,使得Flex客户端能够与Java后端无缝交互...

    ZeroMQ: Messaging for Many Applications

    Learn how to use specific ØMQ programming techniques, build multithreaded applications, and create your own messaging architectures. You’ll discover how ØMQ works with several programming ...

    英文原版-Instant Messaging in Java 1st Edition

    Instant Messaging has exploded into the online world and is a wildly popular service of the largest Internet Service Providers (ISP) like AOL with AOL Instant Messenger, MSN with Microsoft Messenger,...

Global site tag (gtag.js) - Google Analytics