JMS
1 Java Message Service(JMS)
- JMS API是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建、发送、接收和读取消息。它使分布式通信耦合度更低,消息服务更加可靠以及异步性。
JMS (Java Message Service) is an API that provides the facility to create, send and read messages. It provides loosely coupled, reliable and asynchronous communication.
- JMS主要用于应用程序之间的通信。
JMS is mainly used to send and receive message from one application to another.
2 JMS程序组成
- 2.1 JMS提供程序:实现了JMS规范的消息传递系统。
A JMS provider: A messaging system that implements the JMS specification.
- 2.2 JMS客户端:发送和接收消息的Java应用程序。
JMS clients: Java applications that send and receive messages.
- 2.3 消息:用于在JMS客户端之间通信的对象。
Messages: Objects that are used to communicate information between JMS clients.
- 2.4 管理对象:管理员为使用JMS客户端而创建的预定义JMS对象。
Administered objects: Preconfigured JMS objects that are created by an administrator for the use of JMS clients.
3 JMS优点
- 3.1 异步:客户端不需要发送请求来接收消息,消息将自动到达客户端。
Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the client.
- 3.2 可靠:在消息传递过程中提供了保障。
Reliable: It provides assurance that message is delivered.
4 JMS消息模型
-
4.1 点对点模型(队列目的地):在此模型中,消息从生产者传递给一个消费者。
消息将传递到目的地(即队列),然后传递给队列中一个注册的消费者。
虽然任何数量的生产者都可以向队列发送消息,但每个消息都保证被传递,并由一个消费者使用。
如果注册的消费者没有使用消息,则队列会保留消息,直到消费者注册消费它们为止。Point-to-Point (Queue destination): In this model, a message is delivered from a producer to one consumer.
The messages are delivered to the destination, which is a queue, and then delivered to one of the consumers registered for the queue.
While any number of producers can send messages to the queue, each message is guaranteed to be delivered, and consumed by one consumer.
If no consumers are registered to consume the messages, the queue holds them until a consumer registers to consume them.- 4.1.1 P2P模式图
- 4.1.2 涉及到的概念
- 消息队列(Queue)
- 发送者(Sender)
- 接收者(Receiver)
- 每个消息都被发送到一个特定的队列,接收者从队列中获取消息。队列保留着消息,直到他们被消费或超时
- 4.1.3 P2P的特点
- 每个消息只有一个消费者(Consumer)(即一旦被消费,消息就不再在消息队列中)
- 发送者和接收者之间在时间上没有依赖性,也就是说当发送者发送了消息之后,不管接收者有没有正在运行,它不会影响到消息被发送到队列
- 接收者在成功接收消息之后需向队列应答成功
- 4.1.1 P2P模式图
-
4.2 发布/订阅(主题目的地):在此模型中,消息从生产者传递给任意数量的消费者。
消息将传递到主题目标,然后传递给已订阅该主题的所有活动使用者。
此外,任何数量的生产者都可以向主题目的地发送消息,并且每个消息可以被传递给任意数量的订户。
如果没有注册消费者,则主题目标不会保留消息,除非它对非活动消费者具有持久订阅。
持久订阅表示在主题目标中注册的使用者,该消费者在将消息发送到主题时可以是不活动的。Publish/Subscribe (