Search code examples
apache-camelactivemq-classic

ActiveMQ Classic's redelivery policy: message not sent to destination after maximum redeliveries is reached


I am implementing a redelivery policy for ActiveMQ Classic, and I configured the destination so my message is sent to a different queue after the maximum deliveries is reached, but my message is lost and not sent to the other queue.

RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setInitialRedeliveryDelay(5000);
redeliveryPolicy.setRedeliveryDelay(10000);
redeliveryPolicy.setBackOffMultiplier(2);
redeliveryPolicy.setMaximumRedeliveries(2);
redeliveryPolicy.setUseExponentialBackOff(true);
redeliveryPolicy.setQueue("swift_messages_out");
redeliveryPolicy.setDestination(new ActiveMQQueue("swift_messages_out.DLQ"));

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(System.getenv(SwiftWriterProperties.MQ_URI));
connectionFactory.setRedeliveryPolicy(redeliveryPolicy);

JmsTransactionManager transactionManager = new JmsTransactionManager();
transactionManager.setConnectionFactory(connectionFactory);
transactionManager.setRollbackOnCommitFailure(true);
registry.put("jmsTransactionManager", transactionManager);

CamelContext context = new DefaultCamelContext(registry);

ActiveMQComponent activemqComponent;
try {
   String brokerUrl = System.getenv(SwiftWriterProperties.MQ_URI);
   activemqComponent = ActiveMQComponent.activeMQComponent(brokerUrl);
   activemqComponent.setDeliveryPersistent(true);
   activemqComponent.setTransacted(true);
   activemqComponent.setCacheLevelName("CACHE_CONSUMER");
   activemqComponent.setTransactionManager(transactionManager);
   activemqComponent.setConnectionFactory(connectionFactory);
   context.addComponent("jms", activemqComponent);
} catch (Exception e) {
   log.error(e.getLocalizedMessage());
   throw new KantoxException(KantoxExceptionType.FATAL_ERROR, e.getLocalizedMessage());
}

Solution

  • You cannot define DLQ configurations on the client side, the broker must be configured appropriately for the behavior you are seeking. The setDestination and setQueue calls you are using is an API that exists there merely because the policy class are shared between the client and broker, however the Broker is the only user of that class that acts on destination settings. The RedeliveryPolicy when set on the client applies to each consumer on the client and controls client side redelivery options such as maximum times a redelivery will be attempted