Azure service bus dead letter spring JMS

Viewed 110

I have an azure topic and I need to send a message to the azure service bus dead letter if we cannot process the event for any reason. I'm using the following dependency:

<dependency>
  <groupId>com.azure.spring</groupId>
  <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
  <version>${azure.jms.version}</version>
</dependency>

So, I've configured a JmsListenerConnectionFactory to receive the event, and I've configured a JmsTemplate bean to publish to a second topic. The above logic works fine and I decided to configure a second JmsTemplate bean to publish to the dead letter queue:

private ServiceBusJmsConnectionFactory buildJmsConnectionFactory(String connectionString,
            String topicName) 

{
        var replacedConnectionString = connectionString.replace("sb://", "");
        var connectionFactory = new ServiceBusJmsConnectionFactory(replacedConnectionString);
        var serviceBusConnectionString = new ServiceBusConnectionString(replacedConnectionString);
        var remoteUri = String.format(AMQP_URI_FORMAT, serviceBusConnectionString.getEndpointUri(), 100000);
        connectionFactory.setRemoteURI(remoteUri);
        connectionFactory.setClientID(topicName);
        connectionFactory.setUsername(serviceBusConnectionString.getSharedAccessKeyName());
        connectionFactory.setPassword(serviceBusConnectionString.getSharedAccessKey());
        connectionFactory.setCloseTimeout(600000L);
        return connectionFactory;
    }
@Bean
    public ConnectionFactory deadLetterConnectionFactory() {
        var connectionFactory = buildJmsConnectionFactory(listenerConnectionString, topicName);
        var cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);
        cachingConnectionFactory.setCacheProducers(false);
        return cachingConnectionFactory;
    }
@Bean
    public JmsTemplate deadLetterJmsTemplate(@Qualifier("deadLetterPriceListConnectionFactory")
                                              ConnectionFactory connectionFactory) {
        var template = new JmsTemplate(connectionFactory);
        template.setPubSubDomain(true);
        template.setDefaultDestinationName("topicName/Subscriptions/subscriptionName/$deadletterqueue");
return template;

In a second class I just call the convertAndSend method:

deadLetterJmsTemplate.convertAndSend("test destination dead letter");

According with the azure documentation, I suppose that I need to put the destination name with that format, but I'm getting an error that I cannot create a sender for a subscription or a timeout error.

As a quick note, a subscription belongs to the topic.

So my question is: Is this assumption right? Or I need to put another configuration/implementation to send to the dead letter?

Thanks in advance.

0 Answers
Related