一)入门测试RabbitMQ的入门测试
项目构建
pom坐标
添加的坐标位置是的rabbitMQ位置
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>03-springcloud-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>03-springcloud-rabbitmq</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot 对RabbitMQ进行的支持坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.修改核心配置文件的信息
1.添加的信息如下的
rabbitmq的ip地址
port:端口默认为5672
username:连接rabbitmq管理的用户名
password:连接rabbitmq的密码
spring:
application:
name: springcloud-rabbitmq
rabbitmq:
host: 192.168.177.140
port: 5672
username: oldlu
password: 123456
3.到此环境搭建完毕具体的流程可以在spring官方网站上找到安装的信息
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html#boot-features-amqp
二)测试
1.创建配置类创建一个的队列
/**
* @project_name:springcloud
* @date:2019/8/29:10:53
* @author:shinelon
* @Describe:RabbitMQ配置类,创建新的队列
*/
@Configuration
public class RabbitMQ {
/**
* 下面的方法是创建一个的新的队列
* @return
*/
@Bean
public Queue queue(){
return new Queue("hello-rabbitmq");
}
}
2.创建发送方
/**
* @project_name:springcloud
* @date:2019/8/29:10:57
* @author:shinelon
* @Describe:消息的发送方
*/
@Component
public class Send {
/**
* springboot提供对RabbitMQ操作的模板类
*/
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String msg) {
/**
* convertAndSend:参数 1.表示发送方的发送的队列
* 2.发送的信息
*/
this.amqpTemplate.convertAndSend("hello-rabbitmq", msg);
}
}
3.创建接收方
@RabbitListener注解的作用就是监听指定的队列的变化
/**
* @project_name:springcloud
* @date:2019/8/29:11:00
* @author:shinelon
* @Describe:接收方
*/
@Component
public class Receiver {
/**
* @RabbitListener:注解的作用是监听器的作用,用于监听对列的变化,如果队列发生了变话
* 会触发下面的方法
*queues:监听的队列名称
* @param msg :此参数是接受发送方传递的消息的
*/
@RabbitListener(queues = {"hello-rabbitmq"})
public void receiver(String msg){
System.out.println("receiver=="+msg);
}
}
4.测试类
import com.sxt.send.Send;
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)
@SpringBootTest(classes=Application.class)
public class ApplicationTests {
@Autowired
private Send send;
@Test
public void contextLoads() {
this.send.send("hello rabbitmq");
}
}
查看管理界面的出现了一个队列的名称