JMS, the Java Message Service (Java Message Service) application program interface, is an API for message-oriented middleware (MOM) in the Java platform, which is used to send messages between two applications or in a distributed system for asynchronous communication. Java Message Service is a platform-independent API, and most MOM providers support JMS.
ActiveMQ is the most popular and powerful open source message bus produced by Apache. ActiveMQ is a JMS Provider implementation that fully supports the JMS1.1 and J2EE 1.4 specifications. Although the JMS specification has been published for a long time, JMS still plays a special role in today's J2EE applications.
ActiveMQ development package download and running environment construction
Homepage: http://activemq.apache.org/
The latest version: 5.11.1 Development kit and source code download address: http://activemq.apache.org/activemq-5111-release.html
ActiveMQ service startup address: http://127.0.0.1:8161/admin/ username/password admin/admin
ActiveMQ method:
1. Direct Receive method
Session.AUTO_ACKNOWLEDGE. When the client successfully returns from the receive method, or from the MessageListener.onMessage method, the session automatically acknowledges the message received by the client.
Session.CLIENT_ACKNOWLEDGE. The client acknowledges the message through the message's acknowledge method. Note that, in this mode, acknowledgment is done at the session layer: acknowledging a consumed message will automatically acknowledgment all messages that have been consumed by the session. For example, if a message consumer consumes 10 messages and then acknowledges the 5th message, all 10 messages are acknowledged.
Session.DUPS_ACKNOWLEDGE. The selection is only the submission of the session sluggish acknowledgment message. If the JMS provider fails, some duplicate messages may result. If it is a duplicate message, then the JMS provider must set the JMSRedelivered field of the message header to true.
2. Use Listener to listen
-
basic concept
JMS is a Java message service, and asynchronous message transmission can be performed between JMS clients through the JMS service.
-
message model
○ Point-to-Point(P2P) ○ Publish/Subscribe(Pub/Sub)
i.e. peer-to-peer and publish-subscribe models
-
P2P
- P2P Mode Diagram
- concepts involved
- Message queue (Queue)
- Sender
- Receiver
- Each message is sent to a specific queue, and the receiver gets the message from the queue. Queues hold messages until they are consumed or time out.
-
Features of P2P
- There is only one consumer per message (that is, once consumed, the message is no longer in the message queue)
- There is no time dependency between the sender and the receiver, that is, when the sender sends a message, whether the receiver is running or not, it will not affect the message being sent to the queue
- The receiver needs to reply to the queue successfully after receiving the message successfully
You need P2P mode if you want every message you send should be processed successfully.
- P2P Mode Diagram
-
Pub/Sub
- Pub/Sub Mode Diagram
- concepts involved
- Topic
- Publisher
- Subscriber
clients send messages to topics. Multiple publishers send messages to topics, and the system delivers these messages to multiple subscribers.
-
Features of Pub/Sub
- Each message can have multiple consumers
- There is a time dependency between publishers and subscribers. For a subscriber of a topic (Topic), it must create a subscriber before it can consume the message of the publisher, and in order to consume the message, the subscriber must keep the running state.
- To ease such strict time dependencies, JMS allows subscribers to create a durable subscription. This way, even if the subscriber is not activated (running), it can receive messages from the publisher.
If you want to send a message that can be processed without any processing, or processed by one messager, or can be processed by multiple consumers, then you can use the Pub/Sub model
- Pub/Sub Mode Diagram
-
消息的消费
在JMS中,消息的产生和消息是异步的。对于消费来说,JMS的消息者可以通过两种方式来消费消息。
○ 同步
订阅者或接收者调用receive方法来接收消息,receive方法在能够接收到消息之前(或超时之前)将一直阻塞
○ 异步
订阅者或接收者可以注册为一个消息监听器。当消息到达之后,系统自动调用监听器的onMessage方法。 -
JMS编程模型
(1) ConnectionFactory
创建Connection对象的工厂,针对两种不同的jms消息模型,分别有QueueConnectionFactory和TopicConnectionFactory两种。可以通过JNDI来查找ConnectionFactory对象。
(2) Destination
Destination的意思是消息生产者的消息发送目标或者说消息消费者的消息来源。对于消息生产者来说,它的Destination是某个队列(Queue)或某个主题(Topic);对于消息消费者来说,它的Destination也是某个队列或主题(即消息来源)。
所以,Destination实际上就是两种类型的对象:Queue、Topic可以通过JNDI来查找Destination。
(3) Connection
Connection表示在客户端和JMS系统之间建立的链接(对TCP/IP socket的包装)。Connection可以产生一个或多个Session。跟ConnectionFactory一样,Connection也有两种类型:QueueConnection和TopicConnection。
(4) Session
Session是我们操作消息的接口。可以通过session创建生产者、消费者、消息等。Session提供了事务的功能。当我们需要使用session发送/接收多个消息时,可以将这些发送/接收动作放到一个事务中。同样,也分QueueSession和TopicSession。
(5) 消息的生产者
消息生产者由Session创建,并用于将消息发送到Destination。同样,消息生产者分两种类型:QueueSender和TopicPublisher。可以调用消息生产者的方法(send或publish方法)发送消息。
(6) 消息消费者
消息消费者由Session创建,用于接收被发送到Destination的消息。两种类型:QueueReceiver和TopicSubscriber。可分别通过session的createReceiver(Queue)或createSubscriber(Topic)来创建。当然,也可以session的creatDurableSubscriber方法来创建持久化的订阅者。
(7) MessageListener
消息监听器。如果注册了消息监听器,一旦消息到达,将自动调用监听器的onMessage方法。EJB中的MDB(Message-Driven Bean)就是一种MessageListener。
-
企业消息系统的好处
我们先来看看下图,应用程序A将Message发送到服务器上,然后应用程序B从服务器中接收A发来的消息,通过这个图我们一起来分析一下JMS的好处:
- 提供消息灵活性
- 松散耦合
- 异步性