Class ConnectionFactory

java.lang.Object
com.rabbitmq.client.ConnectionFactory
All Implemented Interfaces:
Cloneable

public class ConnectionFactory extends Object implements Cloneable
Convenience "factory" class to facilitate opening a Connection to an AMQP broker.
  • Field Details

    • DEFAULT_USER

      public static final String DEFAULT_USER
      Default user name
      See Also:
    • DEFAULT_PASS

      public static final String DEFAULT_PASS
      Default password
      See Also:
    • DEFAULT_VHOST

      public static final String DEFAULT_VHOST
      Default virtual host
      See Also:
    • DEFAULT_CHANNEL_MAX

      public static final int DEFAULT_CHANNEL_MAX
      Default maximum channel number; zero for unlimited
      See Also:
    • DEFAULT_FRAME_MAX

      public static final int DEFAULT_FRAME_MAX
      Default maximum frame size; zero means no limit
      See Also:
    • DEFAULT_HEARTBEAT

      public static final int DEFAULT_HEARTBEAT
      Default heart-beat interval; 60 seconds
      See Also:
    • DEFAULT_INBOUND_MESSAGE_BODY_SIZE

      public static final int DEFAULT_INBOUND_MESSAGE_BODY_SIZE
      Default maximum body size of inbound messages: 64 MiB.
      See Also:
    • DEFAULT_HOST

      public static final String 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_PORT
      The default non-ssl port
      See Also:
    • DEFAULT_AMQP_OVER_SSL_PORT

      public static final int DEFAULT_AMQP_OVER_SSL_PORT
      The default ssl port
      See Also:
    • DEFAULT_CONNECTION_TIMEOUT

      public static final int DEFAULT_CONNECTION_TIMEOUT
      The default TCP connection timeout: 60 seconds
      See Also:
    • DEFAULT_HANDSHAKE_TIMEOUT

      public static final int DEFAULT_HANDSHAKE_TIMEOUT
      The 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_TIMEOUT
      The default shutdown timeout; zero means wait indefinitely
      See Also:
  • Constructor Details

    • ConnectionFactory

      public ConnectionFactory()
  • Method Details

    • getHost

      public String getHost()
      Returns:
      the default host to use for connections
    • setHost

      public void setHost(String host)
      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

      public String getUsername()
      Retrieve the user name.
      Returns:
      the AMQP user name to use when connecting to the broker
    • setUsername

      public void setUsername(String username)
      Set the user name.
      Parameters:
      username - the AMQP user name to use when connecting to the broker
    • getPassword

      public String getPassword()
      Retrieve the password.
      Returns:
      the password to use when connecting to the broker
    • setPassword

      public void setPassword(String password)
      Set the password.
      Parameters:
      password - the password to use when connecting to the broker
    • getVirtualHost

      public String getVirtualHost()
      Retrieve the virtual host.
      Returns:
      the virtual host to use when connecting to the broker
    • setVirtualHost

      public void setVirtualHost(String virtualHost)
      Set the virtual host.
      Parameters:
      virtualHost - the virtual host to use when connecting to the broker
    • setUri

      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:
      URISyntaxException
      NoSuchAlgorithmException
      KeyManagementException
    • setUri

      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:
      URISyntaxException
      NoSuchAlgorithmException
      KeyManagementException
    • 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

      public Map<String,Object> 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

      public void setClientProperties(Map<String,Object> clientProperties)
      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

      public SaslConfig getSaslConfig()
      Gets the sasl config to use when authenticating
      Returns:
      the sasl config
      See Also:
    • setSaslConfig

      public void setSaslConfig(SaslConfig saslConfig)
      Sets the sasl config to use when authenticating
      Parameters:
      saslConfig -
      See Also:
    • getSocketFactory

      public SocketFactory getSocketFactory()
      Retrieve the socket factory used to make connections with.
    • setSocketFactory

      public void setSocketFactory(SocketFactory factory)
      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

      public SocketConfigurator getSocketConfigurator()
      Get the socket configurator.
      See Also:
    • setSocketConfigurator

      public void setSocketConfigurator(SocketConfigurator socketConfigurator)
      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
    • setSharedExecutor

      public void setSharedExecutor(ExecutorService executor)
      Set the executor to use for consumer operation dispatch by default for newly created connections. 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 consumer operation
    • setShutdownExecutor

      public void setShutdownExecutor(ExecutorService executor)
      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

      public void setHeartbeatExecutor(ScheduledExecutorService executor)
      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

      public ThreadFactory getThreadFactory()
      Retrieve the thread factory used to instantiate new threads.
      See Also:
    • setThreadFactory

      public void setThreadFactory(ThreadFactory threadFactory)
      Set the thread factory used to instantiate new threads.
      See Also:
    • getExceptionHandler

      public ExceptionHandler getExceptionHandler()
      Get the exception handler.
      See Also:
    • setExceptionHandler

      public void setExceptionHandler(ExceptionHandler exceptionHandler)
      Set the exception handler to use for newly created connections.
      See Also:
    • isSSL

      public boolean isSSL()
    • useSslProtocol

      public void useSslProtocol() throws NoSuchAlgorithmException, KeyManagementException
      Convenience method for setting up an SSL socket factory, using the JVM default SSL context and default trust store, with server hostname verification enabled.
      Throws:
      NoSuchAlgorithmException
      KeyManagementException
    • useSslProtocol

      public void useSslProtocol(String protocol) throws NoSuchAlgorithmException, KeyManagementException
      Convenience method for setting up an SSL socket factory, using the supplied protocol and the JVM default trust store, with server hostname verification enabled.
      Throws:
      NoSuchAlgorithmException
      KeyManagementException
    • 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:
      NoSuchAlgorithmException
      KeyManagementException
    • useSslProtocol

      public void useSslProtocol(SSLContext context)
      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

      public void useTlsWithNoVerification() throws NoSuchAlgorithmException, KeyManagementException
      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.

      Throws:
      NoSuchAlgorithmException
      KeyManagementException
    • 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

      public static String computeDefaultTlsProcotol(String[] supportedProtocols)
    • 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

      protected FrameHandlerFactory createFrameHandlerFactory() throws IOException
      Throws:
      IOException
    • newConnection

      public Connection newConnection(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 be Recoverable. 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 problem
      TimeoutException
    • 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 be Recoverable. 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
      clientProvidedName - 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 problem
      TimeoutException
    • newConnection

      public Connection newConnection(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 be Recoverable. 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 problem
      TimeoutException
    • 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 be Recoverable. 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
      clientProvidedName - 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 problem
      TimeoutException
    • 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 be Recoverable. Future reconnection attempts will pick a random accessible address from the provided list.
      Parameters:
      executor - thread execution service for consumers on the connection
      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 problem
      TimeoutException
      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 be Recoverable. Future reconnection attempts will pick a random accessible address from the provided list.
      Parameters:
      executor - thread execution service for consumers on the connection
      addrs - an array of known broker addresses (hostname/port pairs) to try in order
      clientProvidedName - 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 problem
      TimeoutException
      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 be Recoverable. Future reconnection attempts will pick a random accessible address from the provided list.
      Parameters:
      executor - thread execution service for consumers on the connection
      addrs - 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 problem
      TimeoutException
      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 be Recoverable. Future reconnection attempts will pick a random accessible address from the provided list.
      Parameters:
      executor - thread execution service for consumers on the connection
      addrs - a List of known broker addrs (hostname/port pairs) to try in order
      clientProvidedName - 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 problem
      TimeoutException
      See Also:
    • params

      public ConnectionParams params(ExecutorService consumerWorkServiceExecutor)
    • newConnection

      public Connection newConnection() throws IOException, TimeoutException
      Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will be Recoverable. Reconnection attempts will always use the address configured on ConnectionFactory.
      Returns:
      an interface to the connection
      Throws:
      IOException - if it encounters a problem
      TimeoutException
    • newConnection

      public Connection newConnection(String connectionName) throws IOException, TimeoutException
      Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will be Recoverable. Reconnection attempts will always use the address configured on ConnectionFactory.
      Parameters:
      connectionName - arbitrary sring for connection name client property
      Returns:
      an interface to the connection
      Throws:
      IOException - if it encounters a problem
      TimeoutException
    • newConnection

      public Connection newConnection(ExecutorService executor) throws IOException, TimeoutException
      Create a new broker connection. If automatic connection recovery is enabled, the connection returned by this method will be Recoverable. Reconnection attempts will always use the address configured on ConnectionFactory.
      Parameters:
      executor - thread execution service for consumers on the connection
      Returns:
      an interface to the connection
      Throws:
      IOException - if it encounters a problem
      TimeoutException
    • 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 be Recoverable. Reconnection attempts will always use the address configured on ConnectionFactory.
      Parameters:
      executor - thread execution service for consumers on the connection
      connectionName - arbitrary sring for connection name client property
      Returns:
      an interface to the connection
      Throws:
      IOException - if it encounters a problem
      TimeoutException
    • clone

      public ConnectionFactory clone()
      Overrides:
      clone in class Object
    • 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