Package com.rabbitmq.client
Interface Connection
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable,ShutdownNotifier
- All Known Subinterfaces:
RecoverableConnection
- All Known Implementing Classes:
AMQConnection,AutorecoveringConnection,RecoveryAwareAMQConnection
public interface Connection extends ShutdownNotifier, java.io.Closeable
Public API: Interface to an AMQ connection. See the see the spec for details.To connect to a broker, fill in a
ConnectionFactoryand use aConnectionFactoryas follows:ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); factory.setPort(portNumber); factory.setVirtualHost(virtualHost); factory.setUsername(username); factory.setPassword(password); Connection conn = factory.newConnection(); // Then open a channel: Channel channel = conn.createChannel();
Or, more compactly:ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://username:password@hostName:portNumber/virtualHost"); Connection conn = factory.newConnection(); Channel channel = conn.createChannel()Current implementations are thread-safe for code at the client API level, and in fact thread-safe internally except for code within RPC calls.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidabort()Abort this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'.voidabort(int timeout)Abort this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'.voidabort(int closeCode, java.lang.String closeMessage)Abort this connection and all its channels.voidabort(int closeCode, java.lang.String closeMessage, int timeout)Abort this connection and all its channels.BlockedListeneraddBlockedListener(BlockedCallback blockedCallback, UnblockedCallback unblockedCallback)Add a lambda-basedBlockedListener.voidaddBlockedListener(BlockedListener listener)Add aBlockedListener.voidclearBlockedListeners()Remove allBlockedListeners.voidclose()Close this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'.voidclose(int timeout)Close this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'.voidclose(int closeCode, java.lang.String closeMessage)Close this connection and all its channels.voidclose(int closeCode, java.lang.String closeMessage, int timeout)Close this connection and all its channels.ChannelcreateChannel()Create a new channel, using an internally allocated channel number.ChannelcreateChannel(int channelNumber)Create a new channel, using the specified channel number if possible.java.net.InetAddressgetAddress()Retrieve the host.intgetChannelMax()Get the negotiated maximum channel number.java.util.Map<java.lang.String,java.lang.Object>getClientProperties()Get a copy of the map of client properties sent to the serverjava.lang.StringgetClientProvidedName()Returns client-provided connection name, if any.ExceptionHandlergetExceptionHandler()Get the exception handler.intgetFrameMax()Get the negotiated maximum frame size.intgetHeartbeat()Get the negotiated heartbeat interval.java.lang.StringgetId()Returns a unique ID for this connection.intgetPort()Retrieve the port number.java.util.Map<java.lang.String,java.lang.Object>getServerProperties()Retrieve the server properties.default java.util.Optional<Channel>openChannel()Create a new channel wrapped in anOptional.default java.util.Optional<Channel>openChannel(int channelNumber)Create a new channel, using the specified channel number if possible.booleanremoveBlockedListener(BlockedListener listener)Remove aBlockedListener.voidsetId(java.lang.String id)Sets a unique ID for this connection.-
Methods inherited from interface com.rabbitmq.client.ShutdownNotifier
addShutdownListener, getCloseReason, isOpen, notifyListeners, removeShutdownListener
-
-
-
-
Method Detail
-
getAddress
java.net.InetAddress getAddress()
Retrieve the host.- Returns:
- the hostname of the peer we're connected to.
-
getPort
int getPort()
Retrieve the port number.- Returns:
- the port number of the peer we're connected to.
-
getChannelMax
int getChannelMax()
Get the negotiated maximum channel number. Usable channel numbers range from 1 to this number, inclusive.- Returns:
- the maximum channel number permitted for this connection.
-
getFrameMax
int getFrameMax()
Get the negotiated maximum frame size.- Returns:
- the maximum frame size, in octets; zero if unlimited
-
getHeartbeat
int getHeartbeat()
Get the negotiated heartbeat interval.- Returns:
- the heartbeat interval, in seconds; zero if none
-
getClientProperties
java.util.Map<java.lang.String,java.lang.Object> getClientProperties()
Get a copy of the map of client properties sent to the server- Returns:
- a copy of the map of client properties
-
getClientProvidedName
java.lang.String getClientProvidedName()
Returns client-provided connection name, if any. Note that the value returned does not uniquely identify a connection and cannot be used as a connection identifier in HTTP API requests.- Returns:
- client-provided connection name, if any
- See Also:
ConnectionFactory.newConnection(Address[], String),ConnectionFactory.newConnection(ExecutorService, Address[], String)
-
getServerProperties
java.util.Map<java.lang.String,java.lang.Object> getServerProperties()
Retrieve the server properties.- Returns:
- a map of the server properties. This typically includes the product name and version of the server.
-
createChannel
Channel createChannel() throws java.io.IOException
Create a new channel, using an internally allocated channel number. If automatic connection recovery is enabled, the channel returned by this method will beRecoverable.Use
openChannel()if you want to use anOptionalto deal with a value.- Returns:
- a new channel descriptor, or null if none is available
- Throws:
java.io.IOException- if an I/O problem is encountered
-
createChannel
Channel createChannel(int channelNumber) throws java.io.IOException
Create a new channel, using the specified channel number if possible.Use
openChannel(int)if you want to use anOptionalto deal with a value.- Parameters:
channelNumber- the channel number to allocate- Returns:
- a new channel descriptor, or null if this channel number is already in use
- Throws:
java.io.IOException- if an I/O problem is encountered
-
openChannel
default java.util.Optional<Channel> openChannel() throws java.io.IOException
Create a new channel wrapped in anOptional. The channel number is allocated internally.If automatic connection recovery is enabled, the channel returned by this method will be
Recoverable.Use
createChannel()to return directly aChannelornull.- Returns:
- an
Optionalcontaining the channel; nevernullbut potentially empty if no channel is available - Throws:
java.io.IOException- if an I/O problem is encountered- Since:
- 5.6.0
- See Also:
createChannel()
-
openChannel
default java.util.Optional<Channel> openChannel(int channelNumber) throws java.io.IOException
Create a new channel, using the specified channel number if possible.Use
createChannel(int)to return directly aChannelornull.- Parameters:
channelNumber- the channel number to allocate- Returns:
- an
Optionalcontaining the channel, nevernullbut potentially empty if this channel number is already in use - Throws:
java.io.IOException- if an I/O problem is encountered- Since:
- 5.6.0
- See Also:
createChannel(int)
-
close
void close() throws java.io.IOExceptionClose this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'. Waits for all the close operations to complete.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- if an I/O problem is encountered
-
close
void close(int closeCode, java.lang.String closeMessage) throws java.io.IOExceptionClose this connection and all its channels. Waits for all the close operations to complete.- Parameters:
closeCode- the close code (See under "Reply Codes" in the AMQP specification)closeMessage- a message indicating the reason for closing the connection- Throws:
java.io.IOException- if an I/O problem is encountered
-
close
void close(int timeout) throws java.io.IOExceptionClose this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'. This method behaves in a similar way asclose(), with the only difference that it waits with a provided timeout for all the close operations to complete. When timeout is reached the socket is forced to close.- Parameters:
timeout- timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity- Throws:
java.io.IOException- if an I/O problem is encountered
-
close
void close(int closeCode, java.lang.String closeMessage, int timeout) throws java.io.IOExceptionClose this connection and all its channels. Waits with the given timeout for all the close operations to complete. When timeout is reached the socket is forced to close.- Parameters:
closeCode- the close code (See under "Reply Codes" in the AMQP specification)closeMessage- a message indicating the reason for closing the connectiontimeout- timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity- Throws:
java.io.IOException- if an I/O problem is encountered
-
abort
void abort()
Abort this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'. Forces the connection to close. Any encountered exceptions in the close operations are silently discarded.
-
abort
void abort(int closeCode, java.lang.String closeMessage)Abort this connection and all its channels. Forces the connection to close and waits for all the close operations to complete. Any encountered exceptions in the close operations are silently discarded.- Parameters:
closeCode- the close code (See under "Reply Codes" in the AMQP specification)closeMessage- a message indicating the reason for closing the connection
-
abort
void abort(int timeout)
Abort this connection and all its channels with theAMQP.REPLY_SUCCESSclose code and message 'OK'. This method behaves in a similar way asabort(), with the only difference that it waits with a provided timeout for all the close operations to complete. When timeout is reached the socket is forced to close.- Parameters:
timeout- timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity
-
abort
void abort(int closeCode, java.lang.String closeMessage, int timeout)Abort this connection and all its channels. Forces the connection to close and waits with the given timeout for all the close operations to complete. When timeout is reached the socket is forced to close. Any encountered exceptions in the close operations are silently discarded.- Parameters:
closeCode- the close code (See under "Reply Codes" in the AMQP specification)closeMessage- a message indicating the reason for closing the connectiontimeout- timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity
-
addBlockedListener
void addBlockedListener(BlockedListener listener)
Add aBlockedListener.- Parameters:
listener- the listener to add
-
addBlockedListener
BlockedListener addBlockedListener(BlockedCallback blockedCallback, UnblockedCallback unblockedCallback)
Add a lambda-basedBlockedListener.- Parameters:
blockedCallback- the callback when the connection is blockedunblockedCallback- the callback when the connection is unblocked- Returns:
- the listener that wraps the callback
- See Also:
BlockedListener,BlockedCallback,UnblockedCallback
-
removeBlockedListener
boolean removeBlockedListener(BlockedListener listener)
Remove aBlockedListener.- Parameters:
listener- the listener to remove- Returns:
trueif the listener was found and removed,falseotherwise
-
clearBlockedListeners
void clearBlockedListeners()
Remove allBlockedListeners.
-
getExceptionHandler
ExceptionHandler getExceptionHandler()
Get the exception handler.- See Also:
ExceptionHandler
-
getId
java.lang.String getId()
Returns a unique ID for this connection. This ID must be unique, otherwise some services like the metrics collector won't work properly. This ID doesn't have to be provided by the client, services that require it will be assigned automatically if not set.- Returns:
- unique ID for this connection.
-
setId
void setId(java.lang.String id)
Sets a unique ID for this connection. This ID must be unique, otherwise some services like the metrics collector won't work properly. This ID doesn't have to be provided by the client, services that require it will be assigned automatically if not set.
-
-