<!-- 指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.13.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
package cn.cnyasin.rabbit.utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitMqUtils {
public static Channel getChannel() throws Exception {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 配置
factory.setHost("192.168.3.202");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("123456");
factory.setVirtualHost("/");
// 获取连接
Connection connection = factory.newConnection();
// 获取信道
Channel channel = connection.createChannel();
return channel;
}
}
package cn.cnyasin.rabbit.worker;
import cn.cnyasin.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
public class Init {
// 交换机名
public static final String EXCHANGE_NAME = "exchange01";
// 队列名
public static final String QUEUE_NAME = "queue01";
// 路由key
public static final String ROUTING_KEY = "routing01";
public static void main(String[] args) throws Exception {
// 获取信道
Channel channel = RabbitMqUtils.getChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 绑定队列、交换机、路由key
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
System.out.println("初始化成功。。。");
System.exit(0);
}
}
package cn.cnyasin.rabbit.worker;
import cn.cnyasin.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Delivery;
/**
* 工作队列01(消费者)
*/
public class Worker01 {
// 定义本工作队列要处理的队列名字
public static final String QUEUE_NAME = "queue01";
public static void main(String[] args) throws Exception {
// 获取信道
Channel channel = RabbitMqUtils.getChannel();
System.out.println("[*] 工作队列01正在等待接收消息。。。");
// 处理消息
channel.basicConsume(
// 队列名
QUEUE_NAME,
// 自动应答
true,
// 成功处理消息回调
(String consumerTag, Delivery message) -> {
// TODO 业务逻辑代码在这里
System.out.println(" [*] 成功处理消息:" + new String(message.getBody()));
},
// 处理消息失败回调
(String consumerTag) -> {
System.out.println("处理消息失败");
}
);
}
}
package cn.cnyasin.rabbit.worker;
import cn.cnyasin.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import java.util.Scanner;
public class Task {
// 交换机名
public static final String EXCHANGE_NAME = "exchange01";
// 路由key
public static final String ROUTING_KEY = "routing01";
public static void main(String[] args) throws Exception {
// 获取信道
Channel channel = RabbitMqUtils.getChannel();
// 接收控制台输入
Scanner scanner = new Scanner(System.in);
System.out.println("[*] 等待控制台输入消息内容。。。");
while (scanner.hasNext()) {
String input = scanner.next();
if (input.equals("exit")) {
break;
}
// 发送消息
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, input.getBytes());
System.out.println("[*] 消息发送成功。。。");
}
channel.close();
System.out.println("[*] 用户退出。。。");
System.exit(0);
}
}
package cn.cnyasin.rabbit.ack;
import cn.cnyasin.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Delivery;
/**
* 工作队列01(消费者)
*/
public class Worker01 {
// 定义本工作队列要处理的队列名字
public static final String QUEUE_NAME = "queue_ack";
public static void main(String[] args) throws Exception {
// 获取信道
Channel channel = RabbitMqUtils.getChannel();
System.out.println("[*] 工作队列01正在等待接收消息。。。");
// 处理消息
channel.basicConsume(
// 队列名
QUEUE_NAME,
// 自动应答
false,
// 成功处理消息回调
(String consumerTag, Delivery message) -> {
try {
// 沉睡1秒
Thread.sleep(1000 * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// TODO 业务逻辑代码在这里
System.out.println(" [*] 工作队列01成功处理消息:" + new String(message.getBody()));
// TODO 手动应答
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
},
// 处理消息失败回调
(String consumerTag) -> {
System.out.println("处理消息失败");
}
);
}
}
package cn.cnyasin.rabbit.ack;
import cn.cnyasin.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Delivery;
/**
* 工作队列02(消费者)
*/
public class Worker02 {
// 定义本工作队列要处理的队列名字
public static final String QUEUE_NAME = "queue_ack";
public static void main(String[] args) throws Exception {
// 获取信道
Channel channel = RabbitMqUtils.getChannel();
System.out.println("[*] 工作队列02正在等待接收消息。。。");
// 处理消息
channel.basicConsume(
// 队列名
QUEUE_NAME,
// 自动应答
true,
// 成功处理消息回调
(String consumerTag, Delivery message) -> {
try {
// 沉睡10秒
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// TODO 业务逻辑代码在这里
System.out.println(" [*] 工作队列02成功处理消息:" + new String(message.getBody()));
// TODO 手动应答
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
},
// 处理消息失败回调
(String consumerTag) -> {
System.out.println("处理消息失败");
}
);
}
}
// 消息持久化
AMQP.BasicProperties properties = MessageProperties.PERSISTENT_TEXT_PLAIN;
// 发送消息
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, properties, input.getBytes());
说明
核心代码
// 设置不公平分发
int prefetchCount = 1; // 预取值
channel.basicQos(prefetchCount);