Inject RedeliveryPolicy to Spring boot ActiveMQ

Viewed 5335

I'm using spring-boot-starter-activemq with PoolFactory. Everything works great and it's easy to configure via application.yaml, but I cannot find correct way to inject my custom RedeliveryPolicy. So the question is how can I do that?

2 Answers

I have tried the answer from @Iulian Rosca but it does not work for latest version of spring-boot-starter-activemq (I'm using version 2.2.0.RELEASE).

Spring provides many other implementation classes for ConnectionFactory, such as CachingConnectionFactory, JmsPooledConnectionFactory,... and they are not inherited to each others. Therefore, the above solution need to refactor with many class type.

I discover Spring provide a functional interface ActiveMQConnectionFactoryCustomizer, which will be injected when create ActiveMQ Connection Factory, whichever class no matter. It will customize connection factory class, you can set any properties, not only redelivery policy.

Here is my sample code:

@Configuration
public class RedeliveryCofiguration {

    @Bean
    public ActiveMQConnectionFactoryCustomizer configureRedeliveryPolicy() {
        return connectionFactory ->
        {
            RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
            // configure redelivery policy
            connectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        };
    }
}
Related