当前位置:网站首页>Message rejected MQ
Message rejected MQ
2022-07-27 03:21:00 【A light wind and light clouds】
producer
/**
* Message rejected
*/
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);
// This message is used as the number limit of queues
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(" Producer sends message "+message);
}
}
}consumer :
/**
* Message rejected
*/
public class Consumer0001 {
// General switch
private static final String NORMAL_EXCHANGE="normal_exchange";
// Dead letter switch
private static final String DEAD_EXCHANGE="dead_exchange";
public static void main(String[] args) throws Exception{
Channel channel = untils.getChannel();
// Declare dead letter switch , The type is direct
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);
// Declare dead letter queue
String deadQueue="dead_queue";
channel.queueDeclare(deadQueue,false,false,false,null);
// Dead letter queue binding exchange and routingKey value
channel.queueBind(deadQueue,DEAD_EXCHANGE,"lisi");
// The normal queue is bound to the dead letter queue
Map<String,Object> params=new HashMap<>();
// Set the dead letter switch in the normal queue , Parameters key It's a fixed value
params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
// Set dead letter in normal queue routing-key, Parameters key It's a fixed value
params.put("x-dead-letter-routing-key", "lisi");
// Maximum limit length of normal queue settings
params.put("x-max-length",6);
System.out.println(" Waiting to receive message ....");
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 Reject ");
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
}
else
{
System.out.println("01 Message received "+s);
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(normalQueue,false,deliverCallback,consumerTag -> {});
}
}result :



边栏推荐
猜你喜欢
![[learning notes, dog learning C] string + memory function](/img/53/86e529dcc8a5a6b682e9485e7c152a.png)
[learning notes, dog learning C] string + memory function

Social wechat applet of fanzhihu forum community

Bulk copy baby upload prompt garbled, how to solve?

Details of impala implementation plan

带你了解什么是 Web3.0

MySQL:互联网公司常用分库分表方案汇总

185. 部门工资前三高的所有员工(必会)

Alibaba cloud technology expert Yang Zeqiang: Construction of observability on elastic computing cloud

Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name

Pytoch loss function summary
随机推荐
5、 MFC view windows and documents
Common events of window objects
身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
深入理解Mysql索引底层数据结构与算法
客户案例 | 关注老年用户体验,银行APP适老化改造要避虚就实
Comprehensive care analysis lyriq Ruige battery safety design
图解 SQL,这也太形象了吧!
HCIP第十三天笔记
Win10/win11 lossless expansion of C disk space, cross disk consolidation of C and e disks
二叉树(DAY 82)
1.28亿美元!芬兰量子计算公司IQM获世界基金支持
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
关于OpenFeign的源码分析
vector 转 svg 方法
Byte side: can TCP and UDP use the same port?
It's too strong. An annotation handles the data desensitization returned by the interface
“date: write error: No space left on device”解决
抖音服务器带宽有多大,才能供上亿人同时刷?
Redis四大特殊数据类型的学习和理解