`

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.
分享到:
评论

相关推荐

    铅笔头识别数据集,1692张原始训练图,640*640分辨率,91.1%的正确识别率,标注支持coco json格式

    铅笔头识别数据集,1692张原始训练图,640*640分辨率,91.1%的正确识别率,标注支持coco json格式

    高校网络教学的体系规划与创建.docx

    高校网络教学的体系规划与创建.docx

    SpringBoot的学生心理咨询评估系统,你看这篇就够了(附源码)

    SpringBoot的学生心理咨询评估系统,你看这篇就够了(附源码)

    遗传算法优化BP神经网络提升交通流量预测精度的技术实现与应用

    内容概要:本文详细介绍了如何使用遗传算法优化BP神经网络,以提高交通流量预测的准确性。文中首先解释了BP神经网络的基本结构及其局限性,即容易陷入局部最优解的问题。随后,作者展示了遗传算法的工作原理,并将其应用于优化BP神经网络的权重和偏置。通过定义适应度函数、选择、交叉和变异等步骤,实现了对BP神经网络的有效改进。实验结果显示,优化后的BP神经网络在交通流量预测中的精度显著高于传统的BP神经网络,特别是在处理复杂的非线性问题时表现出色。 适用人群:对机器学习、深度学习以及交通流量预测感兴趣的科研人员和技术开发者。 使用场景及目标:适用于需要进行精确交通流量预测的应用场景,如智能交通系统、城市规划等领域。主要目标是通过遗传算法优化BP神经网络,解决其易陷入局部最优的问题,从而提高预测精度和稳定性。 其他说明:文中提供了详细的Python代码实现,帮助读者更好地理解和实践这一优化方法。同时,强调了遗传算法在全局搜索方面的优势,以及其与BP神经网络结合所带来的性能提升。此外,还讨论了一些具体的实施技巧,如适应度函数的设计、交叉和变异操作的选择等。 标签1,标签2,标签3,标签4,标签5

    H5U PLC与触摸屏集成框架:总线伺服控制及跨平台移植的最佳实践

    内容概要:本文详细介绍了H5U框架在PLC与触摸屏集成方面的应用,特别是在总线伺服控制和跨平台移植方面。文章首先解析了伺服控制的核心代码,如使能模块和绝对定位指令,强调了标准化控制流程的优势。接着讨论了触摸屏交互,通过直接映射PLC的DB块地址简化了数据处理。然后介绍了总线配置,尤其是EtherCAT总线初始化及其容错设计。此外,文章还探讨了框架的移植性和报警处理设计,展示了其在不同PLC品牌间的易用性和高效的故障恢复能力。 适合人群:从事工业自动化领域的工程师和技术人员,特别是有PLC编程经验和需要进行伺服控制系统开发的人群。 使用场景及目标:①快速搭建和调试基于PLC和触摸屏的自动化控制系统;②提高多轴设备的调试效率;③实现跨平台的无缝移植;④优化报警管理和故障恢复机制。 其他说明:该框架不仅提供了详细的代码示例和注释,还包含了丰富的实战经验和最佳实践,使得新手能够快速上手,而资深工程师可以在此基础上进一步创新。

    游戏开发UE5引擎核心技术解析与应用:涵盖安装配置、项目创建及蓝图编辑器详解文档的主要内容

    内容概要:本文档《UE5开发.txt》全面介绍了Unreal Engine 5(UE5)的基本概念、安装配置、项目创建、文件结构及常用功能。UE5是一款强大的游戏引擎,支持实时渲染、蓝图创作、C++编程等功能。文档详细描述了UE5的安装步骤,包括硬件要求和环境配置;项目创建过程,涵盖项目模板选择、质量预设、光线追踪等设置;文件结构解析,重点介绍了Config、Content和.uproject文件的重要性。此外,文档深入讲解了蓝图编辑器的使用,包括变量、数组、集合、字典等数据类型的操作,以及事件、函数、宏和事件分发器的应用。蓝图作为一种可视化脚本工具,使开发者无需编写C++代码即可快速创建逻辑,适用于快速开发和迭代。 适合人群:具备一定编程基础的游戏开发者、设计师和对游戏开发感兴趣的初学者,尤其是希望深入了解UE5引擎及其蓝图系

    餐馆点菜系统概要设计说明书.doc

    餐馆点菜系统概要设计说明书.doc

    5+1档轿车手动变速箱设计说明书.doc

    5+1档轿车手动变速箱设计说明书.doc

    1万吨自来水厂详细设计说明书.doc

    1万吨自来水厂详细设计说明书.doc

    wordpress外贸电商企业产品主题

    wordpress外贸电商企业产品主题 页面展示图https://i-blink.csdnimg.cn/direct/e45b2e2e8e27423eb79bda5f4c1216d7.png

    低效林改造作业设计说明书.doc

    低效林改造作业设计说明书.doc

    西门子200smart编程软件V2.8.2.1

    西门子200smart编程软件V2.8.2.1

    135调速器操纵手柄 设计说明书.doc

    135调速器操纵手柄 设计说明书.doc

    蓝桥杯全国软件和信息技术专业人才竞赛指导文档.pdf

    内容概要:本文档为蓝桥杯全国软件和信息技术专业人才竞赛提供了全面的指导,涵盖竞赛概述、流程与规则、核心考点与备赛策略、实战技巧与避坑指南以及备赛资源推荐。蓝桥杯竞赛由工信部人才交流中心主办,涉及算法设计、软件开发、嵌入式系统、电子设计等领域。文档详细介绍了参赛流程(报名、省赛、国赛、国际赛),并针对软件类和电子类竞赛分别阐述了高频考点和备赛建议。对于软件类,强调了算法与数据结构的重要性,如排序、动态规划、图论等;对于电子类,则侧重于硬件基础和开发工具的使用。此外,还提供了详细的答题策略、常见陷阱规避方法及工具调试技巧。; 适合人群:高校本专科生、研究生,尤其是对算法设计、软件开发、嵌入式系统等领域感兴趣的计算机科学及相关专业的学生。; 使用场景及目标:①帮助参赛选手熟悉竞赛流程和规则,明确各阶段任务;②提供系统的备赛策略,包括高频考点的学习和专项突破;③指导选手掌握实战技巧,避免常见错误,提高答题效率和准确性。; 阅读建议:此文档不仅提供了理论知识,还包含了大量实战经验和备赛资源推荐,建议读者结合自身情况制定个性化的备赛计划,充分利用提供的资源进行练习和准备。

    基于行块抽取正文内容的java版本的改进算法.zip

    基于行块抽取正文内容的java版本的改进算法.zip

    基于S7-200 PLC和MCGS的快递分拣系统设计与实现:硬件配置、梯形图编程及组态应用

    内容概要:本文详细介绍了基于西门子S7-200 PLC和MCGS组态软件的快递分拣系统的设计与实现方法。首先阐述了硬件配置的关键要点,包括IO分配表的具体设置以及传感器和执行机构的连接方式。接着深入解析了PLC程序中的梯形图逻辑,涵盖主传送带的连锁保护、机械臂动作的自保持逻辑和安全复位机制等核心部分。同时探讨了MCGS组态画面的应用,展示了如何通过脚本实现动态效果和数据统计功能。此外,文中还分享了一些调试经验和常见问题的解决方案,如防止传感器抖动、优化数据传输效率等。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是对PLC编程和组态软件有一定了解的人群。 使用场景及目标:适用于需要构建高效可靠的快递分拣系统的物流企业或相关项目开发者。目标是帮助读者掌握从硬件选型到软件编程的一整套实施流程,确保系统能够稳定运行并达到预期性能指标。 其他说明:文章不仅提供了理论指导,还结合实际案例进行了详细的步骤讲解,有助于读者更好地理解和应用于实践中。

    joblib-0.12.5-py2.py3-none-any.whl

    该资源为joblib-0.12.5-py2.py3-none-any.whl,欢迎下载使用哦!

    机器学习分类算法实战:基于sklearn的决策树、随机森林与KNN Python实现

    内容概要:本文详细介绍了三种经典的机器学习分类算法——决策树、随机森林和KNN分类器,在Python的sklearn库中的具体实现方法。首先,通过加载鸢尾花数据集进行数据准备,并将其划分为训练集和测试集。接着分别实现了决策树、随机森林和KNN分类器,展示了每种算法的关键参数配置及其对模型性能的影响。对于决策树,重点讨论了max_depth参数的作用以及如何通过可视化工具理解其分裂过程;随机森林部分强调了n_estimators参数的选择和特征重要性的评估;而KNN分类器则着重于特征标准化的重要性和n_neighbors参数的优化。此外,文中还提供了关于模型选择的指导,帮助读者根据不同应用场景选择合适的算法。 适合人群:对机器学习感兴趣的初学者和有一定编程基础的研发人员。 使用场景及目标:①理解并掌握决策树、随机森林和KNN分类器的工作原理;②学会使用sklearn库快速构建和评估分类模型;③能够根据具体问题特点选择最适合的分类算法。 其他说明:本文不仅提供了详细的代码示例,还分享了许多实践经验,如参数调优技巧、模型评估方法等,有助于读者更好地理解和应用这些算法。

    带式输送机传动装置设计课程设计说明书.doc

    带式输送机传动装置设计课程设计说明书.doc

    gh毕业论文 伦潭水利枢纽工程土石坝设计说明书.doc

    gh毕业论文 伦潭水利枢纽工程土石坝设计说明书.doc

Global site tag (gtag.js) - Google Analytics