pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 添加 junit 环境的 jar 包 -->
<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-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
application.yml
server:
tomcat:
uri-encoding: UTF-8
spring:
application:
name: 01-spring-boot-rabbitmq
rabbitmq:
host: 192.168.0.72
port: 5672
username: superrd
password: superrd
QueueConfig.java
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 创建消息队列
**/
@Configuration
public class QueueConfig {
/**
* 创建队列
**/
@Bean
public Queue createQueue(){
return new Queue("hello-queue");
}
}
Sender.java
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息发送者
* @author Administrator
*
*/
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitAmqpTemplate;
/*
* 发送消息的方法
*/
public void send(String msg){
//向消息队列发送消息
//参数一:队列的名称。
//参数二:消息
this.rabbitAmqpTemplate.convertAndSend("hello-queue", msg);
}
}
Receiver.java
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息接收者
* @author Administrator
*
*/
@Component
public class Receiver {
/**
* 接收消息的方法。采用消息队列监听机制
* @param msg
*/
@RabbitListener(queues="hello-queue")
public void process(String msg){
System.out.println("receiver: "+msg);
}
}
测试代码(QueueTest.java):
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;
import com.jorudan.App;
import com.jorudan.Sender;
/**
* 消息队列测试类
* @author Administrator
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class QueueTest {
@Autowired
private Sender sender;
/*
* 测试消息队列
*/
@Test
public void test1(){
this.sender.send("Hello RabbitMQ");
}
}
正确结果