|
34 | 34 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
35 | 35 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
36 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
| 37 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
37 | 38 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
38 | 39 | import org.springframework.context.annotation.Bean;
|
39 | 40 | import org.springframework.context.annotation.Configuration;
|
|
84 | 85 | @Import(RabbitAnnotationDrivenConfiguration.class)
|
85 | 86 | public class RabbitAutoConfiguration {
|
86 | 87 |
|
87 |
| - @Bean |
88 |
| - @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true) |
89 |
| - @ConditionalOnMissingBean(AmqpAdmin.class) |
90 |
| - public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) { |
91 |
| - return new RabbitAdmin(connectionFactory); |
92 |
| - } |
93 |
| - |
94 |
| - @Autowired |
95 |
| - private ConnectionFactory connectionFactory; |
96 |
| - |
97 |
| - @Autowired |
98 |
| - private ObjectProvider<MessageConverter> messageConverter; |
99 |
| - |
100 |
| - @Bean |
101 |
| - @ConditionalOnMissingBean(RabbitTemplate.class) |
102 |
| - public RabbitTemplate rabbitTemplate(RabbitProperties config) { |
103 |
| - RabbitTemplate rabbitTemplate = new RabbitTemplate(this.connectionFactory); |
104 |
| - MessageConverter messageConverter = this.messageConverter.getIfUnique(); |
105 |
| - if (messageConverter != null) { |
106 |
| - rabbitTemplate.setMessageConverter(messageConverter); |
107 |
| - } |
108 |
| - Template template = config.getTemplate(); |
109 |
| - Retry retry = template.getRetry(); |
110 |
| - if (retry.isEnabled()) { |
111 |
| - RetryTemplate retryTemplate = new RetryTemplate(); |
112 |
| - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); |
113 |
| - retryPolicy.setMaxAttempts(retry.getMaxAttempts()); |
114 |
| - retryTemplate.setRetryPolicy(retryPolicy); |
115 |
| - ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); |
116 |
| - backOffPolicy.setInitialInterval(retry.getInitialInterval()); |
117 |
| - backOffPolicy.setMultiplier(retry.getMultiplier()); |
118 |
| - backOffPolicy.setMaxInterval(retry.getMaxInterval()); |
119 |
| - retryTemplate.setBackOffPolicy(backOffPolicy); |
120 |
| - rabbitTemplate.setRetryTemplate(retryTemplate); |
121 |
| - } |
122 |
| - if (template.getReceiveTimeout() != null) { |
123 |
| - rabbitTemplate.setReceiveTimeout(template.getReceiveTimeout()); |
124 |
| - } |
125 |
| - if (template.getReplyTimeout() != null) { |
126 |
| - rabbitTemplate.setReplyTimeout(template.getReplyTimeout()); |
127 |
| - } |
128 |
| - return rabbitTemplate; |
129 |
| - } |
130 |
| - |
131 | 88 | @Configuration
|
132 | 89 | @ConditionalOnMissingBean(ConnectionFactory.class)
|
133 | 90 | protected static class RabbitConnectionFactoryCreator {
|
@@ -185,11 +142,67 @@ public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
|
185 | 142 |
|
186 | 143 | }
|
187 | 144 |
|
| 145 | + @Configuration |
| 146 | + @Import(RabbitConnectionFactoryCreator.class) |
| 147 | + protected static class RabbitTemplateConfiguration { |
| 148 | + |
| 149 | + @Autowired |
| 150 | + private ObjectProvider<MessageConverter> messageConverter; |
| 151 | + |
| 152 | + @Autowired |
| 153 | + private RabbitProperties properties; |
| 154 | + |
| 155 | + @Bean |
| 156 | + @ConditionalOnSingleCandidate(ConnectionFactory.class) |
| 157 | + @ConditionalOnMissingBean(RabbitTemplate.class) |
| 158 | + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { |
| 159 | + RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); |
| 160 | + MessageConverter messageConverter = this.messageConverter.getIfUnique(); |
| 161 | + if (messageConverter != null) { |
| 162 | + rabbitTemplate.setMessageConverter(messageConverter); |
| 163 | + } |
| 164 | + Template template = this.properties.getTemplate(); |
| 165 | + Retry retry = template.getRetry(); |
| 166 | + if (retry.isEnabled()) { |
| 167 | + RetryTemplate retryTemplate = new RetryTemplate(); |
| 168 | + SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); |
| 169 | + retryPolicy.setMaxAttempts(retry.getMaxAttempts()); |
| 170 | + retryTemplate.setRetryPolicy(retryPolicy); |
| 171 | + ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); |
| 172 | + backOffPolicy.setInitialInterval(retry.getInitialInterval()); |
| 173 | + backOffPolicy.setMultiplier(retry.getMultiplier()); |
| 174 | + backOffPolicy.setMaxInterval(retry.getMaxInterval()); |
| 175 | + retryTemplate.setBackOffPolicy(backOffPolicy); |
| 176 | + rabbitTemplate.setRetryTemplate(retryTemplate); |
| 177 | + } |
| 178 | + if (template.getReceiveTimeout() != null) { |
| 179 | + rabbitTemplate.setReceiveTimeout(template.getReceiveTimeout()); |
| 180 | + } |
| 181 | + if (template.getReplyTimeout() != null) { |
| 182 | + rabbitTemplate.setReplyTimeout(template.getReplyTimeout()); |
| 183 | + } |
| 184 | + return rabbitTemplate; |
| 185 | + } |
| 186 | + |
| 187 | + @Bean |
| 188 | + @ConditionalOnSingleCandidate(ConnectionFactory.class) |
| 189 | + @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true) |
| 190 | + @ConditionalOnMissingBean(AmqpAdmin.class) |
| 191 | + public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) { |
| 192 | + return new RabbitAdmin(connectionFactory); |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + @Configuration |
188 | 199 | @ConditionalOnClass(RabbitMessagingTemplate.class)
|
189 | 200 | @ConditionalOnMissingBean(RabbitMessagingTemplate.class)
|
| 201 | + @Import(RabbitTemplateConfiguration.class) |
190 | 202 | protected static class MessagingTemplateConfiguration {
|
191 | 203 |
|
192 | 204 | @Bean
|
| 205 | + @ConditionalOnSingleCandidate(RabbitTemplate.class) |
193 | 206 | public RabbitMessagingTemplate rabbitMessagingTemplate(
|
194 | 207 | RabbitTemplate rabbitTemplate) {
|
195 | 208 | return new RabbitMessagingTemplate(rabbitTemplate);
|
|
0 commit comments