0
votes

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("[email protected]", "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

1 Answers

0
votes

First, receive the message, then use

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

(locktoken is getting from the message properties.)

API reference.