Springboot中怎么对ActiveMQ进行整合,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、首先新建一个springboot工程(新建过程略),本文springboot版本 是2.1.1.RELEASE
2、在pom.xml文件添加activemq的相关依赖
<!--activemq消息队列-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池 此处使用2.0+的版本-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<!-- <version>5.15.0</version> -->
</dependency>
<!-- 消息队列连接池 此处使用2.1+的版本 -->
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
其中连接池相关的依赖可以不用配置
3、配置application.properties 或者application.yml 本文以application.properties为例
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
#spring.jms.pub-sub-domain=true
#true 表示使用内置的MQ,false则连接服务器
spring.activemq.in-memory=false
#true表示使用连接池;false时,每发送一条数据创建一个连接
spring.activemq.pool.enabled=true
#连接池最大连接数
spring.activemq.pool.max-connections=10
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=15000
spring.activemq.pool.enabled=true时要在pom文件中添加连接池pool相关的依赖,为false时不用添加连接池pool相关的依赖;
若使用连接池pool配置时,注意两种依赖的配置否则启动失败。
工程结构如下图
Demo代码如下
package com.example.acmpp.config;
import javax.jms.Queue;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
//定义存放消息的队列
@Bean
public Queue queue() {
return new ActiveMQQueue("ActiveMQQueue");
}
//定义存放消息的队列
@Bean
public Topic topic() {
return new ActiveMQTopic("ActiveMQTopic");
}
}
消息生产者代码
import javax.jms.Queue;
import javax.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
*
* 消息生产者
*/
@RestController
public class ProviderController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
/**
* 消息生产者 Queue模式
*
*/
@RequestMapping("/sendQ")
public void sendQ(String msg) {
//方法一:添加消息到消息队列
jmsMessagingTemplate.convertAndSend(queue, msg);
//方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列
//jmsMessagingTemplate.convertAndSend("testQ", msg);
}
/**
* 消息生产者 Topic模式
* @param msg
*/
@RequestMapping("/sendT")
public void sendT(String msg) {
// 指定消息发送的目的地及内容
System.out.println("@@@@@@@@@@@@@@" + msg);
this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
}
消息消费者代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
/**
* 消息消费者
* @author FFF
*
*/
@Component
public class ConsumerService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 消费ActiveMQQueue
*/
// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "ActiveMQQueue")
// SendTo 会将此方法返回的数据, 写入到 OutQueue 中去.
@SendTo("SQueue")
public String handleMessage(String name) {
System.out.println("ActiveMQQueue成功接受Name" + name);
return "ActiveMQQueue成功接受Name" + name;
}
/**
* 消费ActiveMQ.DLQ
*/
// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "ActiveMQ.DLQ")
public void DLQ(String name) {
System.out.println("ActiveMQ.DLQ成功接受Name==" + name);
}
/**
* 消费SQueue
*/
// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "SQueue")
public void SQueue(String name) {
System.out.println("SQueue成功接受Name==" + name);
}
/**
* 消费testQ
*/
// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "testQ")
public void testQMessage(String name) {
System.out.println("testQ成功接受Name" + name);
}
/**
* 消费topic
*
*/
// 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
@JmsListener(destination = "ActiveMQTopic")
public void topicMessage(String name) {
System.out.println("topicMessage成功接受Name" + name);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3318204/blog/3116314