@ApplicationScoped
public class ConnectionProducer
extends java.lang.Object
A single connection factory provides ONE SINGLE connection to a RabbitMQ message broker via TCP.
It is recommended by the RabbitMQ documentation (v2.7) to use one single connection within a client and to use one channel for every client thread.
| Modifier and Type | Field and Description |
|---|---|
static int |
CONNECTION_ESTABLISH_INTERVAL_IN_MS |
static int |
CONNECTION_HEARTBEAT_IN_SEC |
static int |
CONNECTION_TIMEOUT_IN_MS |
| Constructor and Description |
|---|
ConnectionProducer() |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the connection factory and interrupts all threads associated to it.
|
com.rabbitmq.client.Connection |
getConnection(ConnectionConfig config)
Gets a new connection from the factory.
|
void |
registerConnectionListener(ConnectionConfig config,
ConnectionListener listener)
Registers a connection listener at the factory which is notified about changes of connection
states.
|
void |
removeConnectionListener(ConnectionConfig config,
ConnectionListener listener)
Removes a connection listener from the factory.
|
public static final int CONNECTION_HEARTBEAT_IN_SEC
public static final int CONNECTION_TIMEOUT_IN_MS
public static final int CONNECTION_ESTABLISH_INTERVAL_IN_MS
public com.rabbitmq.client.Connection getConnection(ConnectionConfig config) throws java.io.IOException, java.util.concurrent.TimeoutException, java.security.NoSuchAlgorithmException
Gets a new connection from the factory. As this factory only provides one connection for every process, the connection is established on the first call of this method. Every subsequent call will return the same instance of the first established connection.
In case a connection is lost, the factory will try to reestablish a new connection.
config - the connection configurationjava.io.IOException - if the connection handling failsjava.util.concurrent.TimeoutException - if the connection could not be established within the timeoutjava.security.NoSuchAlgorithmException - if the security context creation for secured connection fails@PreDestroy public void close()
Closes the connection factory and interrupts all threads associated to it.
Note: Make sure to close the connection factory when not used any more as otherwise the connection may remain established and ghost threads may reside.
public void registerConnectionListener(ConnectionConfig config, ConnectionListener listener)
config - the connection configurationlistener - The connection listenerpublic void removeConnectionListener(ConnectionConfig config, ConnectionListener listener)
config - the connection configurationlistener - The connection listener