1.pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.knife</groupId>
<artifactId>testmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testmq</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
2.添加queue
@Bean
public Queue helloQueue() {
return new Queue("testmq");
}
3.发送者
package com.knife.testmq;
import java.util.Date;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloSender1 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String sendMsg = "hello1 " + new Date();
System.out.println("Sender1 : " + sendMsg);
this.rabbitTemplate.convertAndSend("testmq", sendMsg);
}
}
4.接收者
package com.knife.testmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "testmq")
public class HelloReceive1 {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver1 : " + hello);
}
}
5.测试
package com.knife.testmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebAppConfiguration
public class UnitTest {
@Autowired
private HelloSender1 helloSender1;
@Test
public void hello() {
helloSender1.send();
}
}
6.结果
2019-03-07 19:24:59.854 INFO 9828 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase -2147482648
2019-03-07 19:24:59.854 INFO 9828 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2019-03-07 19:24:59.931 INFO 9828 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory : Created new connection: SimpleConnection@5a4bcc64 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 10310]
2019-03-07 19:24:59.995 INFO 9828 --- [ main] com.knife.testmq.UnitTest : Started UnitTest in 3.523 seconds (JVM running for 4.278)
Sender1 : hello1 Thu Mar 07 19:25:00 CST 2019
2019-03-07 19:25:00.040 INFO 9828 --- [ Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@2dde1bff: startup date [Thu Mar 07 19:24:57 CST 2019]; root of context hierarchy
2019-03-07 19:25:00.041 INFO 9828 --- [ Thread-1] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 2147483647
2019-03-07 19:25:00.045 INFO 9828 --- [ Thread-1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
Receiver1 : hello1 Thu Mar 07 19:25:00 CST 2019
2019-03-07 19:25:00.057 INFO 9828 --- [ Thread-1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2019-03-07 19:25:00.057 INFO 9828 --- [ Thread-1] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase -2147482648