Java Message Service学习(一)

简介:

一,背景

近期需要用到ActiveMQ接收Oozie执行作业之后的返回结果。Oozie作为消息的生产者,将消息发送给ActiveMQ,然后Client可以异步去ActiveMQ取消息。

ActiveMQ作为基于 JMS 开源的Apache Message Provider,故记录下JMS相关基础知识。

 

二,基础知识&基本概念

1)面向消息的中间件

Message-oriented middleware (MOM) is best described as a category of software for communication in an loosely-coupled, reliable, scalable and secure manner amongst distributed applications or systems
面向消息的中间件主要就是为了降低各个应用程序之间的耦合。

以前的软件之间的通信如下:

都是各个终端(应用程序)直接与主机通信(mainframe)。

 

而MOM的理念则是,在消息发送者与消息的接收者之间有一个message mediator 。This mediation provides a whole new level of loose coupling for enterprise messaging.

 

引入MOM的好处:

①This is the mechanism that allows for loose coupling between senders and receivers as there is no requirement for each to be connected to the MOM for sending and receiving messages.

可以做到 不必要求发送者和接收者同时“在线”(actived)才能进行消息通信。发送者只管把消息发给MOM,然后可以“离开”,接收者可以在随后任何时间去取(取决于何种通信模式)

即,发送者不需要知道接收者的存在,且可进行异步通信。

②MOMs added many additional features to enterprise messaging that weren't previously possible when systems were tightly coupled.

MOM还可以提供一些额外的功能:比如,消息的持久化(message persistence)、消息的转化、消息之间的路由……而这些在未引入MOM系统中很难实现的。

③ MOMs on the market today provide support for a diverse set of protocols for connectivity

支持各种各样的连接协议,即Client可以通过如HTTP/TCP....协议连接MOM。

④Furthermore, it's typical for a MOM to provide an API for sending and receiving messages and otherwise interacting with the MOM.

提供了相应的API发送及接收消息。

举个不恰当的例子来说:对于用户登陆(QQ或者淘宝的登陆),用户登陆后需要获得一系列的状态信息(好友动态、离线消息、订阅公众号的消息...)。这些信息都是由不同的系统(好友动态系统、离线消息系统、订阅公众号消息系统...)产生的 ,那么登陆系统需要直接与这些系统交互,即通过“服务调用”的方式让其他系统感知到登陆事件发生了。如果还需要其他额外的功能,比如登陆之后向用户发送短信、为了验证安全向 安全验证系统 发送登陆的时间及IP地址……这意味着登陆系统会变得异常复杂。

于是,就可以用下面讲到的“发布订阅”模型来实现系统之间解耦。如图Figure2.7,Producer是登陆系统,用户登陆后产生Topic,每个Subscriber都是依赖登陆事件的各种其他系统。

 

2)为什么有JMS?

1)中介绍了MOM的好处,正因为现实中存在许许多多的MOM厂商,他们开发出不同的MOM产品,如Apache的ActiveMQ,如WebSphereMQ....每家的MOM产品都有一套自己的发送、接收消息的API,因此,不同产品之间兼容性,和操作的统一性就出了问题。从而就出现了JMS。

The Java Message Service (JMS) moved beyond vender-centric MOM APIs to provide an API for enterprise messaging. JMS aims to provide a standardized API to send and receive messages using the Java programming language in a vendor-neutral manner.

The JMS API lowers the barrier to the creation of enterprise messaging applications among different JMS providers. It also eases the portability to other JMS providers.

从上图看出:JMS 客户端通过JMS 规定的API 访问 各种各样的基于JMS协议的消息中间件产品。这与Client 访问 数据库的方式十分相似。

每一家JMS都有自己的产品,如Apache ActiveMQ,每家公司都遵守JMS协议来为自己的产品开发出接口。而用户只需要与统一的JMS API打交道。

在数据库领域,有MySQL、Oracle、SQLSERVER……,但用户程序只需要通过JDBC就可以来访问各种数据库。

JMS clients utilize the JMS API for interacting with the JMS provider. Similar in concept to the using the JDBC API to access data in relational databases, JMS clients use the JMS API for standardized access to the messaging service.

 

关于JMS的一些基本概念

JMS Producer - A client application that creates and sends JMS messages.

JMS Consumer - A client application that receives and processes JMS messages.

JMS Domains - The two styles of messaging that include point-to-point and publish/subscribe.---点对点模型和发布订阅模型

JMS Provider - The implementation of the JMS interfaces which is ideally written in 100% pure Java.相当于各个开发出JMS产品的厂商

 

Administered Objects - Preconfigured JMS objects that contain provider-specific configuration data for use by clients. These objects are typically accessible by clients via JNDI.

    Connection Factory - Clients use a connection factory to create connections to the JMS provider

    Destination - An object to which messages are addressed and sent and from which messages are received.

JMS Message

一个JMS消息由三部分组成:Headers、Properties、Payload

Headers包含该消息的属性:如,消息要发送到哪里去?由JMSDestination字段来表示;

消息的传递模式,由 JMSDeliveryMode表示,传输模式有两种:

1)Persistent:Advises the JMS provider to persist the message so it's not lost if the provider fails. A JMS provider must deliver a persistent message once-and-only-once. In other words, if the JMS provider fails, the message will not be lost and will not be delivered more than once.

在该模式下,provider宕机了,消息不会丢失,且消息只会传递一次。

2)Non-Persistent:Instructs the JMS provider not to persist the message. A JMS provider must deliver a non-persistent message at-most-once. In other words, if the JMS provider fails, the message may be lost, but it will not be delivered twice.

......//还有许多其他的头部属性

 

Properties与Headers有点类似。

Payload:存储JMS实际消息的地方。可以以text形式、二进制形式存储消息。

 

JMS Selector

Consider the fact that there are times when a JMS client is subscribed to a given destination, but it may want to filter the types of messages it receives. This is exactly where headers and properties can be used.

Message selectors allow a JMS client to specify which messages it wants to receive from a destination based on values in message headers.

Message Selector允许用户只接收自己感兴趣的消息。

 

JMS Domain---消息传输模型

The point-to-point (PTP) messaging domain uses destinations known as queues.

点对点模型传输的目的地是队列。

Each message received on the queue is delivered to once-and-only-once consumer.

点对点嘛,消息只能发送给唯一的一个consumer。

 Consumers receive messages from the queue either synchronously using the MessageConsumer.receive() method or asynchronously by registering a MessageListener implementation using the MessageConsumer.setMessageListener() method.

它支持同步通信和异步通信,同步通信使用MessageConsumer.receive()来接收消息。异步通信需要MessageListener监听器的支持。

 

发布-订阅模型

The publish/subscribe (pub/sub) messaging domain uses destinations known as topics

发布-订阅模型传输的目的地是Topics。

Any messages sent to the topic are delivered to all subscribers via a push model, as messages are automatically delivered to the subscriber.

JMS采用Push方式,即主动地把消息推送给订阅者。

 

发布-订阅模型中有两种订阅方式,一种是持久订阅(Durable subscriptions),另一种是非持久订阅。

非持久订阅:只有当 Client与JMS Provider(如,ActiveMQ)保持连接状态才能收到发送到某个Topic的消息。若Client处于离线,这个时间段发送到Topic的消息会丢失。

持久订阅:Using a durable subscription, when a subscriber disconnects from the JMS provider, it is the responsibility of the JMS provider to store messages for the subscriber

更详细的解释参考:

Message Durability 与 Message Persistence 的区别

Message Durability针对 Pub/Sub Domain而言的,它是指接收者以何种方式去接收消息,如果采用非持久订阅,接收者在消息发布到消息服务器的时候 没有连接到消息服务器,则它将收不到这个消息。

而Message Persistence与Domain无关,点对点模型中也存在Message Persistence问题。因为它是针对消息服务器而言的,描述的是消息的可靠性,即当消息服务器宕机后,消息是否丢失。

Message persistence is independent of the message domain. It is used to indicate the JMS application's ability to handle missing messages in the event of a JMS provider failure.

this quality of service is specified on the producer using the JMSDeliveryMode property using either the persistent or non-persistent property.

 

参考书籍:《ActiveMQ in Action》第二章

参考链接:理解JMS规范中消息的传输模式和消息持久化

 JMS学习(二)之ActiveMQ

JMS学习(三)ActiveMQ Message Persistence

ActiveMQ Transport Connectors

本文转自hapjin博客园博客,原文链接:http://www.cnblogs.com/hapjin/p/5431706.html,如需转载请自行联系原作者


相关实践学习
通过轻量消息队列(原MNS)主题HTTP订阅+ARMS实现自定义数据多渠道告警
本场景将自定义告警信息同时分发至多个通知渠道的需求,例如短信、电子邮件及钉钉群组等。通过采用轻量消息队列(原 MNS)的主题模型的HTTP订阅方式,并结合应用实时监控服务提供的自定义集成能力,使得您能够以简便的配置方式实现上述多渠道同步通知的功能。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
4月前
|
IDE Java 编译器
java编程最基础学习
Java入门需掌握:环境搭建、基础语法、面向对象、数组集合与异常处理。通过实践编写简单程序,逐步深入学习,打牢编程基础。
294 1
|
5月前
|
Java API 容器
Java基础学习day08-2
本节讲解Java方法引用与常用API,包括静态、实例、特定类型方法及构造器引用的格式与使用场景,并结合代码示例深入解析。同时介绍String和ArrayList的核心方法及其实际应用。
178 1
|
4月前
|
存储 Oracle Java
java零基础学习者入门课程
本课程为Java零基础入门教程,涵盖环境搭建、变量、运算符、条件循环、数组及面向对象基础,每讲配示例代码与实践建议,助你循序渐进掌握核心知识,轻松迈入Java编程世界。
422 0
|
4月前
|
负载均衡 Java API
grpc-java 架构学习指南
本指南系统解析 grpc-java 架构,涵盖分层设计、核心流程与源码结构,结合实战路径与调试技巧,助你从入门到精通,掌握高性能 RPC 开发精髓。
489 7
|
5月前
|
Java
Java基础学习day08-作业
本作业涵盖Java中Lambda表达式的应用,包括Runnable与Comparator接口的简化实现、自定义函数式接口NumberProcessor进行加减乘及最大值操作,以及通过IntProcessor处理整数数组,实现遍历、平方和奇偶判断等功能,强化函数式编程实践。
97 5
|
5月前
|
Java 程序员
Java基础学习day08
本节讲解Java中的代码块(静态与实例)及其作用,深入介绍内部类(成员、静态、局部及匿名)的定义与使用,并引入函数式编程思想,重点阐述Lambda表达式及其在简化匿名内部类中的应用。
175 5
|
5月前
|
Java
Java基础学习day07-作业
本作业包含六个Java编程案例:1)动物类继承与多态;2)加油卡支付系统;3)员工管理类设计;4)学生信息统计接口;5)USB设备控制;6)家电智能控制。综合运用抽象类、接口、继承、多态等面向对象技术,强化Java基础编程能力。
219 3
|
5月前
|
Java
Java基础学习day06-作业
本内容为Java基础学习作业,涵盖两个案例:一是通过Card类及其子类GoldenCard、SilverCard实现加油卡系统,体现封装与继承;二是通过Shape类及子类Circle、Rectangle演示多态与方法重写,强化面向对象编程理解。
113 1
|
5月前
|
设计模式 存储 Java
Java基础学习day07
本节讲解Java中的final关键字、单例设计模式、枚举类、抽象类与接口。涵盖常量定义、单例写法(饿汉式/懒汉式)、枚举特点及应用场景,以及抽象类与接口的使用与区别,助力掌握核心面向对象编程思想。
198 1
|
5月前
|
算法 Java
Java基础学习day03-作业
本内容包含多个Java编程案例,涵盖条件判断、循环、数组、随机数生成、素数判断等基础算法练习,适用于巩固Java语法与逻辑思维训练。
193 6