Explain JMS in a simple way (1)--Basic concepts of JMS

摘要:The Java Message Service (JMS) API is a messaging standard that allows application components based on the Java Platform Enterprise Edition (Java EE) to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous.

The JMS (JAVA Message Service, java message service) API is a standard or specification of a message service that allows application components to create, send, receive and read messages based on the JavaEE platform. It makes distributed communication less coupled, messaging services more reliable and asynchronous.

In this blog post, we mainly introduce JMS, an important specification in J2EE, because this specification is widely used and important in the enterprise. We mainly introduce the basic concepts of JMS and its mode, message consumption and JMS programming steps.

  1. basic concept

    JMS is a Java message service, and asynchronous message transmission can be performed between JMS clients through the JMS service.

  2. message model

    ○ Point-to-Point(P2P)
    ○ Publish/Subscribe(Pub/Sub)
    

    i.e. peer-to-peer and publish-subscribe models

  3. P2P

    1. P2P Mode Diagram 
      write picture description here
    2. concepts involved 
      1. Message queue (Queue)
      2. Sender
      3. Receiver
      4. 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.
    3. Features of P2P

      1. There is only one consumer per message (that is, once consumed, the message is no longer in the message queue)
      2. 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
      3. 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.

  4. Pub/Sub

    1. Pub/Sub Mode Diagram 
      write picture description here
    2. concepts involved 
      1. Topic
      2. 发布者(Publisher)
      3. 订阅者(Subscriber) 
        客户端将消息发送到主题。多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。
    3. Pub/Sub的特点

      1. 每个消息可以有多个消费者
      2. 发布者和订阅者之间有时间上的依赖性。针对某个主题(Topic)的订阅者,它必须创建一个订阅者之后,才能消费发布者的消息,而且为了消费消息,订阅者必须保持运行的状态。
      3. 为了缓和这样严格的时间相关性,JMS允许订阅者创建一个可持久化的订阅。这样,即使订阅者没有被激活(运行),它也能接收到发布者的消息。

      如果你希望发送的消息可以不被做任何处理、或者被一个消息者处理、或者可以被多个消费者处理的话,那么可以采用Pub/Sub模型

  5. 消息的消费 
    在JMS中,消息的产生和消息是异步的。对于消费来说,JMS的消息者可以通过两种方式来消费消息。 
    ○ 同步 
    订阅者或接收者调用receive方法来接收消息,receive方法在能够接收到消息之前(或超时之前)将一直阻塞 
    ○ 异步 
    订阅者或接收者可以注册为一个消息监听器。当消息到达之后,系统自动调用监听器的onMessage方法。

  6. 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。

  7. 企业消息系统的好处

Let's take a look at the figure below. Application A sends the message to the server, and then application B receives the message from A from the server. Through this figure, let's analyze the benefits of JMS: 
write picture description here

  1. Provides message flexibility
  2. loosely coupled
  3. asynchrony

For the basic concepts of JMS, we have introduced so much. The next blog post will introduce a JMS implementation.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326779794&siteId=291194637