JMS ActiveMQ Spring

来源:百度文库 编辑:神马文学网 时间:2024/04/29 07:17:16


1. 消息与JMS

纸上得来终觉浅,绝知此事要躬行. — 陆游

1.1 什么是消息?

关于消息的定义很多,个人认为在wikipedia上的定义很好,原文如下:

Messaging is a form of loosely coupled distributed communication, where in this context the term ‘communication’ can be understood as an exchange of messages between software components. Message-oriented technologies attempt to relax tightly coupled communication (such as TCP network sockets, CORBA or RMI) by the introduction of an intermediary component, which in this case would be a queue. The latter approach allows software components to communicate ‘indirectly’ with each other. Benefits of this include message senders not needing to have precise knowledge of their receivers, since communication is performed using the queue.

消息是一种松散耦合的分布式通讯方案,这里所说的通讯方案可以理解为软件组件之间 的信息交换. 基于TCP协议的通讯方案,都具有高度紧密的耦合性,面向消息技术就是为了解决紧耦合通讯. 通过消息队列机制,软件组件之间可以通过非直连而采用代理中间件的方式进行信息通讯. 从而实现消息发送者与接收者之间的解耦.

1.2 什么是JMS?

JMS定义,摘录自SUN JMS Spec 1.1

JMS provides a common way for Java programs to create, send, receive and read an enterprise messaging system’s messages.

任何俩个消息发送者和接收者之间都要遵循以下方式来实现消息通讯

消息创建  ->  消息发送  ->消息接收  ->  消息处理

JMS为JAVA程序提供了实现这种消息通讯方式的通用标准.

JMS Provider (JMS提供者)

JMS provider is the entity that implements JMS for a messaging product.

An implementation of the JMS interface for a Message Oriented Middleware (MOM). Providers are implemented as either a Java JMS implementation or an adapter to a non-Java MOM.

Provider是java程序用来访问消息中间件的一段程序,它既可以是通过java来编写的一个JMS应用实现,也可以是一个用来访问非java信息中间件的一个适配(中转)程序.

JMS Client (JMS提供者)

An application or process that produces and/or receives messages.
JMS producer
A JMS client that creates and sends messages.
JMS consumer
A JMS client that receives messages.

JMS 定义了俩种类型的客户类型应用程序,消息的生产者与消费者

JMS Message (JMS消息)

JMS defines a set of message interfaces.Clients use the message implementations supplied by their JMS provider.

JMS Message 定义了一组接口,用来统一JMS Clients之间传输消息的标准. JMS Message Model由以下三个部分组成:

  1. Header – All messages support the same set of header fields. Header fields contain values used by both clients and providers to identify and route messages.
  2. Properties – In addition to the standard header fields, messages provide a built-in facility for adding optional header fields to a message.
  3. Body – JMS defines several types of message body which cover the majority of messaging styles currently in use.
  1. 头信息 – 所有JMS消息都包含相同的头信息,头信息是由一组特定名称的字段组成,这些字段以及其包含的信息分别供JMS Client和Provider使用.
  2. 属性 – 除头信息外额外的属性设置,开发人员可以通过设置属性来为消息增加额外的信息
  3. 消息体 – 消息内容,JMS定义了多种格式来记载消息内容,目前JMS1.1定义了以下5种格式:
    • StreamMessage – JAVA中流对象
    • MapMessage – JAVA中util包下MAP对象
    • TextMessage – String对象
    • ObjectMessage – 可Serializable对象
    • BytesMessage – 字节数组

JMS Models (JMS模型)

The JMS API supports two models:

  • point-to-point or queuing model
  • publish and subscribe model

JMS目前支持两种模式:

点对点(队列)模式

In the point-to-point or queuing model, a sender posts messages to a particular queue and a receiver reads messages from the queue. Here, the sender knows the destination of the message and posts the message directly to the receiver’s queue. It is characterized by the following:

  • Only one consumer gets the message
  • The producer does not have to be running at the time the consumer consumes the message, nor does the consumer need to be running at the time the message is sent
  • Every message successfully processed is acknowledged by the consumer

在队列模式中,当一个发送者将消息发送到某个特定队列后,另一个接受者会通过这个特定队列读取消息。这种情况下,发送者要知晓消息的目的地从而将消息直接发送到接收者的队列中,可以概括为:

  • 只有一个接收者来处理消息.
  • 在这种模式下,发送者/接收者均可以离线而不影响对方发送/接收消息.
  • 接收者会签收所有成功的消息.

发布者/订阅者模式

The publish/subscribe model supports publishing messages to a particular message topic. Subscribers may register interest in receiving messages on a particular message topic. In this model, neither the publisher nor the subscriber know about each other. A good metaphor for it is anonymous bulletin board. The following are characteristics of this model:

  • Multiple consumers can get the message
  • There is a timing dependency between publishers and subscribers. The publisher has to create a subscription in order for clients to be able to subscribe. The subscriber has to remain continuously active to receive messages, unless it has established a durable subscription. In that case, messages published while the subscriber is not connected will be redistributed whenever it reconnects.

发布/订阅模式下,JMS应用允许某个发布者发布一个特定的消息主题,订阅者可以从诸多发布者提供的消息主题随意选择订阅. 这种模式下,发布者和订阅者彼此不知道对方的具体情况. 做一个恰当的比喻,发布/订阅模式非常类似于匿名公告板.

  • 消息可以被多个订阅者接收解读处理
  • 在发布这和订阅者之间存在时间依赖性。发布者需要建立一个订阅,以便客户能够购订阅。订阅者必须保持持续的活动状态以接收消息,除非订阅者建立了持久的订阅,在持久订阅的情况下,在订阅者未连接时发布的消息将在订阅者重新连接时重新发布。

参考文献


英文维基,JMS介绍:http://en.wikipedia.org/wiki/Java_Message_Service
JMS 官方Spec: http://java.sun.com/products/jms/docs.html

Tags: JSM ActiveMQ 消息
Category: Java
You can follow any responses to this entry via RSS.
You can leave a comment or trackback from your own site.