前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot集成ActiveMQ

springboot集成ActiveMQ

作者头像
麦克劳林
发布2018-09-11 16:34:52
8911
发布2018-09-11 16:34:52
举报

1、pom依赖如下:

代码语言:javascript
复制
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-pool</artifactId>
    </dependency>

2、修改application.yml配置文件

application.yml配置

代码语言:javascript
复制
#ActiveMQ配置
activemq:
    broker-url: tcp://180.130.180.70:61616
    in-memory: true
    pool:
      enabled: false
##队列
messages:
    queue: messages_queue

3、生产者

代码语言:javascript
复制
package com.chhliu.springboot.jms;  

import javax.jms.Destination;  

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

@Service("producer")  
public class Producer {  
    @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装  
    private JmsMessagingTemplate jmsTemplate;  
    // 发送消息,destination是发送到的队列,message是待发送的消息  
    public void sendMessage(Destination destination, final String message){  
        jmsTemplate.convertAndSend(destination, message);  
    }  
}

4、消费者

代码语言:javascript
复制
package com.chhliu.springboot.jms;  
  
import org.springframework.jms.annotation.JmsListener;  
import org.springframework.stereotype.Component;  
  
@Component  
public class Consumer {  
        // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息  
    @JmsListener(destination = "mytest.queue")  
    public void receiveQueue(String text) {  
        System.out.println("Consumer收到的报文为:"+text);  
    }  
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.06.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档