这几天学习 RabbitMQ 在安装配置完成后,就迫不及待的编码了.
以下是案例代码:
1.新建一个 springboot项目 / 或者maven项目.
这里是maven的依赖: pom.xml ,我这里是1.5.13的springboot .
如果是2.x的版本可能会不兼容.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.jacktu</groupId>
<artifactId>springcloud-mq</artifactId>
<packaging>jar</packaging>
<name>springcloud-mq</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置
这里默认端口是 5672不要搞错了,登陆界面的是 15672
spring:
application:
name: springcloud-mq
rabbitmq:
host: 127.0.0.1
port: 5672
virtual-host: /
username: guest
password: guest
2.src 中的代码
2.1 QueueConfig 消息队列配置
package top.jacktu.mq;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 创建消息队列
* @author jacktu
*/
@Configuration
public class QueueConfig {
/**
* 创建队列
*
* @return
*/
@Bean
public Queue createQueue() {
return new Queue("hello-queue");
}
}
2.2 Receiver 消息接收者
package top.jacktu.mq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息接收者
* @author jacktu
*/
@Component
public class Receiver {
/**
* 接收消息的方法.采用消息队列监听机制.
*/
@RabbitListener(queues = "hello-queue")
public void process(String msg) {
System.out.println("receiver: "+msg);
}
}
2.3 Sender 消息发送者
package top.jacktu.mq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息发送者
* @author jacktu
*
*/
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitAmqpTemplate;
/**
* 发送消息的方法
*/
public void send(String msg) {
//向消息队列发送消息
//参数一: 队列的名称
//参数二: 消息
this.rabbitAmqpTemplate.convertAndSend("hello-queue", msg);
}
}
2.4 SpringcloudMqApplication 项目启动类
package top.jacktu.mq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringcloudMqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudMqApplication.class, args);
}
}
3.test 中的代码
package top.jacktu.test;
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 top.jacktu.mq.Sender;
import top.jacktu.mq.SpringcloudMqApplication;
/**
* 消息接收者
* @author jacktu
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpringcloudMqApplication.class)
public class QueueTest {
@Autowired
private Sender sender;
/*
* 测试消息队列
*/
@Test
public void testMQ() {
this.sender.send("Hello RabbitMQ");
}
}
4.启动测试类,查看效果
控制台输出:
RabbitMQ管理界面: