6

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?

2 Answers 2

3

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;
    }
}
1

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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.