}
return null;
}
/\*\*
* 关闭通道和连接 工具方法
*
* @param channel Channel
* @param connection Connection
*/
public static void closeConnectionAndChanel(Channel channel, Connection connection) {
try {
if (channel != null) {
channel.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
##### 3、生产者实现
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
![img](https://img-blog.csdnimg.cn/img_convert/89b628eaab97f5c6dfba7d21818d4ca7.png)
![img](https://img-blog.csdnimg.cn/img_convert/c6f5c7722fa7fa0477094a38c646e7d9.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
.(img-3QVBMDXJ-1714821191558)]
[外链图片转存中...(img-RY3v1fAC-1714821191559)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**