开发环境:
1.springBoot 2.3.1.RELEASE
2.gradle 5.6
3.activeMQ 2.3.1.RELEASE
第一步:首先在build.gradle中导入activeMQ的依赖
buildscript {
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '2.3.1.RELEASE'
}
}
坐标可以在maven仓库中寻找地址:
第二步:在springBoot配置文件中配置activeMQ
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
pool:
enabled: true
max-connections: 50
#默认包名安全(支持序列化与反序列化)
packages:
trust-all: true
#支持topic订阅模式
jms:
pub-sub-domain: true
第三部:初始化消息队列
package com.es;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class ActiveMqConfig {
/**
* @Author GONGWENXUE
* @Description //TODO 消息队列初始化配置 - 创建消息队列
* @version: v1.8.0
* @Date 13:55 2020/7/31
* @Param
* @return
**/
@Bean
public Queue queue() {
return new ActiveMQQueue("test.queue");
}
}
第四部:创建消息发布者
package com.es.basedata.mq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Topic;
/**
* @Author GONGWENXUE
* @Description //TODO 定义生产者
* @version: v1.8.0
* @Date 17:04 2020/7/31
* @Param
* @return
**/
@Service
public class MqProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* @Author GONGWENXUE
* @Description //TODO 发布者发送自定义消息
* @version: v1.8.0
* @Date 16:58 2020/7/31
* @Param queueName 队列名称
* @Param message 消息内容
* @return
**/
public void sendMessage(String queueName, String message) {
//发送队列消息
this.jmsMessagingTemplate.convertAndSend(queueName, message);
}
}
第五步:创建消费者
package com.es.basedata.mq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* 定义消费者
* @Description
* @author Qz
* @date 2018年3月27日
*
*/
@Component
public class MqConsumer {
@JmsListener(destination = "test.queue")
public void receiveQueue(String text) {
System.out.println("消费者:来源于生产者对列的消息:"+text);
}
}
第六步:开发接口测试
package com.es.basedata.api;
import com.es.basedata.mq.MqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @className: TestMq
* @Description: TODO 测试MQ
* @version: v1.8.0
* @author: GONGWENXUE
* @date: 2020/7/31 17:05
*/
@RestController
public class TestMq {
@Autowired
private MqProducer mqProducer;
@GetMapping("/test-mq/{message}")
public String test(@PathVariable("message") String message){
mqProducer.sendMessage("test.queue", message);
System.out.println("浏览器上测试mq成功");
return "浏览器上测试mq成功";
}
}
在浏览器上输入 http://localhost:8085/test-mq/浏览器上测试MQ
得到结果:
后台打印:
到此即大功告成!!!
重点:可能遇到的坑
1.找不到JmsMessagingTemplate,无法匹配MessagingTemplateConfiguration
报错如下:
Field jmsMessagingTemplate in com.es.basedata.mq.MqProducer required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The following candidates were found but could not be injected:
- Bean method 'jmsMessagingTemplate' in 'JmsAutoConfiguration.MessagingTemplateConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match
解决办法:
1.手机打开微信--->搜索《Java深度编程》,或扫描下方二维码:
2.查看往期内容:找到《SpringBoot整合MQ报错JmsMessagingTemplate that could not be found解决》,按文章中的方式操作即可解决。
感谢您的阅读,愿您早日成为大神!!!