Class RabbitConnectionFactoryBean
- java.lang.Object
-
- org.springframework.beans.factory.config.AbstractFactoryBean<com.rabbitmq.client.ConnectionFactory>
-
- org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean
-
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,BeanFactoryAware
,DisposableBean
,FactoryBean<com.rabbitmq.client.ConnectionFactory>
,InitializingBean
public class RabbitConnectionFactoryBean extends AbstractFactoryBean<com.rabbitmq.client.ConnectionFactory>
Factory bean to create a RabbitMQ ConnectionFactory, delegating most setter methods and optionally enabling SSL, with or without certificate validation. WhensslPropertiesLocation
is not null, the default implementation loads aPKCS12
keystore and aJKS
truststore using the supplied properties and intializes key and trust manager factories, using algorithmSunX509
by default. These are then used to initialize anSSLContext
using thesslAlgorithm
(default TLSv1.1).Override
createSSLContext()
to create and/or perform further modification of the context.Override
setUpSSL()
to take complete control over setting up SSL.- Since:
- 1.4
- Author:
- Gary Russell, Heath Abelson, Arnaud Cogoluègnes, Hareendran, Dominique Villard, Zachary DeLuca
-
-
Field Summary
Fields Modifier and Type Field Description protected com.rabbitmq.client.ConnectionFactory
connectionFactory
-
Fields inherited from class org.springframework.beans.factory.config.AbstractFactoryBean
logger
-
Fields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE
-
-
Constructor Summary
Constructors Constructor Description RabbitConnectionFactoryBean()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
protected javax.net.ssl.KeyManager[]
configureKeyManagers()
protected javax.net.ssl.TrustManager[]
configureTrustManagers()
protected com.rabbitmq.client.ConnectionFactory
createInstance()
protected javax.net.ssl.SSLContext
createSSLContext()
Override this method to create and/or configure theSSLContext
used by theConnectionFactory
.protected java.lang.String
getKeyStore()
protected java.lang.String
getKeyStoreAlgorithm()
protected java.lang.String
getKeyStorePassphrase()
protected Resource
getKeyStoreResource()
protected java.lang.String
getKeyStoreType()
Get the key store type - this defaults to PKCS12 if not overridden bysetSslPropertiesLocation(Resource)
orsetKeyStoreType(java.lang.String)
.java.lang.Class<?>
getObjectType()
com.rabbitmq.client.ConnectionFactory
getRabbitConnectionFactory()
Access the connection factory to set any other properties not supported by this factory bean.protected ResourceLoader
getResourceLoader()
Get the resource loader; used to resolve the key store and trust storeResource
s to input streams.protected java.security.SecureRandom
getSecureRandom()
protected java.lang.String
getSslAlgorithm()
protected Resource
getSslPropertiesLocation()
protected java.lang.String
getTrustStore()
protected java.lang.String
getTrustStoreAlgorithm()
protected java.lang.String
getTrustStorePassphrase()
protected Resource
getTrustStoreResource()
protected java.lang.String
getTrustStoreType()
Get the trust store type - this defaults to JKS if not overridden bysetSslPropertiesLocation(Resource)
orsetTrustStoreType(java.lang.String)
.boolean
isSkipServerCertificateValidation()
Whether or not Server Side certificate has to be validated or not.protected boolean
isUseSSL()
void
setAutomaticRecoveryEnabled(boolean automaticRecoveryEnabled)
Set to true to enable amqp-client automatic recovery.void
setChannelRpcTimeout(int channelRpcTimeout)
void
setClientProperties(java.util.Map<java.lang.String,java.lang.Object> clientProperties)
Add custom client properties.void
setConnectionTimeout(int connectionTimeout)
void
setCredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider provider)
Set a credentials provider (e.g.void
setCredentialsRefreshService(com.rabbitmq.client.impl.CredentialsRefreshService service)
Set a refresh service.void
setEnableHostnameVerification(boolean enable)
Enable server hostname verification for TLS connections.void
setExceptionHandler(com.rabbitmq.client.ExceptionHandler exceptionHandler)
void
setHost(java.lang.String host)
void
setKeyStore(java.lang.String keyStore)
Set the key store resource (e.g.void
setKeyStoreAlgorithm(java.lang.String keyStoreAlgorithm)
Set the algorithm used when creating the key store, defaultSunX509
.void
setKeyStorePassphrase(java.lang.String keyStorePassphrase)
Set the key store pass phrase - overrides the property insetSslPropertiesLocation(Resource)
.void
setKeyStoreResource(Resource keyStoreResource)
Set a Resource pointing to the key store.void
setKeyStoreType(java.lang.String keyStoreType)
Set the key store type - overrides the property insetSslPropertiesLocation(Resource)
.void
setMetricsCollector(com.rabbitmq.client.MetricsCollector metricsCollector)
void
setNioParams(com.rabbitmq.client.impl.nio.NioParams nioParams)
void
setPassword(java.lang.String password)
void
setPort(int port)
void
setRequestedChannelMax(int requestedChannelMax)
void
setRequestedFrameMax(int requestedFrameMax)
void
setRequestedHeartbeat(int requestedHeartbeat)
void
setResourceLoader(ResourceLoader resourceLoader)
Set the resource loader; used to resolve the key store and trust storeResource
s to input streams.void
setSaslConfig(com.rabbitmq.client.SaslConfig saslConfig)
void
setSecureRandom(java.security.SecureRandom secureRandom)
Set the secure random to use when initializing theSSLContext
.void
setSharedExecutor(java.util.concurrent.ExecutorService executor)
void
setSkipServerCertificateValidation(boolean skipServerCertificateValidation)
Whether or not Server Side certificate has to be validated or not.void
setSocketConfigurator(com.rabbitmq.client.SocketConfigurator socketConfigurator)
void
setSocketFactory(javax.net.SocketFactory factory)
void
setSslAlgorithm(java.lang.String sslAlgorithm)
Set the algorithm to use; default TLSv1.1.void
setSslPropertiesLocation(Resource sslPropertiesLocation)
WhensetUseSSL(boolean)
is true, the SSL properties to use (optional).void
setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
void
setTopologyRecoveryEnabled(boolean topologyRecoveryEnabled)
Set to true to enable amqp-client topology recovery.void
setTrustStore(java.lang.String trustStore)
Set the key store resource (e.g.void
setTrustStoreAlgorithm(java.lang.String trustStoreAlgorithm)
Set the algorithm used when creating the trust store, defaultSunX509
.void
setTrustStorePassphrase(java.lang.String trustStorePassphrase)
Set the trust store pass phrase - overrides the property insetSslPropertiesLocation(Resource)
.void
setTrustStoreResource(Resource trustStoreResource)
Set a Resource pointing to the trust store.void
setTrustStoreType(java.lang.String trustStoreType)
Set the trust store type - overrides the property insetSslPropertiesLocation(Resource)
.protected void
setUpSSL()
Override this method to take complete control over the SSL setup.void
setUri(java.lang.String uriString)
void
setUri(java.net.URI uri)
void
setUseNio(boolean useNio)
Whether or not the factory should be configured to use Java NIO.void
setUsername(java.lang.String username)
void
setUseSSL(boolean useSSL)
Whether or not the factory should be configured to use SSL.void
setVirtualHost(java.lang.String virtualHost)
-
Methods inherited from class org.springframework.beans.factory.config.AbstractFactoryBean
destroy, destroyInstance, getBeanFactory, getBeanTypeConverter, getEarlySingletonInterfaces, getObject, isSingleton, setBeanClassLoader, setBeanFactory, setSingleton
-
-
-
-
Method Detail
-
isSkipServerCertificateValidation
public boolean isSkipServerCertificateValidation()
Whether or not Server Side certificate has to be validated or not.- Returns:
- true if Server Side certificate has to be skipped
- Since:
- 1.6.6
-
setSkipServerCertificateValidation
public void setSkipServerCertificateValidation(boolean skipServerCertificateValidation)
Whether or not Server Side certificate has to be validated or not. This would be used if useSSL is set to true and should only be used on dev or Qa regions skipServerCertificateValidation should never be set to true in production- Parameters:
skipServerCertificateValidation
- Flag to override Server side certificate checks; if set totrue
TrustEverythingTrustManager
would be used.- Since:
- 1.6.6
- See Also:
TrustEverythingTrustManager
-
setUseSSL
public void setUseSSL(boolean useSSL)
Whether or not the factory should be configured to use SSL.- Parameters:
useSSL
- true to use SSL.
-
isUseSSL
protected boolean isUseSSL()
- Returns:
- true to use ssl.
- Since:
- 1.4.4.
-
setSslAlgorithm
public void setSslAlgorithm(java.lang.String sslAlgorithm)
Set the algorithm to use; default TLSv1.1.- Parameters:
sslAlgorithm
- the algorithm.
-
getSslAlgorithm
protected java.lang.String getSslAlgorithm()
- Returns:
- the ssl algorithm.
- Since:
- 1.4.4
-
setSslPropertiesLocation
public void setSslPropertiesLocation(Resource sslPropertiesLocation)
WhensetUseSSL(boolean)
is true, the SSL properties to use (optional). Resource referencing a properties file with the following properties:- keyStore=file:/secret/keycert.p12
- trustStore=file:/secret/trustStore
- keyStore.passPhrase=secret
- trustStore.passPhrase=secret
If this is provided, its properties (if present) will override the explicitly set property in this bean.
- Parameters:
sslPropertiesLocation
- the Resource to the ssl properties
-
getSslPropertiesLocation
protected Resource getSslPropertiesLocation()
- Returns:
- the properties location.
- Since:
- 1.4.4
-
getKeyStore
protected java.lang.String getKeyStore()
- Returns:
- the key store resource.
- Since:
- 1.5
-
setKeyStore
public void setKeyStore(java.lang.String keyStore)
Set the key store resource (e.g. file:/foo/keystore) - overrides the property insetSslPropertiesLocation(Resource)
. Ignored ifsetTrustStoreResource(Resource)
is called with a resource.- Parameters:
keyStore
- the keystore resource.- Since:
- 1.5
-
getKeyStoreResource
protected Resource getKeyStoreResource()
-
setKeyStoreResource
public void setKeyStoreResource(Resource keyStoreResource)
Set a Resource pointing to the key store.- Parameters:
keyStoreResource
- the resource.- Since:
- 1.6.4
-
getTrustStore
protected java.lang.String getTrustStore()
- Returns:
- the trust store resource.
- Since:
- 1.5
-
setTrustStore
public void setTrustStore(java.lang.String trustStore)
Set the key store resource (e.g. file:/foo/truststore) - overrides the property insetSslPropertiesLocation(Resource)
. Ignored ifsetTrustStoreResource(Resource)
is called with a resource.- Parameters:
trustStore
- the truststore resource.- Since:
- 1.5
-
getTrustStoreResource
protected Resource getTrustStoreResource()
-
setTrustStoreResource
public void setTrustStoreResource(Resource trustStoreResource)
Set a Resource pointing to the trust store.- Parameters:
trustStoreResource
- the resource.- Since:
- 1.6.4
-
getKeyStorePassphrase
protected java.lang.String getKeyStorePassphrase()
- Returns:
- the key store pass phrase.
- Since:
- 1.5
-
setKeyStorePassphrase
public void setKeyStorePassphrase(java.lang.String keyStorePassphrase)
Set the key store pass phrase - overrides the property insetSslPropertiesLocation(Resource)
.- Parameters:
keyStorePassphrase
- the key store pass phrase.- Since:
- 1.5
-
getTrustStorePassphrase
protected java.lang.String getTrustStorePassphrase()
- Returns:
- the trust store pass phrase.
- Since:
- 1.5
-
setTrustStorePassphrase
public void setTrustStorePassphrase(java.lang.String trustStorePassphrase)
Set the trust store pass phrase - overrides the property insetSslPropertiesLocation(Resource)
.- Parameters:
trustStorePassphrase
- the trust store pass phrase.- Since:
- 1.5
-
getKeyStoreType
protected java.lang.String getKeyStoreType()
Get the key store type - this defaults to PKCS12 if not overridden bysetSslPropertiesLocation(Resource)
orsetKeyStoreType(java.lang.String)
.- Returns:
- the key store type.
- Since:
- 1.6.2
-
setKeyStoreType
public void setKeyStoreType(java.lang.String keyStoreType)
Set the key store type - overrides the property insetSslPropertiesLocation(Resource)
.- Parameters:
keyStoreType
- the key store type.- Since:
- 1.6.2
- See Also:
KeyStore.getInstance(String)
-
getTrustStoreType
protected java.lang.String getTrustStoreType()
Get the trust store type - this defaults to JKS if not overridden bysetSslPropertiesLocation(Resource)
orsetTrustStoreType(java.lang.String)
.- Returns:
- the trust store type.
- Since:
- 1.6.2
-
setTrustStoreType
public void setTrustStoreType(java.lang.String trustStoreType)
Set the trust store type - overrides the property insetSslPropertiesLocation(Resource)
.- Parameters:
trustStoreType
- the trust store type.- Since:
- 1.6.2
- See Also:
KeyStore.getInstance(String)
-
getSecureRandom
protected java.security.SecureRandom getSecureRandom()
-
setSecureRandom
public void setSecureRandom(java.security.SecureRandom secureRandom)
Set the secure random to use when initializing theSSLContext
. Defaults to null, in which case the default implementation is used.- Parameters:
secureRandom
- the secure random.- Since:
- 1.6.4
- See Also:
SSLContext.init(KeyManager[], TrustManager[], SecureRandom)
-
setHost
public void setHost(java.lang.String host)
- Parameters:
host
- the host.- See Also:
ConnectionFactory.setHost(java.lang.String)
-
setPort
public void setPort(int port)
- Parameters:
port
- the port.- See Also:
ConnectionFactory.setPort(int)
-
setUsername
public void setUsername(java.lang.String username)
- Parameters:
username
- the user name.- See Also:
ConnectionFactory.setUsername(java.lang.String)
-
setPassword
public void setPassword(java.lang.String password)
- Parameters:
password
- the password.- See Also:
ConnectionFactory.setPassword(java.lang.String)
-
setCredentialsProvider
public void setCredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider provider)
Set a credentials provider (e.g. OAUTH2).- Parameters:
provider
- the provider.- Since:
- 2.3
-
setCredentialsRefreshService
public void setCredentialsRefreshService(com.rabbitmq.client.impl.CredentialsRefreshService service)
Set a refresh service.- Parameters:
service
- the service.- Since:
- 2.3
-
setVirtualHost
public void setVirtualHost(java.lang.String virtualHost)
- Parameters:
virtualHost
- the virtual host.- See Also:
ConnectionFactory.setVirtualHost(java.lang.String)
-
setUri
public void setUri(java.net.URI uri)
- Parameters:
uri
- the uri.- See Also:
ConnectionFactory.setUri(java.net.URI)
-
setUri
public void setUri(java.lang.String uriString)
- Parameters:
uriString
- the uri.- See Also:
ConnectionFactory.setUri(java.lang.String)
-
setRequestedChannelMax
public void setRequestedChannelMax(int requestedChannelMax)
- Parameters:
requestedChannelMax
- the max requested channels.- See Also:
ConnectionFactory.setRequestedChannelMax(int)
-
setRequestedFrameMax
public void setRequestedFrameMax(int requestedFrameMax)
- Parameters:
requestedFrameMax
- the requested max frames.- See Also:
ConnectionFactory.setRequestedFrameMax(int)
-
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout)
- Parameters:
connectionTimeout
- the connection timeout.- See Also:
ConnectionFactory.setConnectionTimeout(int)
-
setRequestedHeartbeat
public void setRequestedHeartbeat(int requestedHeartbeat)
- Parameters:
requestedHeartbeat
- the requested heartbeat.- See Also:
ConnectionFactory.setRequestedHeartbeat(int)
-
setClientProperties
public void setClientProperties(java.util.Map<java.lang.String,java.lang.Object> clientProperties)
Add custom client properties.- Parameters:
clientProperties
- the client properties.- See Also:
ConnectionFactory.setClientProperties(java.util.Map)
-
setSaslConfig
public void setSaslConfig(com.rabbitmq.client.SaslConfig saslConfig)
- Parameters:
saslConfig
- the sasl config.- See Also:
ConnectionFactory.setSaslConfig(com.rabbitmq.client.SaslConfig)
-
setSocketFactory
public void setSocketFactory(javax.net.SocketFactory factory)
- Parameters:
factory
- the socket factory.- See Also:
ConnectionFactory.setSocketFactory(javax.net.SocketFactory)
-
setSocketConfigurator
public void setSocketConfigurator(com.rabbitmq.client.SocketConfigurator socketConfigurator)
- Parameters:
socketConfigurator
- the socket configurator.- See Also:
ConnectionFactory.setSocketConfigurator(com.rabbitmq.client.SocketConfigurator)
-
setSharedExecutor
public void setSharedExecutor(java.util.concurrent.ExecutorService executor)
- Parameters:
executor
- the executor service- See Also:
ConnectionFactory.setSharedExecutor(java.util.concurrent.ExecutorService)
-
setThreadFactory
public void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
- Parameters:
threadFactory
- the thread factory.- See Also:
ConnectionFactory.setThreadFactory(java.util.concurrent.ThreadFactory)
-
setExceptionHandler
public void setExceptionHandler(com.rabbitmq.client.ExceptionHandler exceptionHandler)
- Parameters:
exceptionHandler
- the exception handler.- See Also:
ConnectionFactory.setExceptionHandler(com.rabbitmq.client.ExceptionHandler)
-
setUseNio
public void setUseNio(boolean useNio)
Whether or not the factory should be configured to use Java NIO.- Parameters:
useNio
- true to use Java NIO, false to use blocking IO- See Also:
ConnectionFactory.useNio()
-
setNioParams
public void setNioParams(com.rabbitmq.client.impl.nio.NioParams nioParams)
- Parameters:
nioParams
- the NIO parameters- See Also:
ConnectionFactory.setNioParams(com.rabbitmq.client.impl.nio.NioParams)
-
setMetricsCollector
public void setMetricsCollector(com.rabbitmq.client.MetricsCollector metricsCollector)
- Parameters:
metricsCollector
- the metrics collector instance- See Also:
ConnectionFactory.setMetricsCollector(MetricsCollector)
-
setAutomaticRecoveryEnabled
public void setAutomaticRecoveryEnabled(boolean automaticRecoveryEnabled)
Set to true to enable amqp-client automatic recovery. Note: Spring AMQP implements its own connection recovery and this is generally not needed.- Parameters:
automaticRecoveryEnabled
- true to enable.- Since:
- 1.7.1
-
setTopologyRecoveryEnabled
public void setTopologyRecoveryEnabled(boolean topologyRecoveryEnabled)
Set to true to enable amqp-client topology recovery. Note: if there is a Rabbit admin in the application context, Spring AMQP implements its own recovery and this is generally not needed.- Parameters:
topologyRecoveryEnabled
- true to enable.- Since:
- 1.7.1
-
setChannelRpcTimeout
public void setChannelRpcTimeout(int channelRpcTimeout)
- Parameters:
channelRpcTimeout
- continuation timeout for RPC calls in channels- Since:
- 2.0
- See Also:
ConnectionFactory.setChannelRpcTimeout(int)
-
setEnableHostnameVerification
public void setEnableHostnameVerification(boolean enable)
Enable server hostname verification for TLS connections.This enables hostname verification regardless of the IO mode used (blocking or non-blocking IO).
This can be called typically after setting the
SSLContext
with one of theuseSslProtocol
methods. Requires amqp-client 5.4.0 or later.- Parameters:
enable
- false to disable.- Since:
- 2.0.6
- See Also:
ConnectionFactory.enableHostnameVerification()
-
getKeyStoreAlgorithm
protected java.lang.String getKeyStoreAlgorithm()
-
setKeyStoreAlgorithm
public void setKeyStoreAlgorithm(java.lang.String keyStoreAlgorithm)
Set the algorithm used when creating the key store, defaultSunX509
.- Parameters:
keyStoreAlgorithm
- the algorithm.- Since:
- 2.1.6
-
getTrustStoreAlgorithm
protected java.lang.String getTrustStoreAlgorithm()
-
setTrustStoreAlgorithm
public void setTrustStoreAlgorithm(java.lang.String trustStoreAlgorithm)
Set the algorithm used when creating the trust store, defaultSunX509
.- Parameters:
trustStoreAlgorithm
- the algorithm.- Since:
- 2.1.6
-
getResourceLoader
protected ResourceLoader getResourceLoader()
Get the resource loader; used to resolve the key store and trust storeResource
s to input streams.- Returns:
- the resource loader.
- Since:
- 2.3
-
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
Set the resource loader; used to resolve the key store and trust storeResource
s to input streams.- Parameters:
resourceLoader
- the resource loader.- Since:
- 2.3
-
getRabbitConnectionFactory
public com.rabbitmq.client.ConnectionFactory getRabbitConnectionFactory()
Access the connection factory to set any other properties not supported by this factory bean.- Returns:
- the connection factory.
- Since:
- 1.7.14
-
afterPropertiesSet
public void afterPropertiesSet()
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Overrides:
afterPropertiesSet
in classAbstractFactoryBean<com.rabbitmq.client.ConnectionFactory>
-
getObjectType
public java.lang.Class<?> getObjectType()
- Specified by:
getObjectType
in interfaceFactoryBean<com.rabbitmq.client.ConnectionFactory>
- Specified by:
getObjectType
in classAbstractFactoryBean<com.rabbitmq.client.ConnectionFactory>
-
createInstance
protected com.rabbitmq.client.ConnectionFactory createInstance()
- Specified by:
createInstance
in classAbstractFactoryBean<com.rabbitmq.client.ConnectionFactory>
-
setUpSSL
protected void setUpSSL()
Override this method to take complete control over the SSL setup.- Since:
- 1.4.4
-
configureKeyManagers
@Nullable protected javax.net.ssl.KeyManager[] configureKeyManagers() throws java.security.KeyStoreException, java.io.IOException, java.security.NoSuchAlgorithmException, java.security.cert.CertificateException, java.security.UnrecoverableKeyException
- Throws:
java.security.KeyStoreException
java.io.IOException
java.security.NoSuchAlgorithmException
java.security.cert.CertificateException
java.security.UnrecoverableKeyException
-
configureTrustManagers
@Nullable protected javax.net.ssl.TrustManager[] configureTrustManagers() throws java.security.KeyStoreException, java.io.IOException, java.security.NoSuchAlgorithmException, java.security.cert.CertificateException
- Throws:
java.security.KeyStoreException
java.io.IOException
java.security.NoSuchAlgorithmException
java.security.cert.CertificateException
-
createSSLContext
protected javax.net.ssl.SSLContext createSSLContext() throws java.security.NoSuchAlgorithmException
Override this method to create and/or configure theSSLContext
used by theConnectionFactory
.- Returns:
- The
SSLContext
. - Throws:
java.security.NoSuchAlgorithmException
- if the algorithm is not available.- Since:
- 1.4.4
-
-