How to push the failure messages to Azure service bus Dead Letter Queue in Spring Boot Java?

做~自己de王妃 提交于 2021-01-29 10:31:21

问题


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.

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

Reference https://spring.io/guides/gs/messaging-jms/

How to move error message to Azure dead letter queue using Java?


回答1:


First, receive the message, then use

com.microsoft.azure.servicebus -> QueueClient -> deadLetter(UUID lockToken)

(locktoken is getting from the message properties.)

API reference.



来源:https://stackoverflow.com/questions/65527256/how-to-push-the-failure-messages-to-azure-service-bus-dead-letter-queue-in-sprin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!