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