如何在Spring Boot Java中将故障消息推送到Azure服务总线Dead Letter Queue? [英] How to push the failure messages to Azure service bus Dead Letter Queue in Spring Boot Java?
问题描述
我正在使用JMS侦听器从Azure主题中读取消息并处理该消息,一旦过程完成,我将推回另一个主题.我在spring文档的帮助下成功完成了功能.现在,我需要处理失败消息-错误处理程序.如果我们在阅读或处理消息时遇到异常,这意味着我需要将其推送到死信队列"中.
I'm using JMS Listener to read the message from the Azure topic and processing the message, Once the process completed I'm pushing back to another topic. I successfully completed functionality with help of spring documentation. Now I need to handle failure messages - Error handler. In case If we have an exception while reading or processing the message means I need to push it to the Dead letter Queue.
我根据Spring文档尝试过的示例代码.
Sample code I tried based on Spring Documentation.
@Component
public class Receiver {
@JmsListener(destination = "mailbox", containerFactory = "myFactory")
public void receiveMessage(Email email) {
System.out.println("Received <" + email + ">");
}
}
@SpringBootApplication
@EnableJms
public class Application {
@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
// You could still override some of Boot's default if necessary.
return factory;
}
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
public static void main(String[] args) {
// Launch the application
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
// Send a message with a POJO - the template reuse the message converter
System.out.println("Sending an email message.");
jmsTemplate.convertAndSend("mailbox", new Email("info@example.com", "Hello"));
}
}
任何人,请为此提供建议
Anyone, please advise me on this
参考 https://spring.io/guides/gs/messaging-jms/
推荐答案
首先,接收消息,然后使用
First, receive the message, then use
com.microsoft.azure.servicebus->QueueClient->deadLetter(UUID lockToken)
(locktoken从消息属性中获取.)
(locktoken is getting from the message properties.)