MQ系列01:JMS API

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模式图
      avatar
    • 4.1.2 涉及到的概念
      • 消息队列(Queue)
      • 发送者(Sender)
      • 接收者(Receiver)
      • 每个消息都被发送到一个特定的队列,接收者从队列中获取消息。队列保留着消息,直到他们被消费或超时
    • 4.1.3 P2P的特点
      • 每个消息只有一个消费者(Consumer)(即一旦被消费,消息就不再在消息队列中)
      • 发送者和接收者之间在时间上没有依赖性,也就是说当发送者发送了消息之后,不管接收者有没有正在运行,它不会影响到消息被发送到队列
      • 接收者在成功接收消息之后需向队列应答成功
  • 4.2 发布/订阅(主题目的地):在此模型中,消息从生产者传递给任意数量的消费者。
    消息将传递到主题目标,然后传递给已订阅该主题的所有活动使用者。
    此外,任何数量的生产者都可以向主题目的地发送消息,并且每个消息可以被传递给任意数量的订户。
    如果没有注册消费者,则主题目标不会保留消息,除非它对非活动消费者具有持久订阅。
    持久订阅表示在主题目标中注册的使用者,该消费者在将消息发送到主题时可以是不活动的。

    Publish/Subscribe (Topic destination): In this model, a message is delivered from a producer to any number of consumers.
    Messages are delivered to the topic destination, and then to all active consumers who have subscribed to the topic.
    In addition, any number of producers can send messages to a topic destination, and each message can be delivered to any number of subscribers.
    If there are no consumers registered, the topic destination doesn’t hold messages unless it has durable subscription for inactive consumers.
    A durable subscription represents a consumer registered with the topic destination that can be inactive at the time the messages are sent to the topic.

    • 4.2.1 Pub/Sub模式图
      avatar
    • 4.2.2 涉及到的概念
      • 主题(Topic)
      • 发布者(Publisher)
      • 订阅者(Subscriber)
      • 客户端将消息发送到主题。多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。
    • 4.2.3 Pub/Sub的特点
      • 每条消息可以有多个消费者
      • 发布者和订阅者之间有时间上的依赖性。针对某个主题(Topic)的订阅者,它必须创建一个订阅者之后,才能消费发布者的消息,而且为了消费消息,订阅者必须保持运行的状态。
      • 为了缓和这样严格的时间相关性,JMS允许订阅者创建一个可持久化的订阅。这样,即使订阅者没有被激活(运行),它也能接收到发布者的消息。

5 消息的消费

在JMS中,消息的产生和消费是异步。对于消费来说,JMS的消息者可以通过两种方式(同步/异步)来消费消息。

  • 5.1 同步
    订阅者或接收者调用receive方法来接收消息,receive方法在能够接收到消息之前(或超时之前)将一直阻塞
  • 5.2 异步
    订阅者或接收者可以注册为一个消息监听器。当消息到达之后,系统自动调用监听器的onMessage方法。

6 JMS API编程模型

  • JMS API编程模型图
    avatar
  • JMS应用程序的基本构建模块
    • Administered objects: connection factories and destinations
      • connection factory:用于创建JMS客户端与JMS提供者之间的连接。根据不两种不同的JMS消息类型,分类可创建QueueConnectionFactory和TopicConnectionFactory两种Connection对象。
        Context ctx = new InitialContext();
        //queue
        QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("jms/QueueConnectionFactory");
        //topic
        TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("jms/TopicConnectionFactory"); 
        
      • destination:生产者消息发送的目标,消费者消息消费的来源。在PTP模式中,destination是队列(Queue);在Pub/Sub模式中,destination是主题(Topic)
        //queue
        Queue queue = (Queue) ctx.lookup("jms/Queue");
        //topic
        Topic topic = (Topic) ctx.lookup("jms/Topic");
        
    • Connections:
      • Connection根据JMS的类型,分为QueueConnection和TopicConnection。每个个Connection可以创建一个或多个Session。
      • 每个Connection封装了与JMS提供程序之间建立的连接(比如:对TCP/IP socket 的封装)
        //queue
        QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
        //topic
        TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
        
    • Sessions
      • Session是用于产生和使用消息的单线程上下文,分为QueueSession和TopicSession。
      • Session可创建生产者、消费者、消息、队列、主题等。
      • Session具有事务功能,可将一组发送和接收操作放入到一个事务中。
        //queue
        QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        //topic
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        
    • JMSContext objects, which combine a connection and a session in one object
      • JMSContext 是 Connections 和 Sessions的结合体
        JMSContext jmsContext = connectionFactory.createContext(JMSContext.AUTO_ACKNOWLEDGE);
        
    • Message producers
      • 消息生产者,将消息发送到Destination。分两种类型:QueueSender和TopicPublisher,使用send()或publish()发送消息
        //queue
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.send(msg);
        //topic
        TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicPublisher.publish(msg);
        
    • Message consumers
      • 消息消费者,用于接收被发送到Destination的消息。两种类型:QueueReceiver和TopicSubscriber。
      • 可使用JMSContext.createDurableConsumer方法创建持久订阅
        //queue
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        //topic
        TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
        
      • JMS Message Listeners:消息监听器,用于监听消息
        Listener myListener = new Listener();
        consumer.setMessageListener(myListener);
        
        //监听器
        public class MyListener implements MessageListener {
            @Override
            public void onMessage(Message message) {
                try {
                    TextMessage msg = (TextMessage) message;
                    System.out.println(msg.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
        
    • Messages
      • 消息创建
        //common
        Message message = session.createMessage();
        XxxMessage xxxMessage = session.createXxxMessage();
        
      • 消息由三部分组成:
        • 消息头(必填):包含客户端和提供者用于标识和路由消息的值。

        • 属性(可选):提供有关数据的其他信息。属性可以视为标题的扩展,由属性名称/值对组成

        • 正文(可选):包含要交换的实际数据。JMS规范定义了JMS提供者必须支持的六种类型或类别的消息。

          消息类型描述
          TextMessage文本消息(String)
          MapMessage映射表消息(Key-Value)
          BytesMessage字节消息
          StreamMessage流消息
          ObjectMessage对象消息(可序列化的JAVA对象)
          MessageNothing.

参考资料

1 JMS Tutorial
2 Getting Started with Java Message Service (JMS)
3 深入浅出JMS(一)–JMS基本概念
4 Messaging Styles
5 The JMS API Programming Model

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值