当前位置:网站首页>10. Fallback message
10. Fallback message
2022-07-29 04:15:00 【Machoul】
rabbitmq Fallback message
mandatory Parameters
When only the producer confirmation mechanism is turned on , After the switch receives the message , A confirmation message will be sent directly to the message producer , If the message is found to be non routable , Then the message will be discarded directly , At this time, the producer does not know that the message is discarded .
Can be set by mandatory Parameter returns the message to the producer when the destination cannot be reached during message delivery .
Case presentation
add to application.properties The configuration file
server.port=8080
spring.rabbitmq.host=172.16.140.133
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring.rabbitmq.publisher-confirm-type=correlated
Add configuration class
/** * Fallback message configuration class */
@Configuration
public class ConfirmConfig {
public static final String CONFIRM_EXCHANGE = "confirm.exchange";
public static final String CONFIRM_QUEUE_NAME = "confirm.queue";
@Bean("confirmExchange")
public DirectExchange confirmExchange(){
return new DirectExchange(CONFIRM_EXCHANGE);
}
@Bean("confirmQueue")
public Queue confirmQueue(){
return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
}
@Bean
public Binding queueBinding(@Qualifier("confirmQueue") Queue queue,@Qualifier("confirmExchange") DirectExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("key1");
}
}
Add callback interface
/** * Message fallback callback interface */
@Component
@Slf4j
public class MyCallBack implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
String id = correlationData != null ? correlationData.getId():"";
if (ack){
log.info(" The switch has received id by :{} The news of ",id);
}else {
log.info(" The switch hasn't received id by :{} The news of , For some reason :{}",id ,cause);
}
}
@Override
public void returnedMessage(ReturnedMessage returned) {
log.info(" news :{} Returned by server , Reasons for return :{}, The switch is :{}, route key:{}",new String(returned.getMessage().getBody()),returned.getReplyText(),returned.getExchange(),returned.getRoutingKey());
}
}
Add consumer
/** * Message consumer */
@Component
@Slf4j
public class ConfirmConsumer {
public static final String CONFIRM_QUEUE_NAME = "confirm.queue";
@RabbitListener(queues = CONFIRM_QUEUE_NAME)
public void receiveMsg(Message message){
String msg = new String(message.getBody());
log.info(" Received queue confirm.queue news :{}",msg);
}
}
Add producers
/** * Message producer */
@RestController
@RequestMapping("/confirm")
@Slf4j
public class ProducerController {
public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private MyCallBack myCallBack;
/** * initialization rabbitTemplate */
@PostConstruct
public void init(){
rabbitTemplate.setConfirmCallback(myCallBack);
/** * true: When the switch cannot route messages , The message will be returned to the producer * false: If a message is found to be unable to route , Throw away */
rabbitTemplate.setMandatory(true);
rabbitTemplate.setReturnsCallback(myCallBack);
}
@GetMapping("sendMessage/{message}")
public void sendMessage(@PathVariable String message){
CorrelationData correlationData = new CorrelationData("1");
String routingKey = "key1";
rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE_NAME,routingKey,message+routingKey,correlationData);
CorrelationData correlationData2 = new CorrelationData("2");
String routingKey2 = "key2";
rabbitTemplate.convertAndSend(CONFIRM_EXCHANGE_NAME,routingKey2,message+routingKey,correlationData2);
log.info(" Send message content :{}",message);
}
}
result

边栏推荐
- 安装ros的laser_scan_matche库所遇到的问题(一)
- Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
- There is a special cryptology language called asn.1
- 店铺排名问题,如何解决?
- Design of environment detection system based on STM32 and Alibaba cloud
- 有一种密码学专用语言叫做ASN.1
- C语言:联合体知识点总结
- Methods of using multiple deformations on an element
- (.*?) regular expression
- Multi rotor six axis hardware selection
猜你喜欢

Beginner: array & String

VScode连接ssh遇到的问题

Value transmission and address transmission of C language, pointer of pointer

Record of problems encountered in ROS learning

Fu Yingna: Yuan universe is the new generation of Internet!

Note: restframe work records many to one tables, how to serialize in that table (reverse query)

从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)

Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment

不会就坚持68天吧 狒狒吃香蕉

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
随机推荐
AssertionError(“Torch not compiled with CUDA enabled“)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
Shielding ODBC load balancing mode in gbase 8A special scenarios?
一个公司的面试笔记
Class starts! See how smardaten decomposes complex business scenarios
MPU6050
Leftmost prefix principle of index
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
LCA 板子
HC06 HC05 BT
不会就坚持64天吧 查找插入位置
Differences and principles of bio, NiO and AIO
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
Beginner: array & String
Change the value of the argument by address through malloc and pointer
2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
Code or script to speed up the video playback of video websites
顺序表和链表
MPU6050
Note: restframe work records many to one tables, how to serialize in that table (reverse query)