'How to create mulitple instances of RabbitTemplate in Spring Boot?

I have a system that is currently using SimpleMessageConverter to send messages to multiple systems.

Now I want to start using Jackson2JsonMessageConverter for one of the systems.

This is to explain why I want to create more than one instance of a RabbitTemplate.

I also want to be able to keep using all configuration options provided by RabbitAutoConfiguration, so for example, if I specify a property "spring.rabbitmq.connectionTimeout" I want it to affect all instances of RabbitTemplate that will be created.

Would it be possible to extend RabbitAutoConfiguration to do this?



Solution 1:[1]

The option I found was to create a configuration class and copy part of the code from RabbitAutoConfiguration:

@Configuration
public class RabbitTemplateConfiguration {

    @Bean
    public RabbitTemplate jsonRabbitTemplate(ConnectionFactory connectionFactory, RabbitProperties properties) {
        RabbitTemplate rabbitTemplate = createRabbitTemplate(connectionFactory, properties);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean
    @Primary
    public RabbitTemplate simpleRabbitTemplate(ConnectionFactory connectionFactory, RabbitProperties properties) {
        RabbitTemplate rabbitTemplate = createRabbitTemplate(connectionFactory, properties);
        rabbitTemplate.setMessageConverter(new SimpleMessageConverter());
        return rabbitTemplate;
    }

    private RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory, RabbitProperties properties) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);

        rabbitTemplate.setMandatory(determineMandatoryFlag(properties));
        RabbitProperties.Template templateProperties = properties.getTemplate();
        RabbitProperties.Retry retryProperties = templateProperties.getRetry();
        if (retryProperties.isEnabled()) {
            rabbitTemplate.setRetryTemplate(createRetryTemplate(retryProperties));
        }
        if (templateProperties.getReceiveTimeout() != null) {
            rabbitTemplate.setReceiveTimeout(templateProperties.getReceiveTimeout());
        }
        if (templateProperties.getReplyTimeout() != null) {
            rabbitTemplate.setReplyTimeout(templateProperties.getReplyTimeout());
        }
        return rabbitTemplate;
    }

    private boolean determineMandatoryFlag(RabbitProperties properties) {
        Boolean mandatory = properties.getTemplate().getMandatory();
        return (mandatory != null ? mandatory : properties.isPublisherReturns());
    }

    private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
        RetryTemplate template = new RetryTemplate();
        SimpleRetryPolicy policy = new SimpleRetryPolicy();
        policy.setMaxAttempts(properties.getMaxAttempts());
        template.setRetryPolicy(policy);
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(properties.getInitialInterval());
        backOffPolicy.setMultiplier(properties.getMultiplier());
        backOffPolicy.setMaxInterval(properties.getMaxInterval());
        template.setBackOffPolicy(backOffPolicy);
        return template;
    }
}

Solution 2:[2]

A simpler way is to create your bean using the RabbitTemplateConfigurer with all pre-populated settings from Spring Boot.

    @Bean
    public RabbitTemplate myRabbitTemplate(RabbitTemplateConfigurer configurer,
                                       ConnectionFactory connectionFactory) {
        this.configurer = configurer;
        this.connectionFactory = connectionFactory;
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        this.configurer.configure(rabbitTemplate, this.connectionFactory);
        rabbitTemplate.setMessageConverter(...);
        return rabbitTemplate;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Constantino Cronemberger
Solution 2 k_o_