一,背景

近期需要用到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

Java Message Service学习(一)的更多相关文章

  1. 认识 Java Message Service

    1. Java Message Service : 是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建.发送.接收和读取消息. 实现Java 程序与MQ Server 之间互相 ...

  2. Jmeter 测试 JMS (Java Message Service)/ActiveMQ 性能

    前言 JMS介绍:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...

  3. Java Message Service

    en.wikipedia.org/wiki/Java_Message_Service Messaging is a form of loosely coupled distributed commun ...

  4. Simple guide to Java Message Service (JMS) using ActiveMQ

    JMS let’s you send messages containing for example a String, array of bytes or a serializable Java o ...

  5. JMS Java消息服务(Java Message Service)

    JMS 在一些场景下RPC的同步方式可能不太适合业务逻辑的处理,并且这种方式在某些场景下会导致业务的紧耦合. 基于异步交互模型的JMS解决了RPC产生的紧耦合问题,它提供了一个可以通过网络访问的抽象消 ...

  6. Java Web Service 学习笔记

    一.服务端 1. 创建Java工程 2. 创建接口HostipalServiceInterface package ws_server; import javax.jws.WebMethod; imp ...

  7. Java Web Service 学习

    参考原文: http://blog.csdn.net/ostrichmyself/article/details/6393627 http://www.cnblogs.com/Jessy/p/3528 ...

  8. 17) JMS: java Message Service(Java消息服务)

         JMS是一个标准,就像EJB,有很多开源的,商业的实现,ms技术对应的规范是jsr914,规范的实现称为jms provider,常见的实现有ActiveMQ.JBoss MQ.IBM We ...

  9. Spring的消息 Java Message Service (JMS)

     Spring有两种方法提供对EJB的支持: Spring能让你在Spring的配置文件里,把EJB作为Bean来声明.这样,把EJB引用置入到其他Bean的属性里就成为可能了,好像EJB就是另一个P ...

随机推荐

  1. 【翻译】Brewer's CAP Theorem CAP定理

    Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...

  2. B1048 数字加密

    15/20 #include<bits/stdc++.h> using namespace std; stack<int> s; char a[3]={'J','Q','K'} ...

  3. 桌面输入法评测报告 之 搜狗拼音输入法vs必应拼音输入法

    输入法是电脑用户不可或缺的软件,它几乎无时无刻不陪伴在使用者的身旁.一个优秀的输入法,应该满足客户对使用体验以及效率的需求.我们小队的任务便是对当今最为常用的两种输入法: 搜狗拼音输入法和必应拼音输入 ...

  4. 2-Twelfth Scrum Meeting20151212

    任务安排 成员 今日完成 明日任务 闫昊 获取视频播放的进度  获取视频播放进度 唐彬 解决handler可能引起的内存泄露问题  阅读IOS代码+阅读上届网络核心代码 史烨轩 下载service开发 ...

  5. 【Beta阶段】第四次Scrum Meeting!

    每日任务内容: 本次会议为第四次Scrum Meeting会议~ 由于本次会议女生今日因身体不适未参与会议,故在宿舍楼开会,大家集会15分钟. 队员 昨日完成任务 明日要完成任务 刘乾 #130 学习 ...

  6. centos 64位 下hadoop-2.7.2 下编译

    centos 64位 下hadoop-2.7.2 下编译 由于机器安装的是centos 6.7 64位 系统  从hadoop中下载是32位  hadoop 依赖的的库是libhadoop.so 是3 ...

  7. BaseServlet 继承 httpServlet

    BaseServlet   核心 package cn.core; import java.io.IOException; import java.lang.reflect.Method; impor ...

  8. IOS的开发演变历史

    对IOS开发平台一直抱有很大兴趣,正好通过这个机会好好了解一下IOS的开发历程! 通过一些查阅,我了解到IOS的开发平台主要是依靠Xcode软件来编写程序,同时又需要在MAC OS X的环境下运行,w ...

  9. Daily Scrum - 11/17

    今天小组例会内容较少.拜重阳将一个简易的UI设计好push上TFS了,其他人没有太多进展.在原有项目基础上继续开发看似工作量变少,其实开始需要弄清楚原先代码的实现架构和各种借口还是比较困难的.

  10. Objective-C语言--self和super关键字解析

    看代码: @implementation Son : Father - (id)init{ self = [super init]; if (self){ } return self; } self是 ...