1.导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.编写消息生产者
package cn.com.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@SpringBootTest
public class RabbitPushTest {
@Test
public void pushmessage() throws IOException, TimeoutException {
/*/
- 建立连接
- 创建Channel
- 声明队列
- 发送消息
- 关闭连接和channel
*/
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 2.设置连接参数
factory.setHost(ip);
factory.setPort(5672);
factory.setUsername("test");
factory.setPassword("test");
factory.setVirtualHost("test");
// 3.创建连接
Connection connection = factory.newConnection();
// 4.创建信道
Channel channel = connection.createChannel();
// 5.创建队列
/*
void queueDeclareNoWait(String queue, boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
参数说明
1.String queue : 声明一个队列名称
2.boolean durable : 是否持久化(是否持久化到硬盘中)
3.boolean exclusive : 声明独占队列
4.boolean autoDelete : 是否自动删除
5. Map<String, Object> arguments : 队列的其他属性
*/
String queueName = "rabbitMQ";
channel.queueDeclare(queueName,false,false,false,null);
// 6.发送消息
String message = "hello, rabbitmq!";
channel.basicPublish("",queueName,null,message.getBytes());
// 7.关闭连接
channel.close();
connection.close();
}
}
3.编写消费者
package cn.com.rabbitmq;
import com.rabbitmq.client.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@SpringBootTest
public class consumerTest {
@Test
public void pullMessage() throws IOException, TimeoutException {
/*
- 建立连接
- 创建Channel
- 声明队列
- 订阅消息
*/
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 2.设置连接参数
factory.setHost(ip);
factory.setPort(5672);
factory.setUsername("test");
factory.setPassword("test");
factory.setVirtualHost("test");
Connection connection = factory.newConnection();
// 3.创建channel
Channel channel = connection.createChannel();
// 4.声明队列
String queueName = "rabbitMQ";
channel.queueDeclare(queueName,false,false,false,null);
// 5.持续订阅消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
// 5.处理消息
String message = new String(body);
System.out.println("接收到消息:【" + message + "】");
}
};
channel.basicConsume(queueName,true,defaultConsumer);
}
}