Package com.rabbitmq.client
Class ConnectionFactory
java.lang.Object
com.rabbitmq.client.ConnectionFactory
- All Implemented Interfaces:
Cloneable
Convenience "factory" class to facilitate opening a
Connection to an AMQP broker.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default ssl portstatic final intThe default non-ssl portstatic final intDefault maximum channel number; zero for unlimitedstatic final intThe default TCP connection timeout: 60 secondsstatic final intDefault maximum frame size; zero means no limitstatic final intThe default AMQP 0-9-1 connection handshake timeout.static final intDefault heart-beat interval; 60 secondsstatic final StringThe default hoststatic final intDefault maximum body size of inbound messages: 64 MiB.static final StringDefault passwordstatic final intThe default shutdown timeout; zero means wait indefinitelystatic final StringDefault user namestatic final StringDefault virtual hoststatic final int'Use the default port' port -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()static StringcomputeDefaultTlsProcotol(String[] supportedProtocols) protected FrameHandlerFactoryvoidEnables server hostname verification (TLS endpoint identification) for TLS connections created by this connection factory: the server certificate must be valid for the host the client connects to.Retrieve the currently-configured table of client properties that will be sent to the server during connection startup.intRetrieve the TCP connection timeout.Get the exception handler.intRetrieve the AMQP 0-9-1 protocol handshake timeout.getHost()intMaximum body size of inbound (received) messages in bytes.longReturns automatic connection recovery interval in milliseconds.Retrieve the password.intgetPort()intRetrieve the requested maximum channel numberintRetrieve the requested maximum frame sizeintRetrieve the requested heartbeat interval.Gets the sasl config to use when authenticatingintRetrieve the shutdown timeout.Get the socket configurator.Retrieve the socket factory used to make connections with.Retrieve the thread factory used to instantiate new threads.Retrieve the user name.Retrieve the virtual host.booleanReturns true if automatic connection recovery is enabled, false otherwisebooleanisSSL()booleanReturns true if topology recovery is enabled, false otherwiseCreate a new broker connection.newConnection(Address[] addrs) Create a new broker connection, picking the first available address from the list.newConnection(Address[] addrs, String clientProvidedName) Create a new broker connection with a client-provided name, picking the first available address from the list.newConnection(String connectionName) Create a new broker connection.newConnection(ExecutorService executor) Create a new broker connection.newConnection(ExecutorService executor, Address[] addrs) Create a new broker connection, picking the first available address from the list.newConnection(ExecutorService executor, Address[] addrs, String clientProvidedName) Create a new broker connection with a client-provided name, picking the first available address from the list.newConnection(ExecutorService executor, String connectionName) Create a new broker connection.newConnection(ExecutorService executor, List<Address> addrs) Create a new broker connection, picking the first available address from the list.newConnection(ExecutorService executor, List<Address> addrs, String clientProvidedName) Create a new broker connection with a client-provided name, picking the first available address from the list.newConnection(List<Address> addrs) Create a new broker connection, picking the first available address from the list.newConnection(List<Address> addrs, String clientProvidedName) Create a new broker connection with a client-provided name, picking the first available address from the list.params(ExecutorService consumerWorkServiceExecutor) static intportOrDefault(int port, boolean ssl) voidsetAutomaticRecoveryEnabled(boolean automaticRecovery) Enables or disables automatic connection recovery.voidsetClientProperties(Map<String, Object> clientProperties) Replace the table of client properties that will be sent to the server during subsequent connection startups.voidsetConnectionTimeout(int timeout) Set the TCP connection timeout.voidsetExceptionHandler(ExceptionHandler exceptionHandler) Set the exception handler to use for newly created connections.voidsetHandshakeTimeout(int timeout) Set the AMQP0-9-1 protocol handshake timeout.voidsetHeartbeatExecutor(ScheduledExecutorService executor) Set the executor to use to send heartbeat frames.voidvoidsetMaxInboundMessageBodySize(int maxInboundMessageBodySize) Set the maximum body size of inbound (received) messages in bytes; larger messages are rejected with an exception instead of being allocated, to avoid running out of memory.voidsetNetworkRecoveryInterval(int networkRecoveryInterval) Sets connection recovery interval.voidsetNetworkRecoveryInterval(long networkRecoveryInterval) Sets connection recovery interval.voidsetPassword(String password) Set the password.voidsetPort(int port) Set the target port.voidsetRequestedChannelMax(int requestedChannelMax) Set the requested maximum channel numbervoidsetRequestedFrameMax(int requestedFrameMax) Set the requested maximum frame sizevoidsetRequestedHeartbeat(int requestedHeartbeat) Set the requested heartbeat timeout.voidsetSaslConfig(SaslConfig saslConfig) Sets the sasl config to use when authenticatingvoidsetSharedExecutor(ExecutorService executor) Set the executor to use for consumer operation dispatch by default for newly created connections.voidsetShutdownExecutor(ExecutorService executor) Set the executor to use for connection shutdown.voidsetShutdownTimeout(int shutdownTimeout) Set the shutdown timeout.voidsetSocketConfigurator(SocketConfigurator socketConfigurator) Set the socket configurator.voidsetSocketFactory(SocketFactory factory) Set the socket factory used to make connections with.voidsetThreadFactory(ThreadFactory threadFactory) Set the thread factory used to instantiate new threads.voidsetTopologyRecoveryEnabled(boolean topologyRecovery) Enables or disables topology recoveryvoidConvenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host.voidConvenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host.voidsetUsername(String username) Set the user name.voidsetVirtualHost(String virtualHost) Set the virtual host.voidConvenience method for setting up an SSL socket factory, using the JVM default SSL context and default trust store, with server hostname verification enabled.voiduseSslProtocol(String protocol) Convenience method for setting up an SSL socket factory, using the supplied protocol and the JVM default trust store, with server hostname verification enabled.voiduseSslProtocol(String protocol, TrustManager trustManager) Convenience method for setting up an SSL socket factory.voiduseSslProtocol(SSLContext context) Convenience method for setting up an SSL socket factory.voidConfigure TLS without any certificate or hostname verification.
-
Field Details
-
DEFAULT_USER
Default user name- See Also:
-
DEFAULT_PASS
Default password- See Also:
-
DEFAULT_VHOST
Default virtual host- See Also:
-
DEFAULT_CHANNEL_MAX
public static final int DEFAULT_CHANNEL_MAXDefault maximum channel number; zero for unlimited- See Also:
-
DEFAULT_FRAME_MAX
public static final int DEFAULT_FRAME_MAXDefault maximum frame size; zero means no limit- See Also:
-
DEFAULT_HEARTBEAT
public static final int DEFAULT_HEARTBEATDefault heart-beat interval; 60 seconds- See Also:
-
DEFAULT_INBOUND_MESSAGE_BODY_SIZE
public static final int DEFAULT_INBOUND_MESSAGE_BODY_SIZEDefault maximum body size of inbound messages: 64 MiB.- See Also:
-
DEFAULT_HOST
The default host- See Also:
-
USE_DEFAULT_PORT
public static final int USE_DEFAULT_PORT'Use the default port' port- See Also:
-
DEFAULT_AMQP_PORT
public static final int DEFAULT_AMQP_PORTThe default non-ssl port- See Also:
-
DEFAULT_AMQP_OVER_SSL_PORT
public static final int DEFAULT_AMQP_OVER_SSL_PORTThe default ssl port- See Also:
-
DEFAULT_CONNECTION_TIMEOUT
public static final int DEFAULT_CONNECTION_TIMEOUTThe default TCP connection timeout: 60 seconds- See Also:
-
DEFAULT_HANDSHAKE_TIMEOUT
public static final int DEFAULT_HANDSHAKE_TIMEOUTThe default AMQP 0-9-1 connection handshake timeout. See DEFAULT_CONNECTION_TIMEOUT for TCP (socket) connection timeout.- See Also:
-
DEFAULT_SHUTDOWN_TIMEOUT
public static final int DEFAULT_SHUTDOWN_TIMEOUTThe default shutdown timeout; zero means wait indefinitely- See Also:
-
-
Constructor Details
-
ConnectionFactory
public ConnectionFactory()
-
-
Method Details
-
getHost
- Returns:
- the default host to use for connections
-
setHost
- Parameters:
host- the default host to use for connections
-
portOrDefault
public static int portOrDefault(int port, boolean ssl) -
getPort
public int getPort()- Returns:
- the default port to use for connections
-
setPort
public void setPort(int port) Set the target port.- Parameters:
port- the default port to use for connections
-
getUsername
Retrieve the user name.- Returns:
- the AMQP user name to use when connecting to the broker
-
setUsername
Set the user name.- Parameters:
username- the AMQP user name to use when connecting to the broker
-
getPassword
Retrieve the password.- Returns:
- the password to use when connecting to the broker
-
setPassword
Set the password.- Parameters:
password- the password to use when connecting to the broker
-
getVirtualHost
Retrieve the virtual host.- Returns:
- the virtual host to use when connecting to the broker
-
setVirtualHost
Set the virtual host.- Parameters:
virtualHost- the virtual host to use when connecting to the broker
-
setUri
public void setUri(URI uri) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException Convenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host. If any part of the URI is ommited, the ConnectionFactory's corresponding variable is left unchanged.- Parameters:
uri- is the AMQP URI containing the data- Throws:
URISyntaxExceptionNoSuchAlgorithmExceptionKeyManagementException
-
setUri
public void setUri(String uriString) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException Convenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host. If any part of the URI is ommited, the ConnectionFactory's corresponding variable is left unchanged. Note that not all valid AMQP URIs are accepted; in particular, the hostname must be given if the port, username or password are given, and escapes in the hostname are not permitted.- Parameters:
uriString- is the AMQP URI containing the data- Throws:
URISyntaxExceptionNoSuchAlgorithmExceptionKeyManagementException
-
getRequestedChannelMax
public int getRequestedChannelMax()Retrieve the requested maximum channel number- Returns:
- the initially requested maximum channel number; zero for unlimited
-
setRequestedChannelMax
public void setRequestedChannelMax(int requestedChannelMax) Set the requested maximum channel number- Parameters:
requestedChannelMax- initially requested maximum channel number; zero for unlimited
-
getRequestedFrameMax
public int getRequestedFrameMax()Retrieve the requested maximum frame size- Returns:
- the initially requested maximum frame size, in octets; zero for unlimited
-
setRequestedFrameMax
public void setRequestedFrameMax(int requestedFrameMax) Set the requested maximum frame size- Parameters:
requestedFrameMax- initially requested maximum frame size, in octets; zero for unlimited
-
getRequestedHeartbeat
public int getRequestedHeartbeat()Retrieve the requested heartbeat interval.- Returns:
- the initially requested heartbeat interval, in seconds; zero for none
-
setConnectionTimeout
public void setConnectionTimeout(int timeout) Set the TCP connection timeout.- Parameters:
timeout- connection TCP establishment timeout in milliseconds; zero for infinite
-
getConnectionTimeout
public int getConnectionTimeout()Retrieve the TCP connection timeout.- Returns:
- the TCP connection timeout, in milliseconds; zero for infinite
-
getHandshakeTimeout
public int getHandshakeTimeout()Retrieve the AMQP 0-9-1 protocol handshake timeout.- Returns:
- the AMQP0-9-1 protocol handshake timeout, in milliseconds
-
setHandshakeTimeout
public void setHandshakeTimeout(int timeout) Set the AMQP0-9-1 protocol handshake timeout.- Parameters:
timeout- the AMQP0-9-1 protocol handshake timeout, in milliseconds
-
setShutdownTimeout
public void setShutdownTimeout(int shutdownTimeout) Set the shutdown timeout. This is the amount of time that Consumer implementations have to continue working through deliveries (and other Consumer callbacks) after the connection has closed but before the ConsumerWorkService is torn down. If consumers exceed this timeout then any remaining queued deliveries (and other Consumer callbacks, including the Consumer's handleShutdownSignal() invocation) will be lost.- Parameters:
shutdownTimeout- shutdown timeout in milliseconds; zero for infinite; default 10000
-
getShutdownTimeout
public int getShutdownTimeout()Retrieve the shutdown timeout.- Returns:
- the shutdown timeout, in milliseconds; zero for infinite
-
setRequestedHeartbeat
public void setRequestedHeartbeat(int requestedHeartbeat) Set the requested heartbeat timeout. Heartbeat frames will be sent at about 1/2 the timeout interval.- Parameters:
requestedHeartbeat- the initially requested heartbeat timeout, in seconds; zero for none- See Also:
-
getMaxInboundMessageBodySize
public int getMaxInboundMessageBodySize()Maximum body size of inbound (received) messages in bytes.Default value is 67,108,864 (64 MiB).
-
setMaxInboundMessageBodySize
public void setMaxInboundMessageBodySize(int maxInboundMessageBodySize) Set the maximum body size of inbound (received) messages in bytes; larger messages are rejected with an exception instead of being allocated, to avoid running out of memory.Default value is 67,108,864 (64 MiB).
- Parameters:
maxInboundMessageBodySize- the maximum size of inbound messages
-
getClientProperties
Retrieve the currently-configured table of client properties that will be sent to the server during connection startup. Clients may add, delete, and alter keys in this table. Such changes will take effect when the next new connection is started using this factory.- Returns:
- the map of client properties
- See Also:
-
setClientProperties
Replace the table of client properties that will be sent to the server during subsequent connection startups.- Parameters:
clientProperties- the map of extra client properties- See Also:
-
getSaslConfig
Gets the sasl config to use when authenticating- Returns:
- the sasl config
- See Also:
-
setSaslConfig
Sets the sasl config to use when authenticating- Parameters:
saslConfig-- See Also:
-
getSocketFactory
Retrieve the socket factory used to make connections with. -
setSocketFactory
Set the socket factory used to make connections with. Can be used to enable SSL connections by passing in a javax.net.ssl.SSLSocketFactory instance.- See Also:
-
getSocketConfigurator
Get the socket configurator.- See Also:
-
setSocketConfigurator
Set the socket configurator. This gets a chance to "configure" a socket before it has been opened. The default socket configurator disables Nagle's algorithm.- Parameters:
socketConfigurator- the configurator to use
-
setShutdownExecutor
Set the executor to use for connection shutdown. All connections that use this executor share it. It's developer's responsibility to shut down the executor when it is no longer needed.- Parameters:
executor- executor service to be used for connection shutdown
-
setHeartbeatExecutor
Set the executor to use to send heartbeat frames. All connections that use this executor share it. It's developer's responsibility to shut down the executor when it is no longer needed.- Parameters:
executor- executor service to be used to send heartbeat
-
getThreadFactory
Retrieve the thread factory used to instantiate new threads.- See Also:
-
setThreadFactory
Set the thread factory used to instantiate new threads.- See Also:
-
getExceptionHandler
Get the exception handler.- See Also:
-
setExceptionHandler
Set the exception handler to use for newly created connections.- See Also:
-
isSSL
public boolean isSSL() -
useSslProtocol
Convenience method for setting up an SSL socket factory, using the JVM default SSL context and default trust store, with server hostname verification enabled. -
useSslProtocol
Convenience method for setting up an SSL socket factory, using the supplied protocol and the JVM default trust store, with server hostname verification enabled. -
useSslProtocol
public void useSslProtocol(String protocol, TrustManager trustManager) throws NoSuchAlgorithmException, KeyManagementException Convenience method for setting up an SSL socket factory. Pass in the SSL protocol to use, e.g. "TLSv1" or "TLSv1.2".- Parameters:
protocol- SSL protocol to use.- Throws:
NoSuchAlgorithmExceptionKeyManagementException
-
useSslProtocol
Convenience method for setting up an SSL socket factory. Pass in an initialized SSLContext. Server hostname verification is enabled for sockets created by this connection factory.- Parameters:
context- An initialized SSLContext
-
useTlsWithNoVerification
Configure TLS without any certificate or hostname verification.DO NOT USE IN PRODUCTION. This disables all server authentication and provides no protection against man-in-the-middle attacks. Use only in local development or CI environments where the broker identity is not sensitive.
-
enableHostnameVerification
public void enableHostnameVerification()Enables server hostname verification (TLS endpoint identification) for TLS connections created by this connection factory: the server certificate must be valid for the host the client connects to.This is applied by default by the
useSslProtocol()family of methods. -
computeDefaultTlsProcotol
-
isAutomaticRecoveryEnabled
public boolean isAutomaticRecoveryEnabled()Returns true if automatic connection recovery is enabled, false otherwise- Returns:
- true if automatic connection recovery is enabled, false otherwise
- See Also:
-
setAutomaticRecoveryEnabled
public void setAutomaticRecoveryEnabled(boolean automaticRecovery) Enables or disables automatic connection recovery.- Parameters:
automaticRecovery- if true, enables connection recovery- See Also:
-
isTopologyRecoveryEnabled
public boolean isTopologyRecoveryEnabled()Returns true if topology recovery is enabled, false otherwise- Returns:
- true if topology recovery is enabled, false otherwise
- See Also:
-
setTopologyRecoveryEnabled
public void setTopologyRecoveryEnabled(boolean topologyRecovery) Enables or disables topology recovery- Parameters:
topologyRecovery- if true, enables topology recovery- See Also:
-
createFrameHandlerFactory
- Throws:
IOException
-
newConnection
Create a new broker connection, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
addrs- an array of known broker addresses (hostname/port pairs) to try in order- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
public Connection newConnection(Address[] addrs, String clientProvidedName) throws IOException, TimeoutException Create a new broker connection with a client-provided name, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
addrs- an array of known broker addresses (hostname/port pairs) to try in orderclientProvidedName- application-specific connection name, will be displayed in the management UI if RabbitMQ server supports it. This value doesn't have to be unique and cannot be used as a connection identifier e.g. in HTTP API requests. This value is supposed to be human-readable.- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
Create a new broker connection, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
addrs- a List of known broker addresses (hostname/port pairs) to try in order- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
public Connection newConnection(List<Address> addrs, String clientProvidedName) throws IOException, TimeoutException Create a new broker connection with a client-provided name, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
addrs- a List of known broker addresses (hostname/port pairs) to try in orderclientProvidedName- application-specific connection name, will be displayed in the management UI if RabbitMQ server supports it. This value doesn't have to be unique and cannot be used as a connection identifier e.g. in HTTP API requests. This value is supposed to be human-readable.- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
public Connection newConnection(ExecutorService executor, Address[] addrs) throws IOException, TimeoutException Create a new broker connection, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
executor- thread execution service for consumers on the connectionaddrs- an array of known broker addresses (hostname/port pairs) to try in order- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException- See Also:
-
newConnection
public Connection newConnection(ExecutorService executor, Address[] addrs, String clientProvidedName) throws IOException, TimeoutException Create a new broker connection with a client-provided name, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
executor- thread execution service for consumers on the connectionaddrs- an array of known broker addresses (hostname/port pairs) to try in orderclientProvidedName- application-specific connection name, will be displayed in the management UI if RabbitMQ server supports it. This value doesn't have to be unique and cannot be used as a connection identifier e.g. in HTTP API requests. This value is supposed to be human-readable.- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException- See Also:
-
newConnection
public Connection newConnection(ExecutorService executor, List<Address> addrs) throws IOException, TimeoutException Create a new broker connection, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
executor- thread execution service for consumers on the connectionaddrs- a List of known broker addrs (hostname/port pairs) to try in order- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException- See Also:
-
newConnection
public Connection newConnection(ExecutorService executor, List<Address> addrs, String clientProvidedName) throws IOException, TimeoutException Create a new broker connection with a client-provided name, picking the first available address from the list. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Future reconnection attempts will pick a random accessible address from the provided list.- Parameters:
executor- thread execution service for consumers on the connectionaddrs- a List of known broker addrs (hostname/port pairs) to try in orderclientProvidedName- application-specific connection name, will be displayed in the management UI if RabbitMQ server supports it. This value doesn't have to be unique and cannot be used as a connection identifier e.g. in HTTP API requests. This value is supposed to be human-readable.- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException- See Also:
-
params
-
newConnection
Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Reconnection attempts will always use the address configured onConnectionFactory.- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Reconnection attempts will always use the address configured onConnectionFactory.- Parameters:
connectionName- arbitrary sring for connection name client property- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Reconnection attempts will always use the address configured onConnectionFactory.- Parameters:
executor- thread execution service for consumers on the connection- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
newConnection
public Connection newConnection(ExecutorService executor, String connectionName) throws IOException, TimeoutException Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will beRecoverable. Reconnection attempts will always use the address configured onConnectionFactory.- Parameters:
executor- thread execution service for consumers on the connectionconnectionName- arbitrary sring for connection name client property- Returns:
- an interface to the connection
- Throws:
IOException- if it encounters a problemTimeoutException
-
clone
-
getNetworkRecoveryInterval
public long getNetworkRecoveryInterval()Returns automatic connection recovery interval in milliseconds.- Returns:
- how long will automatic recovery wait before attempting to reconnect, in ms; default is 5000
-
setNetworkRecoveryInterval
public void setNetworkRecoveryInterval(int networkRecoveryInterval) Sets connection recovery interval. Default is 5000.- Parameters:
networkRecoveryInterval- how long will automatic recovery wait before attempting to reconnect, in ms
-
setNetworkRecoveryInterval
public void setNetworkRecoveryInterval(long networkRecoveryInterval) Sets connection recovery interval. Default is 5000.- Parameters:
networkRecoveryInterval- how long will automatic recovery wait before attempting to reconnect, in ms
-