关于消息的定义很多,个人认为在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协议的通讯方案,都具有高度紧密的耦合性,面向消息技术就是为了解决紧耦合通讯. 通过消息队列机制,软件组件之间可以通过非直连而采用代理中间件的方式进行信息通讯. 从而实现消息发送者与接收者之间的解耦.
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 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信息中间件的一个适配(中转)程序.
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 defines a set of message interfaces.Clients use the message implementations supplied by their JMS provider.
JMS Message 定义了一组接口,用来统一JMS Clients之间传输消息的标准. JMS Message Model由以下三个部分组成:
- 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.
- Properties – In addition to the standard header fields, messages provide a built-in facility for adding optional header fields to a message.
- Body – JMS defines several types of message body which cover the majority of messaging styles currently in use.
- StreamMessage – JAVA中流对象
- MapMessage – JAVA中util包下MAP对象
- TextMessage – String对象
- ObjectMessage – 可Serializable对象
- BytesMessage – 字节数组
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
联系客服