package com.lizhengi.work.demo1;
import com.lizhengi.hello.demo2.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.testng.annotations.Test;
import java.io.IOException;
/\*\*
\* @author liziheng
\* @version 1.0.0
\* @description Rabbit 消息分发模型 生产者
\* @date 2022-12-23 11:32 上午
\*\*/
public class Producer {
/\*\*
\* 消息生产
\*
\* @throws IOException IOException
\*/
@Test
public void testSendMessage() throws IOException {
// 获取连接对象
Connection connection = ConnectionUtil.getConnection();
// 获取通道
assert connection != null;
Channel channel = connection.createChannel();
// 通过通道声明队列
channel.queueDeclare("WorkQueue", true, false, false, null);
// 生产消息
for (int i = 0; i < 100; i++) {
channel.basicPublish("", "WorkQueue", null, ("Hello RabbitMQ " + i).getBytes());
}
// 资源关闭
ConnectionUtil.closeConnectionAndChanel(channel, connection);
}
}
4、消费者-1 实现
package com.lizhengi.work.demo1;
import com.rabbitmq.client.\*;
import java.io.IOException;
/\*\*
\* @author liziheng
\* @version 1.0.0
\* @description Rabbit 消息分发模型 消费者-1
\* @date 2022-12-23 11:32 上午
\*\*/
public class Customer1 {
public static void main(String[] msg) throws IOException {
Connection connection = ConnectionUtil.getConnection();
assert connection != null;
Channel channel = connection.createChannel();
channel.queueDeclare("WorkQueue", true, false, false, null);
channel.basicConsume("WorkQueue", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
System.out.println("消费者1:" + new String(body));
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
5、消费者-2 实现
package com.lizhengi.work.demo1;
import com.rabbitmq.client.\*;
import java.io.IOException;
/\*\*
\* @author liziheng
\* @version 1.0.0
\* @description Rabbit 消息分发模型 消费者-2
\* @date 2022-12-23 11:32 上午
\*\*/
public class Customer2 {
public static void main(String[] msg) throws IOException {
Connection connection = ConnectionUtil.getConnection();
assert connection != null;
Channel channel = connection.createChannel();
channel.queueDeclare("WorkQueue", true, false, false, null);
channel.basicConsume("WorkQueue", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
System.out.println("消费者2:" + new String(body));
}
});
}
}
6、消息队列的循环机制
运行上面的消费者与生产者,观察消费者打印:
消费者1:Hello RabbitMQ 1
消费者1:Hello RabbitMQ 3
消费者1:Hello RabbitMQ 5
消费者1:Hello RabbitMQ 7
消费者1:Hello RabbitMQ 9
消费者2:Hello RabbitMQ 0
消费者2:Hello RabbitMQ 2
消费者2:Hello RabbitMQ 4
消费者2:Hello RabbitMQ 6
消费者2:Hello RabbitMQ 8
可以观察到,两个消费者依次打印了队列中的消息,哪怕是在其中一个加上 Thread.sleep(1000);
依然是这种循环机制!
三、RabbitMQ 手动消息确认
1、消费者-1 实现
package com.lizhengi.work.demo2;
import com.lizhengi.work.demo1.ConnectionUtil;
import com.rabbitmq.client.\*;
import java.io.IOException;
/\*\*
\* @author liziheng
\* @version 1.0.0
\* @description Rabbit 消息分发模型 消费者-1
\* @date 2022-12-23 11:32 上午
\*\*/
public class Customer1 {
public static void main(String[] msg) throws IOException {
Connection connection = ConnectionUtil.getConnection();
assert connection != null;
Channel channel = connection.createChannel();
// 设置每次只能消费一个消息
channel.basicQos(1);
channel.queueDeclare("WorkQueue", true, false, false, null);
// 参数2:自动消息确认关闭
channel.basicConsume("WorkQueue", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("消费者1:" + new String(body));
/\* 手动消息确认
\* 参数1:确认队列中的那个具体消息
\* 参数2:是否开启多个消息同时确认
\*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
});
}
}
2、消费者-2 实现
package com.lizhengi.work.demo2;
import com.lizhengi.work.demo1.ConnectionUtil;
import com.rabbitmq.client.\*;
import java.io.IOException;
/\*\*
\* @author liziheng
\* @version 1.0.0
\* @description Rabbit 消息分发模型 消费者-2
\* @date 2022-12-23 11:32 上午
\*\*/
public class Customer2 {
public static void main(String[] msg) throws IOException {
Connection connection = ConnectionUtil.getConnection();
assert connection != null;
Channel channel = connection.createChannel();
//设置每次只能消费一个消息
channel.basicQos(1);
channel.queueDeclare("WorkQueue", true, false, false, null);
channel.basicConsume("WorkQueue", false, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者2:" + new String(body));
channel.basicAck(envelope.getDeliveryTag(), false);
}
});
}
}
3、实现能者多劳
运行上面的消费者与生产者,观察消费者打印:
消费者1:Hello RabbitMQ 0
消费者1:Hello RabbitMQ 2
消费者1:Hello RabbitMQ 3
消费者1:Hello RabbitMQ 4
消费者1:Hello RabbitMQ 5
消费者1:Hello RabbitMQ 6
消费者1:Hello RabbitMQ 7…
消费者2:Hello RabbitMQ 1
消费者2:Hello RabbitMQ 64
消费者2:Hello RabbitMQ 76
消费者2:Hello RabbitMQ 89
可以观察到,消费者1开启了手动消费机制后进行了更大比重消息数量的消费!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
.(img-eqAcAe5q-1714420360600)]
[外链图片转存中…(img-IZA2ugYd-1714420360600)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!