亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot基于Active MQ實現(xiàn)整合JMS

 更新時間:2020年07月02日 11:40:28   作者:樓蘭的胡楊  
這篇文章主要介紹了Spring Boot基于Active MQ實現(xiàn)整合JMS,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

我們使用jms一般是使用spring-jms和activemq相結(jié)合,通過spring Boot為我們配置好的JmsTemplate發(fā)送消息到指定的目的地Destination。本文以點到點消息模式為例,演示如何在Spring Boot中整合 JMS 和 Active MQ ,實現(xiàn) MQ 消息的生產(chǎn)與消費。

點到點消息模式定義:當消息發(fā)送者發(fā)送消息,消息代理獲得消息后,把消息放入一個隊列里,當有消息接收者來接收消息的時候,消息將從隊列里取出并且傳遞給接收者,這時候隊列里就沒有此消息了。隊列Queue的存在使得消息的異步傳輸成為可能。

1 安裝ActiveMQ

關(guān)于具體的安裝步驟,請參考專門介紹安裝與部署的另一篇博文《ActiveMQ安裝及部署教程圖解》。

2 配置Active MQ 依賴

關(guān)于如何搭建 Spring Boot工程,請移步《如何使用intellij IDEA搭建Spring Boot項目》。假設(shè)項目已經(jīng)創(chuàng)建完畢,在項目 pom 文件中加入Active MQ依賴:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-activemq</artifactId>
 <version>2.2.7.RELEASE</version>
</dependency>

3 修改application.properties配置文件

在application.properties中配置Active MQ:

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
#默認值false,表示點到點模式,true時代表發(fā)布訂閱模式
spring.jms.pub-sub-domain=false

4 創(chuàng)建消息生產(chǎn)者和消費者

本文以點到點消息模式演示。新增消息生產(chǎn)者:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

@Service("producer")
public class Producer {
 /**
  * 也可以注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝
  */
 @Autowired
 private JmsMessagingTemplate jmsTemplate;

 /**
  * 發(fā)送消息,destination是發(fā)送到的隊列,message是待發(fā)送的消息
  *
  * @param destination
  * @param message
  */
 public void sendMessage(Destination destination, final String message) {
  jmsTemplate.convertAndSend(destination, message);
 }
}

通過JmsMessagingTemplate 的convertAndSend方法向目的地 destination發(fā)送消息。 新增消息監(jiān)聽者:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class ConsumerListener {
 private static Logger logger = LoggerFactory.getLogger(Consumer.class);
 /**
  * 使用JmsListener配置消費者監(jiān)聽的隊列
  * @param receivedMsg 接收到的消息
  */
 @JmsListener(destination = "myDest.queue")
 public void receiveQueue(String receivedMsg) {
  logger.info("Consumer收到的報文為: {}", receivedMsg);
 }
}

@JmsListener是Spring 4.1 提供的一個新特性,用于簡化JMS開發(fā),只需使用此注解的屬性destination指定要監(jiān)聽的目的地【myDest.queue】,即可接收該目的地發(fā)送的消息。消費者2 ConsumerListener2 的代碼同上,但是日志如下:

logger.info("Consumer2收到的報文為: {}", receivedMsg);

溫馨提示,消息消費者和生產(chǎn)者的類上必須加上注解 @Component 或者 @Service,使得消息消費者類就會被委派給Listener類,原理類似于使用SessionAwareMessageListener以及MessageListenerAdapter來實現(xiàn)消息驅(qū)動POJO。

5 測試 MQ

在Junit測試類中新增測試方法

import org.apache.activemq.command.ActiveMQQueue;
import javax.jms.Destination;

@Autowired
private Producer producer;
@Test
public void jmsActiveMqTest() throws InterruptedException {
 Destination destination = new ActiveMQQueue("myDest.queue");
 for (int i = 0; i < 21; i++) {
  producer.sendMessage(destination, String.format("My name is Wiener%s", i));
 }
}

控制臺顯示消費者接收到的消息如下:

Consumer收到的報文為: My name is Wiener0
Consumer2 收到的報文為:My name is Wiener1
Consumer收到的報文為: My name is Wiener2
Consumer2 收到的報文為:My name is Wiener3
Consumer收到的報文為: My name is Wiener4
Consumer2 收到的報文為:My name is Wiener5
Consumer收到的報文為: My name is Wiener6
Consumer2 收到的報文為:My name is Wiener7
...

經(jīng)過上面的五個步驟,spring Boot、Jms 和 Active MQ 就基本上整合完成了,是不是使用起來很方便了!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論