spring boot整合activemq rabbitmq

来源:互联网 发布:vlookup匹配多列数据 编辑:程序博客网 时间:2024/05/21 17:22

1.下载并安装activemq服务(windows),下载地址:http://download.csdn.net/download/loveuserzzz/9938202

2.创建springboot工程,并引入依赖

<dependency>   <groupId>org.apache.activemq</groupId>   <artifactId>activemq-all</artifactId>   <version>5.15.0</version></dependency>

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-activemq</artifactId></dependency>
3.编写生产者代码实现

@Componentpublic class Producer  implements CommandLineRunner{    @Autowired    private JmsMessagingTemplate jmsMessagingTemplate;    @Autowired    private Queue queue;    @Override    public void run(String... args) throws Exception {        send("Sample message");        System.out.println("Message was sent to the Queue");    }    public void send(String msg) {        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);    }}
4.编写客户端实现

@Componentpublic class Consumer {    @JmsListener(destination = "sample.queue")    public void receiveQueue(String text) {        System.out.println(text);    }}
5.编写application.properties

spring.activemq.in-memory=truespring.activemq.pool.enabled=false
6.启动activemq服务


7.启动main运行查看结果

下载源码:https://github.com/yifanzzz/springboot-mq.git

示例中包含activemq和rabbitmq两部分,可以加群一起讨论QQ:324070788





原创粉丝点击