SpringBoot项目集成RabbitMq
来源:互联网 发布:淘宝花种子哪家好 编辑:程序博客网 时间:2024/05/16 12:10
SpringBoot项目 集成RabbitMq
RabbitMq是目前比较流行的几个Mq之一,也是Spring官方AMQP的实现,官方文档地址:
https://docs.spring.io/spring-amqp/docs/2.0.0.RELEASE/reference/html/
RabbitMq也是国内很多互联网公司都在使用,今天主要说一下,它咋SpringBoot项目中的具体实现.
1.首先在pom中添加依赖
SpringBoot已经帮我们封装好了依赖,只需要我们导入到自己的pom文件中就可以.
<!-- RabbitMq队列 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.添加RabbitMq服务器的配置信息
这个你可以写在一个properties文件中,使用的时候可以用@value(${xxx})
把每个参数挨个读取到类中使用,也可以直接用一个bean直接接受这几个参数,这样更优雅一些,我这里使用的是后者.
properties文件中的配置信息:
spring.rabbitmq.host=172.xx.xx.xxx
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxxx
spring.rabbitmq.password=xxxx
接受参数的bean
@Component
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitMqConfig {
private String host;
private Integer port;
private String username;
private String password;
//省略get() set()
3.写一个configuration类添加配置信息
`/**
* 队列连接工厂类
*
* @author hou
* @date 2017-11-13 10:35
*/
@Configuration
public class RabbitMqConfiguration {
private static final Logger logger = LogManager.getLogger(RabbitMqConfiguration.class);
@Autowiredprivate RabbitMqConfig rabbitMqConfig;/** * 配置队列连接Factory * @return */@Beanpublic ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMqConfig.getHost(),rabbitMqConfig.getPort()); connectionFactory.setUsername(rabbitMqConfig.getUsername()); connectionFactory.setPassword(rabbitMqConfig.getPassword());
// connectionFactory.setVirtualHost(this.mqRabbitVirtualHost);
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
/** * 用来发送消息 * @return */@Beanpublic RabbitTemplate rabbitTemplate() { RabbitTemplate template = new RabbitTemplate(connectionFactory()); return template;}/** * 配置交换机 * @return */@Beanpublic DirectExchange defaultExchange() { return new DirectExchange(Constants.EXCHANGE_NAME_MESSAGE);}/** * 构建队列,名称,是否持久化之类 * @return */@Beanpublic Queue toonMessageQueue() { return new Queue(Constants.QUEUE_NAME_TOON_MESSAGE, true);}/** * 将DirectExchange与Queue进行绑定 * @return */@Beanpublic Binding binding() { return BindingBuilder.bind(toonMessageQueue()).to(defaultExchange()).with(Constants.ROUTING_KEY_MESSAGE);}@Autowiredprivate MessageListener messageListener;@Beanpublic SimpleMessageListenerContainer execMessageContainerOne() { //设置监听者“容器” SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory()); //设置队列名 container.setQueueNames(Constants.QUEUE_NAME_TOON_MESSAGE); //设置监听者数量,即消费线程数 container.setConcurrentConsumers(1); container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> { byte[] body = message.getBody(); if(null != body) { try { String msg = new String(body); //消费消息 messageListener.listenerAndSendToonMessage(msg); } catch(Exception e) { e.printStackTrace(); } } }); return container;}`
4.处理消息的实现方法,和放入消息的方法
放入消息的实现:
`
/**
* 队列消费者实现类
*
* @author 146584
* @date 2017-11-13 11:19
*/
@Service
public class QueueProductServiceImpl implements RabbitTemplate.ConfirmCallback{
private static final Logger logger = LogManager.getLogger(QueueProductServiceImpl.class);
@Autowiredprivate RabbitTemplate rabbitTemplate;public QueueProductServiceImpl(RabbitTemplate rabbitTemplate){ this.rabbitTemplate=rabbitTemplate; this.rabbitTemplate.setConfirmCallback(this);}@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) { logger.info(":===>消息id:" + correlationData); if (ack) { logger.info("放入队列成功"); } else { logger.info("放入队列失败:" + cause); }}/** * 发送消息,不需要实现任何接口,供外部调用 * @param msg */public void send(String msg){ CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString()); String result = rabbitTemplate.convertSendAndReceive(Constants.EXCHANGE_NAME_MESSAGE,Constants.ROUTING_KEY_MESSAGE, msg, correlationId)+"";}`
消息消费的实现
`@Service
public class MessageListener {
private static final Logger logger = LogManager.getLogger(MessageListener.class);
public void listenerAndSendToonMessage(String message) { //此处省略处理逻辑}`
写一个测试用例测试一下
` @Autowired
private MessageQueueController messageQueueController;
@Testpublic void testSendMsg(){ List<MessageEntity> messageEntityList = new ArrayList<>(); for(int i=0;i<200;i++){ MessageEntity entity = new MessageEntity(); //设置参数.... messageEntityList.add(entity); } messageQueueController.sendMessageByQueue(messageEntityList);}`
output
引用:
http://www.mamicode.com/info-detail-1971209.html
https://www.cnblogs.com/ASPNET2008/p/6414145.html
- SpringBoot项目集成RabbitMq
- SpringBoot集成RabbitMQ
- Springboot集成RabbitMq
- Springboot集成RabbitMQ
- SpringBoot 快速集成RabbitMQ
- Springboot之集成RabbitMQ
- springboot集成RabbitMQ
- Springboot集成RabbitMq
- springboot中rabbitmq集成——单项目
- springboot中rabbitmq集成——单项目
- SpringBoot项目实战--RabbitMQ
- springboot(集成篇):RabbitMQ集成详解
- springboot项目集成Swagger2
- springBoot项目集成dubbox
- 随便谈谈RabbitMQ与springBoot进行集成。
- springboot集成rabbitMQ之对象传输
- springboot中rabbitmq集成——多项目
- 微服务,微架构[十三]springboot集成RabbitMQ
- 在cmd中报错:java找不到或无法加载主类
- HaoSQL_好用的SQL等数据库一键包发布!
- 【递归入门】组合+判断素数
- Qt5.9 在windows下静态编译
- Scala.for.the.Impatient.2nd.Edition.2016.12.pdf 英文原版 免费下载
- SpringBoot项目集成RabbitMq
- esayui查询信息单选按钮回显选中
- IT领域的721定律
- python 写公司日报3
- 扩展系统功能——装饰模式(二)
- TextInputLayout和TextInputEditText使用
- 国外机房供电模式不如国内的?对比一下就知道了
- mybatis jdbctype类型
- 前端面试(css进阶部分)