public interface Connection extends ShutdownNotifier, Closeable
To connect to a broker, fill in a ConnectionFactory
and use a ConnectionFactory
as 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.
Modifier and Type | Method and Description |
---|---|
void |
abort()
Abort this connection and all its channels
with the
AMQP.REPLY_SUCCESS close code
and message 'OK'. |
void |
abort(int timeout)
Abort this connection and all its channels
with the
AMQP.REPLY_SUCCESS close code
and message 'OK'. |
void |
abort(int closeCode,
String closeMessage)
Abort this connection and all its channels.
|
void |
abort(int closeCode,
String closeMessage,
int timeout)
Abort this connection and all its channels.
|
BlockedListener |
addBlockedListener(BlockedCallback blockedCallback,
UnblockedCallback unblockedCallback)
Add a lambda-based
BlockedListener . |
void |
addBlockedListener(BlockedListener listener)
Add a
BlockedListener . |
void |
clearBlockedListeners()
Remove all
BlockedListener s. |
void |
close()
Close this connection and all its channels
with the
AMQP.REPLY_SUCCESS close code
and message 'OK'. |
void |
close(int timeout)
Close this connection and all its channels
with the
AMQP.REPLY_SUCCESS close code
and message 'OK'. |
void |
close(int closeCode,
String closeMessage)
Close this connection and all its channels.
|
void |
close(int closeCode,
String closeMessage,
int timeout)
Close this connection and all its channels.
|
Channel |
createChannel()
Create a new channel, using an internally allocated channel number.
|
Channel |
createChannel(int channelNumber)
Create a new channel, using the specified channel number if possible.
|
InetAddress |
getAddress()
Retrieve the host.
|
int |
getChannelMax()
Get the negotiated maximum channel number.
|
Map<String,Object> |
getClientProperties()
Get a copy of the map of client properties sent to the server
|
String |
getClientProvidedName()
Returns client-provided connection name, if any.
|
ExceptionHandler |
getExceptionHandler()
Get the exception handler.
|
int |
getFrameMax()
Get the negotiated maximum frame size.
|
int |
getHeartbeat()
Get the negotiated heartbeat interval.
|
String |
getId()
Returns a unique ID for this connection.
|
int |
getPort()
Retrieve the port number.
|
Map<String,Object> |
getServerProperties()
Retrieve the server properties.
|
default Optional<Channel> |
openChannel()
Create a new channel wrapped in an
Optional . |
default Optional<Channel> |
openChannel(int channelNumber)
Create a new channel, using the specified channel number if possible.
|
boolean |
removeBlockedListener(BlockedListener listener)
Remove a
BlockedListener . |
void |
setId(String id)
Sets a unique ID for this connection.
|
addShutdownListener, getCloseReason, isOpen, notifyListeners, removeShutdownListener
InetAddress getAddress()
int getPort()
int getChannelMax()
int getFrameMax()
int getHeartbeat()
Map<String,Object> getClientProperties()
String getClientProvidedName()
ConnectionFactory.newConnection(Address[], String)
,
ConnectionFactory.newConnection(ExecutorService, Address[], String)
Map<String,Object> getServerProperties()
Channel createChannel() throws IOException
Recoverable
.
Use openChannel()
if you want to use an Optional
to deal
with a value.
IOException
- if an I/O problem is encounteredChannel createChannel(int channelNumber) throws IOException
Use openChannel(int)
if you want to use an Optional
to deal
with a value.
channelNumber
- the channel number to allocateIOException
- if an I/O problem is encountereddefault Optional<Channel> openChannel() throws IOException
Optional
.
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 a Channel
or null
.
Optional
containing the channel;
never null
but potentially empty if no channel is availableIOException
- if an I/O problem is encounteredcreateChannel()
default Optional<Channel> openChannel(int channelNumber) throws IOException
Use createChannel(int)
to return directly a Channel
or null
.
channelNumber
- the channel number to allocateOptional
containing the channel,
never null
but potentially empty if this channel number is already in useIOException
- if an I/O problem is encounteredcreateChannel(int)
void close() throws IOException
AMQP.REPLY_SUCCESS
close code
and message 'OK'.
Waits for all the close operations to complete.close
in interface AutoCloseable
close
in interface Closeable
IOException
- if an I/O problem is encounteredvoid close(int closeCode, String closeMessage) throws IOException
closeCode
- the close code (See under "Reply Codes" in the AMQP specification)closeMessage
- a message indicating the reason for closing the connectionIOException
- if an I/O problem is encounteredvoid close(int timeout) throws IOException
AMQP.REPLY_SUCCESS
close code
and message 'OK'.
This method behaves in a similar way as close()
, 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.timeout
- timeout (in milliseconds) for completing all the close-related
operations, use -1 for infinityIOException
- if an I/O problem is encounteredvoid close(int closeCode, String closeMessage, int timeout) throws IOException
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 infinityIOException
- if an I/O problem is encounteredvoid abort()
AMQP.REPLY_SUCCESS
close code
and message 'OK'.
Forces the connection to close.
Any encountered exceptions in the close operations are silently discarded.void abort(int closeCode, String closeMessage)
closeCode
- the close code (See under "Reply Codes" in the AMQP specification)closeMessage
- a message indicating the reason for closing the connectionvoid abort(int timeout)
AMQP.REPLY_SUCCESS
close code
and message 'OK'.
This method behaves in a similar way as abort()
, 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.timeout
- timeout (in milliseconds) for completing all the close-related
operations, use -1 for infinityvoid abort(int closeCode, String closeMessage, int timeout)
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 infinityvoid addBlockedListener(BlockedListener listener)
BlockedListener
.listener
- the listener to addBlockedListener addBlockedListener(BlockedCallback blockedCallback, UnblockedCallback unblockedCallback)
BlockedListener
.blockedCallback
- the callback when the connection is blockedunblockedCallback
- the callback when the connection is unblockedBlockedListener
,
BlockedCallback
,
UnblockedCallback
boolean removeBlockedListener(BlockedListener listener)
BlockedListener
.listener
- the listener to removetrue
if the listener was found and removed,
false
otherwisevoid clearBlockedListeners()
BlockedListener
s.ExceptionHandler getExceptionHandler()
ExceptionHandler
String getId()
void setId(String id)
Copyright © 2022 VMware, Inc. or its affiliates.. All rights reserved.