当前位置:网站首页>消息被拒MQ
消息被拒MQ
2022-07-27 01:16:00 【一个风轻云淡】
生产者
/**
* 消息被拒的情况
*/
public class Produce0001 {
private static final String NORMAL_EXCHANGE="normal_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//该消息用作队列的个数限制
for(int i=0;i<10;i++)
{
String message="info"+i;
channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",null,message.getBytes(StandardCharsets.UTF_8));
System.out.println("生产者发送消息"+message);
}
}
}消费者:
/**
* 消息被拒的情况
*/
public class Consumer0001 {
//普通交换机
private static final String NORMAL_EXCHANGE="normal_exchange";
//死信交换机
private static final String DEAD_EXCHANGE="dead_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
//声明死信交换机,类型为direct
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
//声明死信队列
String deadQueue="dead_queue";
channel.queueDeclare(deadQueue,false,false,false,null);
//死信队列绑定交换和routingKey值
channel.queueBind(deadQueue,DEAD_EXCHANGE,"lisi");
//正常队列绑定死信队列
Map<String,Object> params=new HashMap<>();
//正常队列设置死信交换机,参数key是固定值
params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
//正常队列设置死信routing-key,参数key是固定值
params.put("x-dead-letter-routing-key", "lisi");
//正常队列设置的最大限制长度
params.put("x-max-length",6);
System.out.println("等待接收消息....");
String normalQueue="normal_queue";
channel.queueDeclare(normalQueue,false,false,false,params);
channel.queueBind(normalQueue,NORMAL_EXCHANGE,"zhangsan");
DeliverCallback deliverCallback=(consumerTag, message) -> {
String s = new String(message.getBody(), StandardCharsets.UTF_8);
if (s.equals("info5"))
{
System.out.println("info5拒接");
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
}
else
{
System.out.println("01接收到消息"+s);
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(normalQueue,false,deliverCallback,consumerTag -> {});
}
}结果:



边栏推荐
- 食物链(DAY 79)
- [栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
- 积分发放带给商家的两个帮助
- QT编译出来的exe以管理员权限启动
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Portraiture5 new and upgraded leather filter plug-in artifact
- Worthington木瓜蛋白酶解离系统解决方案
- After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized
- Ten thousand words long text, take you to understand the kubernetes network model
- 二叉树(北京邮电大学机试题)(DAY 85)
猜你喜欢

Worthington果胶酶的特性及测定方案

1.28亿美元!芬兰量子计算公司IQM获世界基金支持

5、 MFC view windows and documents

The EXE compiled by QT is started with administrator privileges

一体式水利视频监控站 遥测终端视频图像水位水质水量流速监测

Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion

Baidu cloud face recognition

HCIP第十四天笔记

After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized

HCIP第十三天笔记
随机推荐
177. 第N高的薪水(简单)
Source code analysis of warning notification for skywalking series learning
Plato Farm全新玩法,套利ePLATO稳获超高收益
CAS部署使用以及登录成功跳转地址
A test class understands beanutils.copyproperties
最低票价(DAY 80)
cocos小游戏实战-04-碰撞检测与NPC渲染
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
What did kubedmin do?
食物链(DAY 79)
[SQL简单题] LeetCode 627. 变更性别
shell(38) : ssh端口转发
HCIP第十四天笔记
周全的照护 解析LYRIQ锐歌电池安全设计
Call jshaman's Web API interface to realize JS code encryption.
“date: write error: No space left on device”解决
Integrated water conservancy video monitoring station telemetry terminal video image water level water quality water quantity flow velocity monitoring
Hcip 13th day notes
在线问题反馈模块实战(十五):实现在线更新反馈状态功能
{“errcode“:44001,“errmsg“:“empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.