博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RabbitMQ 生产与消费
阅读量:4655 次
发布时间:2019-06-09

本文共 6503 字,大约阅读时间需要 21 分钟。

Spring Boot + RabbitMQ 整合

新建Spring Boot 项目

pom.xml

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.9.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-maven-plugin

 

application.yml文件

spring:  rabbitmq:    host: 127.0.0.1    port: 5672    username: guest    password: guest    publisher-confirms: true    virtual-host: /  application:    name: rabbitMQ

 RabbitMQConfiguration.java

package com.example.demo;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class RabbitMQConfiguration {    private static final String QUEUE_SIMPLE_NAME = "hello";    // 队列    @Bean    public Queue queue(){        return new Queue(QUEUE_SIMPLE_NAME);    }}

 

创建消息的消费者 Consumer,java

package com.example.demo;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;/** * @author: DevanYan * @create: 2019-05-28 11:46 *///消息的消费者@Component@RabbitListener(queues = "hello")public class Consumer {    @RabbitHandler    public void use(String message) {        System.out.println("消费了一条消息 : " + message);    }}

 

创建消息的生产者 Creater.java

package com.example.demo;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * @author: DevanYan * @create: 2019-05-28 11:47 */// 消息的生产者@Componentpublic class Creater {    @Autowired    private AmqpTemplate amqpTemplate;    public void  create(){        String message = "Hello RabbitMQ !";        amqpTemplate.convertAndSend("hello", message);        System.out.println("创建了一条消息 : " + message);    }}

 

编写测试类

package com.example.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests {    @Autowired    Creater creater;    @Test    public void creatertest(){        creater.create();    }}

 

 

控制台输出

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.0.9.RELEASE)2019-05-28 13:36:49.927  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests on Yan with PID 9052 (started by Administrator in F:\cloud\RabbitMQ)2019-05-28 13:36:49.929  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to default profiles: default2019-05-28 13:36:49.952  INFO 9052 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ece4966: startup date [Tue May 28 13:36:49 CST 2019]; root of context hierarchy2019-05-28 13:36:50.507  INFO 9052 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$fd413eb4] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2019-05-28 13:36:51.141  INFO 9052 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 21474836472019-05-28 13:36:51.151  INFO 9052 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [127.0.0.1:5672]2019-05-28 13:36:51.231  INFO 9052 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#f4c0e4e:0/SimpleConnection@64fbb393 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 49490]2019-05-28 13:36:51.318  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 1.785 seconds (JVM running for 2.843)创建了一条消息 : Hello RabbitMQ !2019-05-28 13:36:51.424  INFO 9052 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ece4966: startup date [Tue May 28 13:36:49 CST 2019]; root of context hierarchy2019-05-28 13:36:51.426  INFO 9052 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647消费了一条消息 : Hello RabbitMQ !2019-05-28 13:36:51.466  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.2019-05-28 13:36:51.469  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.2019-05-28 13:36:51.469  INFO 9052 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 02019-05-28 13:36:51.472  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Shutdown ignored - container is not active alreadyDisconnected from the target VM, address: '127.0.0.1:49475', transport: 'socket'Process finished with exit code 0

 

转载于:https://www.cnblogs.com/devan/p/10936715.html

你可能感兴趣的文章
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
移动端单屏解决方案
查看>>
web渗透测试基本步骤
查看>>
使用Struts2标签遍历集合
查看>>
angular.isUndefined()
查看>>
第一次软件工程作业(改进版)
查看>>
网络流24题-飞行员配对方案问题
查看>>
Jenkins 2.16.3默认没有Launch agent via Java Web Start,如何配置使用
查看>>
引入css的四种方式
查看>>
iOS开发UI篇—transframe属性(形变)
查看>>
3月7日 ArrayList集合
查看>>
jsp 环境配置记录
查看>>
Python03
查看>>
LOJ 2537 「PKUWC2018」Minimax
查看>>
使用java中replaceAll方法替换字符串中的反斜杠
查看>>
Some configure
查看>>
流量调整和限流技术 【转载】
查看>>
1 线性空间
查看>>
VS不显示最近打开的项目
查看>>
DP(动态规划)
查看>>