MQ 系列之 JMSTemplate

时间:2022-07-26
本文章向大家介绍MQ 系列之 JMSTemplate,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.1 简介

1.1.1 概述

  JMSTemplate 是由 Spring 提供的一个 JMS 消息发送模板(与 JDBCTmplate 类似),可以用来方便地进行消息的发送,消息发送方法 convertAndSend 的第一个参数是消息队列,第二个参数是消息内容,@JmsListener 注解则表示相应的方法是一个消息消费者。

1.1.2 整合 JMSTemplate

☞ Spring 整合 JMSTemplate ☞ SpringBoot 整合 JMSTemplate

1.2 JMSTemplate 的使用

1.2.1 常用方法

  消息接收可以使用消息监听的方式替代模板方法,但是发送的时候是无法代替的,必须使用 Spring 提供的 JmsTemplate 中的方法来进行发送操作。浏览只是针对 Queue 的概念,Topic 没有浏览。浏览是指获取消息而消息依然保持在 broker 中,而消息的接收会把消息从 broker 中移除。

方法

描述

void send(Destination destination,MessageCreator messageCreator)

将消息发送到指定的 Destination

void send(String destinationName,MessageCreator messageCreator)

将消息发送到指定的 Destination这个方法和send(Destination destination,MessageCreator messageCreator) 做同样的事情,只是这个方法依赖于解析器和类型

void send(MessageCreator messageCreator)

将消息发送到默认的 Destination,这个方法要求提前设置 defaultDestination可以调用 setDefaultDestination(Destination destination) 或者 setDefaultDestinationName(String destinationName) 来满足这个前提

void convertAndSend(Destination destination,Object message)

将 message 转换成 JMS 的 Message,并发送到指定的 Destination

void convertAndSend(String destinationName,Object message)

将 message 转换成 JMS 的 Message,并发送到指定的 Destination

void convertAndSend(Object message)

将 message 转换成 JMS 的 Message,并发送到默认的 Destination

Message receive(Destination destination)

从指定的 Destination 接收消息并返回

Message receive(String destinationName)

从指定的 Destination 接收消息并返回

Message receive()

从默认的 Destination 接收消息并返回

Object receiveAndConvert()

参考 Message receive(),在此基础上做了转换

Object receiveAndConvert(Destination destination)

参考 Message receive(Destination destination),在此基础上做了转换

Object receiveAndConvert(String destinationName)

参考 Message receive(String destinationName),在此基础上做了转换

T browse(BrowserCallback action)

从默认的 Queue 中浏览消息,要求设置了默认的 Destination,且类型为 Queueaction 是一个回调对象,它负责浏览消息,并返回浏览的结果

T browse(Queue queue,BrowserCallback action)

从指定的 Queue 中浏览消息

T browse(String queueName,BrowserCallback action)

从指定 name 的 Queue 中浏览消息

T doInJms(Session session, QueueBrowser browser)

浏览队列中的消息,并返回浏览的结果这是一个回调方法,Spring 会为我们提供 QueueBrowser 对象,我们可以据此获取消息,并自由的转换。最后以我们希望的类型返回

void setDeliveryMode(int deliveryMode)

设置是否持久化要发送的消息:1-非持久化;2-持久化

int getDeliveryMode()

获取持久化模式的设置:1-非持久化;2-持久化

void setDeliveryPersistent(boolean deliveryPersistent)

设置是否持久化要发送的消息,true-持久化;false-非持久化

void setPriority(int priority)

为将要发送的消息设置优先级

int getPriority()

获取优先级

void setTimeToLive(long timeToLive)

设置消息的存活时间,毫秒单位

long getTimeToLive()

获取消息的存活时间,毫秒单位

void setReceiveTimeout(long receiveTimeout)

设置接收等待时间,毫秒单位

long getReceiveTimeout()

获取接收等待时间,毫秒单位

1.2.2 发送消息

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/5
 * @description JMS 发送消息
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class MyMQTest {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Test
    public void jms() {
        jmsTemplate.convertAndSend(new ActiveMQQueue("myTest"), "测试消息");
    }
}

1.2.3 接收消息

☞ 普通方式

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/5
 * @description AMQ 接收消息
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class MyMQTest {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Test
    public void recevie() {
        Object myTest = jmsTemplate.receiveAndConvert("myTest");

        if (!Objects.isNull(myTest)) {
            System.out.println(myTest);
        }
    }
}

☞ 监听消息

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/5
 * @description AMQ 监听消息
 */
@Component
public class MyMQListener {

    @Autowired
    private JmsTemplate jmsTemplate;

    @JmsListener(destination = "myTest")
    // 可监听多个队列
    // @JmsListeners(value = {@JmsListener(destination = "T1"), @JmsListener(destination = "T2")})
    public void listener(String msg) {
        if (!Objects.isNull(msg)) {
            System.out.println(msg);
       }
    }
}