如何在Spring Boot中创建RabbitTemplate的多个实例?

时间:2018-06-12 01:03:59

标签: spring-boot spring-rabbit

我有一个系统,当前正在使用SimpleMessageConverter向多个系统发送消息。

现在我想开始在其中一个系统中使用Jackson2JsonMessageConverter。

这是为了解释为什么我要创建一个RabbitTemplate的多个实例。

我还希望能够继续使用RabbitAutoConfiguration提供的所有配置选项,例如,如果我指定了一个属性" spring.rabbitmq.connectionTimeout"我希望它能影响将要创建的所有RabbitTemplate实例。

是否可以扩展RabbitAutoConfiguration来执行此操作?

1 个答案:

答案 0 :(得分:2)

我找到的选项是创建配置类并从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;
    }
}