|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.amqp; |
18 | 18 |
|
19 | | -import java.time.Duration; |
20 | 19 | import java.util.stream.Collectors; |
21 | 20 |
|
22 | 21 | import com.rabbitmq.client.Channel; |
|
40 | 39 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
41 | 40 | import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
42 | 41 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
43 | | -import org.springframework.boot.context.properties.PropertyMapper; |
44 | 42 | import org.springframework.context.annotation.Bean; |
45 | 43 | import org.springframework.context.annotation.Configuration; |
46 | 44 | import org.springframework.context.annotation.Import; |
|
84 | 82 | * @author Gary Russell |
85 | 83 | * @author Phillip Webb |
86 | 84 | * @author Artsiom Yudovin |
| 85 | + * @author Chris Bono |
87 | 86 | * @since 1.0.0 |
88 | 87 | */ |
89 | 88 | @Configuration(proxyBeanMethods = false) |
|
93 | 92 | public class RabbitAutoConfiguration { |
94 | 93 |
|
95 | 94 | @Configuration(proxyBeanMethods = false) |
96 | | - @ConditionalOnMissingBean(ConnectionFactory.class) |
97 | 95 | protected static class RabbitConnectionFactoryCreator { |
98 | 96 |
|
99 | 97 | @Bean |
100 | | - public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties, |
| 98 | + @ConditionalOnMissingBean |
| 99 | + RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer(RabbitProperties properties, |
101 | 100 | ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider, |
102 | | - ObjectProvider<CredentialsRefreshService> credentialsRefreshService, |
103 | | - ObjectProvider<ConnectionNameStrategy> connectionNameStrategy, |
| 101 | + ObjectProvider<CredentialsRefreshService> credentialsRefreshService) { |
| 102 | + RabbitConnectionFactoryBeanConfigurer configurer = new RabbitConnectionFactoryBeanConfigurer(); |
| 103 | + configurer.setRabbitProperties(properties); |
| 104 | + configurer.setResourceLoader(resourceLoader); |
| 105 | + configurer.setCredentialsProvider(credentialsProvider.getIfUnique()); |
| 106 | + configurer.setCredentialsRefreshService(credentialsRefreshService.getIfUnique()); |
| 107 | + return configurer; |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + @ConditionalOnMissingBean |
| 112 | + CachingConnectionFactoryConfigurer rabbitConnectionFactoryConfigurer(RabbitProperties rabbitProperties, |
| 113 | + ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) { |
| 114 | + CachingConnectionFactoryConfigurer configurer = new CachingConnectionFactoryConfigurer(); |
| 115 | + configurer.setRabbitProperties(rabbitProperties); |
| 116 | + configurer.setConnectionNameStrategy(connectionNameStrategy.getIfUnique()); |
| 117 | + return configurer; |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + @ConditionalOnMissingBean(ConnectionFactory.class) |
| 122 | + CachingConnectionFactory rabbitConnectionFactory( |
| 123 | + RabbitConnectionFactoryBeanConfigurer rabbitConnectionFactoryBeanConfigurer, |
| 124 | + CachingConnectionFactoryConfigurer rabbitCachingConnectionFactoryConfigurer, |
104 | 125 | ObjectProvider<ConnectionFactoryCustomizer> connectionFactoryCustomizers) throws Exception { |
105 | | - com.rabbitmq.client.ConnectionFactory connectionFactory = getRabbitConnectionFactoryBean(properties, |
106 | | - resourceLoader, credentialsProvider, credentialsRefreshService).getObject(); |
| 126 | + |
| 127 | + RabbitConnectionFactoryBean connectionFactoryBean = new RabbitConnectionFactoryBean(); |
| 128 | + rabbitConnectionFactoryBeanConfigurer.configure(connectionFactoryBean); |
| 129 | + connectionFactoryBean.afterPropertiesSet(); |
| 130 | + com.rabbitmq.client.ConnectionFactory connectionFactory = connectionFactoryBean.getObject(); |
107 | 131 | connectionFactoryCustomizers.orderedStream() |
108 | 132 | .forEach((customizer) -> customizer.customize(connectionFactory)); |
| 133 | + |
109 | 134 | CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory); |
110 | | - PropertyMapper map = PropertyMapper.get(); |
111 | | - map.from(properties::determineAddresses).to(factory::setAddresses); |
112 | | - map.from(properties::getAddressShuffleMode).whenNonNull().to(factory::setAddressShuffleMode); |
113 | | - map.from(properties::isPublisherReturns).to(factory::setPublisherReturns); |
114 | | - map.from(properties::getPublisherConfirmType).whenNonNull().to(factory::setPublisherConfirmType); |
115 | | - RabbitProperties.Cache.Channel channel = properties.getCache().getChannel(); |
116 | | - map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize); |
117 | | - map.from(channel::getCheckoutTimeout).whenNonNull().as(Duration::toMillis) |
118 | | - .to(factory::setChannelCheckoutTimeout); |
119 | | - RabbitProperties.Cache.Connection connection = properties.getCache().getConnection(); |
120 | | - map.from(connection::getMode).whenNonNull().to(factory::setCacheMode); |
121 | | - map.from(connection::getSize).whenNonNull().to(factory::setConnectionCacheSize); |
122 | | - map.from(connectionNameStrategy::getIfUnique).whenNonNull().to(factory::setConnectionNameStrategy); |
123 | | - return factory; |
124 | | - } |
| 135 | + rabbitCachingConnectionFactoryConfigurer.configure(factory); |
125 | 136 |
|
126 | | - private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(RabbitProperties properties, |
127 | | - ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider, |
128 | | - ObjectProvider<CredentialsRefreshService> credentialsRefreshService) { |
129 | | - RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean(); |
130 | | - factory.setResourceLoader(resourceLoader); |
131 | | - PropertyMapper map = PropertyMapper.get(); |
132 | | - map.from(properties::determineHost).whenNonNull().to(factory::setHost); |
133 | | - map.from(properties::determinePort).to(factory::setPort); |
134 | | - map.from(properties::determineUsername).whenNonNull().to(factory::setUsername); |
135 | | - map.from(properties::determinePassword).whenNonNull().to(factory::setPassword); |
136 | | - map.from(properties::determineVirtualHost).whenNonNull().to(factory::setVirtualHost); |
137 | | - map.from(properties::getRequestedHeartbeat).whenNonNull().asInt(Duration::getSeconds) |
138 | | - .to(factory::setRequestedHeartbeat); |
139 | | - map.from(properties::getRequestedChannelMax).to(factory::setRequestedChannelMax); |
140 | | - RabbitProperties.Ssl ssl = properties.getSsl(); |
141 | | - if (ssl.determineEnabled()) { |
142 | | - factory.setUseSSL(true); |
143 | | - map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm); |
144 | | - map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType); |
145 | | - map.from(ssl::getKeyStore).to(factory::setKeyStore); |
146 | | - map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase); |
147 | | - map.from(ssl::getKeyStoreAlgorithm).whenNonNull().to(factory::setKeyStoreAlgorithm); |
148 | | - map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType); |
149 | | - map.from(ssl::getTrustStore).to(factory::setTrustStore); |
150 | | - map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase); |
151 | | - map.from(ssl::getTrustStoreAlgorithm).whenNonNull().to(factory::setTrustStoreAlgorithm); |
152 | | - map.from(ssl::isValidateServerCertificate) |
153 | | - .to((validate) -> factory.setSkipServerCertificateValidation(!validate)); |
154 | | - map.from(ssl::getVerifyHostname).to(factory::setEnableHostnameVerification); |
155 | | - } |
156 | | - map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) |
157 | | - .to(factory::setConnectionTimeout); |
158 | | - map.from(properties::getChannelRpcTimeout).whenNonNull().asInt(Duration::toMillis) |
159 | | - .to(factory::setChannelRpcTimeout); |
160 | | - map.from(credentialsProvider::getIfUnique).whenNonNull().to(factory::setCredentialsProvider); |
161 | | - map.from(credentialsRefreshService::getIfUnique).whenNonNull().to(factory::setCredentialsRefreshService); |
162 | | - factory.afterPropertiesSet(); |
163 | 137 | return factory; |
164 | 138 | } |
165 | 139 |
|
|
0 commit comments