16
16
17
17
package org .springframework .boot .autoconfigure .amqp ;
18
18
19
+ import java .time .Duration ;
20
+
19
21
import com .rabbitmq .client .Channel ;
20
22
21
23
import org .springframework .amqp .core .AmqpAdmin ;
33
35
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
34
36
import org .springframework .boot .autoconfigure .condition .ConditionalOnSingleCandidate ;
35
37
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
38
+ import org .springframework .boot .context .properties .PropertyMapper ;
36
39
import org .springframework .context .annotation .Bean ;
37
40
import org .springframework .context .annotation .Configuration ;
38
41
import org .springframework .context .annotation .Import ;
76
79
* @author Josh Long
77
80
* @author Stephane Nicoll
78
81
* @author Gary Russell
82
+ * @author Phillip Webb
79
83
*/
80
84
@ Configuration
81
85
@ ConditionalOnClass ({ RabbitTemplate .class , Channel .class })
@@ -88,66 +92,55 @@ public class RabbitAutoConfiguration {
88
92
protected static class RabbitConnectionFactoryCreator {
89
93
90
94
@ Bean
91
- public CachingConnectionFactory rabbitConnectionFactory (RabbitProperties config )
92
- throws Exception {
95
+ public CachingConnectionFactory rabbitConnectionFactory (
96
+ RabbitProperties properties ) throws Exception {
97
+ PropertyMapper map = PropertyMapper .get ();
98
+ CachingConnectionFactory factory = new CachingConnectionFactory (
99
+ getRabbitConnectionFactoryBean (properties ).getObject ());
100
+ map .from (properties ::determineAddresses ).to (factory ::setAddresses );
101
+ map .from (properties ::isPublisherConfirms ).to (factory ::setPublisherConfirms );
102
+ map .from (properties ::isPublisherReturns ).to (factory ::setPublisherReturns );
103
+ RabbitProperties .Cache .Channel channel = properties .getCache ().getChannel ();
104
+ map .from (channel ::getSize ).whenNonNull ().to (factory ::setChannelCacheSize );
105
+ map .from (channel ::getCheckoutTimeout ).whenNonNull ()
106
+ .to (factory ::setChannelCheckoutTimeout );
107
+ RabbitProperties .Cache .Connection connection = properties .getCache ()
108
+ .getConnection ();
109
+ map .from (connection ::getMode ).whenNonNull ().to (factory ::setCacheMode );
110
+ map .from (connection ::getSize ).whenNonNull ()
111
+ .to (factory ::setConnectionCacheSize );
112
+ return factory ;
113
+ }
114
+
115
+ private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean (
116
+ RabbitProperties properties ) throws Exception {
117
+ PropertyMapper map = PropertyMapper .get ();
93
118
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean ();
94
- if (config .determineHost () != null ) {
95
- factory .setHost (config .determineHost ());
96
- }
97
- factory .setPort (config .determinePort ());
98
- if (config .determineUsername () != null ) {
99
- factory .setUsername (config .determineUsername ());
100
- }
101
- if (config .determinePassword () != null ) {
102
- factory .setPassword (config .determinePassword ());
103
- }
104
- if (config .determineVirtualHost () != null ) {
105
- factory .setVirtualHost (config .determineVirtualHost ());
106
- }
107
- if (config .getRequestedHeartbeat () != null ) {
108
- factory .setRequestedHeartbeat (
109
- (int ) config .getRequestedHeartbeat ().getSeconds ());
110
- }
111
- RabbitProperties .Ssl ssl = config .getSsl ();
119
+ map .from (properties ::determineHost ).whenNonNull ().to (factory ::setHost );
120
+ map .from (properties ::determinePort ).to (factory ::setPort );
121
+ map .from (properties ::determineUsername ).whenNonNull ()
122
+ .to (factory ::setUsername );
123
+ map .from (properties ::determinePassword ).whenNonNull ()
124
+ .to (factory ::setPassword );
125
+ map .from (properties ::determineVirtualHost ).whenNonNull ()
126
+ .to (factory ::setVirtualHost );
127
+ map .from (properties ::getRequestedHeartbeat ).whenNonNull ()
128
+ .asInt (Duration ::getSeconds ).to (factory ::setRequestedHeartbeat );
129
+ RabbitProperties .Ssl ssl = properties .getSsl ();
112
130
if (ssl .isEnabled ()) {
113
131
factory .setUseSSL (true );
114
- if (ssl .getAlgorithm () != null ) {
115
- factory .setSslAlgorithm (ssl .getAlgorithm ());
116
- }
117
- factory .setKeyStoreType (ssl .getKeyStoreType ());
118
- factory .setKeyStore (ssl .getKeyStore ());
119
- factory .setKeyStorePassphrase (ssl .getKeyStorePassword ());
120
- factory .setTrustStoreType (ssl .getTrustStoreType ());
121
- factory .setTrustStore (ssl .getTrustStore ());
122
- factory .setTrustStorePassphrase (ssl .getTrustStorePassword ());
123
- }
124
- if (config .getConnectionTimeout () != null ) {
125
- factory .setConnectionTimeout (
126
- (int ) config .getConnectionTimeout ().toMillis ());
132
+ map .from (ssl ::getAlgorithm ).whenNonNull ().to (factory ::setSslAlgorithm );
133
+ map .from (ssl ::getKeyStoreType ).to (factory ::setKeyStoreType );
134
+ map .from (ssl ::getKeyStore ).to (factory ::setKeyStore );
135
+ map .from (ssl ::getKeyStorePassword ).to (factory ::setKeyStorePassphrase );
136
+ map .from (ssl ::getTrustStoreType ).to (factory ::setTrustStoreType );
137
+ map .from (ssl ::getTrustStore ).to (factory ::setTrustStore );
138
+ map .from (ssl ::getTrustStorePassword ).to (factory ::setTrustStorePassphrase );
127
139
}
140
+ map .from (properties ::getConnectionTimeout ).whenNonNull ()
141
+ .asInt (Duration ::toMillis ).to (factory ::setConnectionTimeout );
128
142
factory .afterPropertiesSet ();
129
- CachingConnectionFactory connectionFactory = new CachingConnectionFactory (
130
- factory .getObject ());
131
- connectionFactory .setAddresses (config .determineAddresses ());
132
- connectionFactory .setPublisherConfirms (config .isPublisherConfirms ());
133
- connectionFactory .setPublisherReturns (config .isPublisherReturns ());
134
- if (config .getCache ().getChannel ().getSize () != null ) {
135
- connectionFactory
136
- .setChannelCacheSize (config .getCache ().getChannel ().getSize ());
137
- }
138
- if (config .getCache ().getConnection ().getMode () != null ) {
139
- connectionFactory
140
- .setCacheMode (config .getCache ().getConnection ().getMode ());
141
- }
142
- if (config .getCache ().getConnection ().getSize () != null ) {
143
- connectionFactory .setConnectionCacheSize (
144
- config .getCache ().getConnection ().getSize ());
145
- }
146
- if (config .getCache ().getChannel ().getCheckoutTimeout () != null ) {
147
- connectionFactory .setChannelCheckoutTimeout (
148
- config .getCache ().getChannel ().getCheckoutTimeout ());
149
- }
150
- return connectionFactory ;
143
+ return factory ;
151
144
}
152
145
153
146
}
@@ -171,26 +164,24 @@ public RabbitTemplateConfiguration(
171
164
@ ConditionalOnSingleCandidate (ConnectionFactory .class )
172
165
@ ConditionalOnMissingBean (RabbitTemplate .class )
173
166
public RabbitTemplate rabbitTemplate (ConnectionFactory connectionFactory ) {
174
- RabbitTemplate rabbitTemplate = new RabbitTemplate (connectionFactory );
167
+ PropertyMapper map = PropertyMapper .get ();
168
+ RabbitTemplate template = new RabbitTemplate (connectionFactory );
175
169
MessageConverter messageConverter = this .messageConverter .getIfUnique ();
176
170
if (messageConverter != null ) {
177
- rabbitTemplate .setMessageConverter (messageConverter );
178
- }
179
- rabbitTemplate .setMandatory (determineMandatoryFlag ());
180
- RabbitProperties .Template templateProperties = this .properties .getTemplate ();
181
- RabbitProperties .Retry retryProperties = templateProperties .getRetry ();
182
- if (retryProperties .isEnabled ()) {
183
- rabbitTemplate .setRetryTemplate (createRetryTemplate (retryProperties ));
171
+ template .setMessageConverter (messageConverter );
184
172
}
185
- if (templateProperties .getReceiveTimeout () != null ) {
186
- rabbitTemplate .setReceiveTimeout (templateProperties .getReceiveTimeout ());
173
+ template .setMandatory (determineMandatoryFlag ());
174
+ RabbitProperties .Template properties = this .properties .getTemplate ();
175
+ if (properties .getRetry ().isEnabled ()) {
176
+ template .setRetryTemplate (createRetryTemplate (properties .getRetry ()));
187
177
}
188
- if (templateProperties .getReplyTimeout () != null ) {
189
- rabbitTemplate .setReplyTimeout (templateProperties .getReplyTimeout ());
190
- }
191
- rabbitTemplate .setExchange (templateProperties .getExchange ());
192
- rabbitTemplate .setRoutingKey (templateProperties .getRoutingKey ());
193
- return rabbitTemplate ;
178
+ map .from (properties ::getReceiveTimeout ).whenNonNull ()
179
+ .to (template ::setReceiveTimeout );
180
+ map .from (properties ::getReplyTimeout ).whenNonNull ()
181
+ .to (template ::setReplyTimeout );
182
+ map .from (properties ::getExchange ).to (template ::setExchange );
183
+ map .from (properties ::getRoutingKey ).to (template ::setRoutingKey );
184
+ return template ;
194
185
}
195
186
196
187
private boolean determineMandatoryFlag () {
@@ -199,14 +190,16 @@ private boolean determineMandatoryFlag() {
199
190
}
200
191
201
192
private RetryTemplate createRetryTemplate (RabbitProperties .Retry properties ) {
193
+ PropertyMapper map = PropertyMapper .get ();
202
194
RetryTemplate template = new RetryTemplate ();
203
195
SimpleRetryPolicy policy = new SimpleRetryPolicy ();
204
- policy . setMaxAttempts (properties . getMaxAttempts () );
196
+ map . from (properties :: getMaxAttempts ). to ( policy :: setMaxAttempts );
205
197
template .setRetryPolicy (policy );
206
198
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy ();
207
- backOffPolicy .setInitialInterval (properties .getInitialInterval ());
208
- backOffPolicy .setMultiplier (properties .getMultiplier ());
209
- backOffPolicy .setMaxInterval (properties .getMaxInterval ());
199
+ map .from (properties ::getInitialInterval )
200
+ .to (backOffPolicy ::setInitialInterval );
201
+ map .from (properties ::getMultiplier ).to (backOffPolicy ::setMultiplier );
202
+ map .from (properties ::getMaxInterval ).to (backOffPolicy ::setMaxInterval );
210
203
template .setBackOffPolicy (backOffPolicy );
211
204
return template ;
212
205
}
0 commit comments