0

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?

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
Debugger
  • 690
  • 1
  • 18
  • 41

1 Answers1

0

First, receive the message, then use

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

(locktoken is getting from the message properties.)

API reference.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • Thanks Bowman, Not sure with Implementation of Dead Letter method. If possible can you please share me some sample implementation for the same? – Debugger Jan 04 '21 at 05:40