com.rabbitmq.client.impl
Class AMQConnection

java.lang.Object
  extended by com.rabbitmq.client.impl.ShutdownNotifierComponent
      extended by com.rabbitmq.client.impl.AMQConnection
All Implemented Interfaces:
Connection, ShutdownNotifier

public class AMQConnection
extends ShutdownNotifierComponent
implements Connection

Concrete class representing and managing an AMQP connection to a broker.

To create a broker connection, use ConnectionFactory. See Connection for an example.


Field Summary
 ChannelManager _channelManager
          Object that manages a set of channels
 java.util.Map<java.lang.String,java.lang.Object> _serverProperties
          Saved server properties field from connection.start
static int CONNECTION_CLOSING_TIMEOUT
          Timeout used while waiting for a connection.close-ok (milliseconds)
static int HANDSHAKE_TIMEOUT
          Timeout used while waiting for AMQP handshaking to complete (milliseconds)
 
Fields inherited from class com.rabbitmq.client.impl.ShutdownNotifierComponent
_shutdownCause, listeners
 
Constructor Summary
AMQConnection(ConnectionFactory factory, FrameHandler frameHandler)
          Construct a new connection to a broker.
AMQConnection(ConnectionFactory factory, FrameHandler frameHandler, ExceptionHandler exceptionHandler)
          Construct a new connection to a broker.
 
Method Summary
 void abort()
          Public API - Abort this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'.
 void abort(int timeout)
          Public API - Abort this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'.
 void abort(int closeCode, java.lang.String closeMessage)
          Public API - Abort this connection and all its channels.
 void abort(int closeCode, java.lang.String closeMessage, int timeout)
          Public API - Abort this connection and all its channels.
 void checkPreconditions()
          Private API - check required preconditions and protocol invariants
 void close()
          Public API - Close this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'.
 void close(int timeout)
          Public API - Close this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'.
 void close(int closeCode, java.lang.String closeMessage)
          Public API - Close this connection and all its channels.
 void close(int closeCode, java.lang.String closeMessage, boolean initiatedByApplication, java.lang.Throwable cause)
          Protected API - Delegates to the six-argument close method, passing 0 for the timeout, and false for the abort flag.
 void close(int closeCode, java.lang.String closeMessage, boolean initiatedByApplication, java.lang.Throwable cause, int timeout, boolean abort)
          Protected API - Close this connection with the given code, message, source and timeout value for all the close operations to complete.
 void close(int closeCode, java.lang.String closeMessage, int timeout)
          Public API - Close this connection and all its channels.
 Channel createChannel()
          Public API - Create a new channel, using an internally allocated channel number.
 Channel createChannel(int channelNumber)
          Public API - Create a new channel, using the specified channel number if possible.
static java.util.Map<java.lang.String,java.lang.Object> defaultClientProperties()
          Retrieve a copy of the default table of client properties that will be sent to the server during connection startup.
 void disconnectChannel(ChannelN channel)
          Protected API - respond, in the driver thread, to a ShutdownSignal.
 void ensureIsOpen()
           
 int getChannelMax()
          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 server
 ExceptionHandler getExceptionHandler()
          Protected API - retrieve the current ExceptionHandler
 FrameHandler getFrameHandler()
           
 int getFrameMax()
          Get the negotiated maximum frame size.
 int getHeartbeat()
          Get the negotiated heartbeat interval.
 java.lang.String getHost()
          Retrieve the host.
 int getPort()
          Retrieve the port number.
 java.util.Map<java.lang.String,java.lang.Object> getServerProperties()
          Retrieve the server properties.
 void handleConnectionClose(Command closeCommand)
           
 void handleSocketTimeout()
          Private API - Called when a frame-read operation times out.
 boolean processControlCommand(Command c)
          Handles incoming control commands on channel zero.
 Frame readFrame()
          Private API - reads a single frame from the connection to the broker, or returns null if the read times out.
 void setFrameMax(int value)
          Protected API - set the max frame size.
 void setHeartbeat(int heartbeat)
          Protected API - set the heartbeat timeout.
 ShutdownSignalException shutdown(java.lang.Object reason, boolean initiatedByApplication, java.lang.Throwable cause, boolean notifyRpc)
          Protected API - causes all attached channels to terminate with a ShutdownSignal built from the argument, and stops this connection from accepting further work from the application.
 void start()
          Start up the connection, including the MainLoop thread.
 java.lang.String toString()
           
 void writeFrame(Frame f)
          Public API - sends a frame directly to the broker.
 
Methods inherited from class com.rabbitmq.client.impl.ShutdownNotifierComponent
addShutdownListener, getCloseReason, isOpen, notifyListeners, removeShutdownListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.rabbitmq.client.ShutdownNotifier
addShutdownListener, getCloseReason, isOpen, notifyListeners, removeShutdownListener
 

Field Detail

HANDSHAKE_TIMEOUT

public static final int HANDSHAKE_TIMEOUT
Timeout used while waiting for AMQP handshaking to complete (milliseconds)

See Also:
Constant Field Values

CONNECTION_CLOSING_TIMEOUT

public static final int CONNECTION_CLOSING_TIMEOUT
Timeout used while waiting for a connection.close-ok (milliseconds)

See Also:
Constant Field Values

_channelManager

public ChannelManager _channelManager
Object that manages a set of channels


_serverProperties

public java.util.Map<java.lang.String,java.lang.Object> _serverProperties
Saved server properties field from connection.start

Constructor Detail

AMQConnection

public AMQConnection(ConnectionFactory factory,
                     FrameHandler frameHandler)
Construct a new connection to a broker.

Parameters:
factory - the initialization parameters for a connection
frameHandler - interface to an object that will handle the frame I/O for this connection

AMQConnection

public AMQConnection(ConnectionFactory factory,
                     FrameHandler frameHandler,
                     ExceptionHandler exceptionHandler)
Construct a new connection to a broker.

Parameters:
factory - the initialization parameters for a connection
frameHandler - interface to an object that will handle the frame I/O for this connection
exceptionHandler - interface to an object that will handle any special exceptions encountered while using this connection
Method Detail

defaultClientProperties

public static java.util.Map<java.lang.String,java.lang.Object> defaultClientProperties()
Retrieve a copy of the default table of client properties that will be sent to the server during connection startup. This method is called when each new ConnectionFactory instance is constructed.

Returns:
a map of client properties
See Also:
Connection.getClientProperties()

disconnectChannel

public final void disconnectChannel(ChannelN channel)
Protected API - respond, in the driver thread, to a ShutdownSignal.

Parameters:
channel - the channel to disconnect

ensureIsOpen

public void ensureIsOpen()
                  throws AlreadyClosedException
Throws:
AlreadyClosedException

getHost

public java.lang.String getHost()
Retrieve the host.

Specified by:
getHost in interface Connection
Returns:
the hostname of the peer we're connected to.

getPort

public int getPort()
Retrieve the port number.

Specified by:
getPort in interface Connection
Returns:
the port number of the peer we're connected to.

getFrameHandler

public FrameHandler getFrameHandler()

getServerProperties

public java.util.Map<java.lang.String,java.lang.Object> getServerProperties()
Retrieve the server properties.

Specified by:
getServerProperties in interface Connection
Returns:
a map of the server properties. This typically includes the product name and version of the server.

start

public void start()
           throws java.io.IOException
Start up the connection, including the MainLoop thread. Sends the protocol version negotiation header, and runs through Connection.Start/.StartOk, Connection.Tune/.TuneOk, and then calls Connection.Open and waits for the OpenOk. Sets heartbeat and frame max values after tuning has taken place.

Throws:
java.io.IOException - if an error is encountered

checkPreconditions

public void checkPreconditions()
Private API - check required preconditions and protocol invariants


getChannelMax

public int getChannelMax()
Get the negotiated maximum channel number. Usable channel numbers range from 1 to this number, inclusive.

Specified by:
getChannelMax in interface Connection
Returns:
the maximum channel number permitted for this connection.

getFrameMax

public int getFrameMax()
Get the negotiated maximum frame size.

Specified by:
getFrameMax in interface Connection
Returns:
the maximum frame size, in octets; zero if unlimited

setFrameMax

public void setFrameMax(int value)
Protected API - set the max frame size. Should only be called during tuning.


getHeartbeat

public int getHeartbeat()
Get the negotiated heartbeat interval.

Specified by:
getHeartbeat in interface Connection
Returns:
the heartbeat interval, in seconds; zero if none

setHeartbeat

public void setHeartbeat(int heartbeat)
Protected API - set the heartbeat timeout. Should only be called during tuning.


getClientProperties

public java.util.Map<java.lang.String,java.lang.Object> getClientProperties()
Description copied from interface: Connection
Get a copy of the map of client properties sent to the server

Specified by:
getClientProperties in interface Connection
Returns:
a copy of the map of client properties

getExceptionHandler

public ExceptionHandler getExceptionHandler()
Protected API - retrieve the current ExceptionHandler


createChannel

public Channel createChannel(int channelNumber)
                      throws java.io.IOException
Public API - Create a new channel, using the specified channel number if possible.

Specified by:
createChannel in interface Connection
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

createChannel

public Channel createChannel()
                      throws java.io.IOException
Public API - Create a new channel, using an internally allocated channel number.

Specified by:
createChannel in interface Connection
Returns:
a new channel descriptor, or null if none is available
Throws:
java.io.IOException - if an I/O problem is encountered

readFrame

public Frame readFrame()
                throws java.io.IOException
Private API - reads a single frame from the connection to the broker, or returns null if the read times out.

Throws:
java.io.IOException

writeFrame

public void writeFrame(Frame f)
                throws java.io.IOException
Public API - sends a frame directly to the broker.

Throws:
java.io.IOException

handleSocketTimeout

public void handleSocketTimeout()
                         throws MissedHeartbeatException
Private API - Called when a frame-read operation times out. Checks to see if too many heartbeats have been missed, and if so, throws MissedHeartbeatException.

Throws:
MissedHeartbeatException - if too many silent timeouts have gone by

processControlCommand

public boolean processControlCommand(Command c)
                              throws java.io.IOException
Handles incoming control commands on channel zero.

Throws:
java.io.IOException

handleConnectionClose

public void handleConnectionClose(Command closeCommand)

shutdown

public ShutdownSignalException shutdown(java.lang.Object reason,
                                        boolean initiatedByApplication,
                                        java.lang.Throwable cause,
                                        boolean notifyRpc)
Protected API - causes all attached channels to terminate with a ShutdownSignal built from the argument, and stops this connection from accepting further work from the application.

Returns:
a shutdown signal built using the given arguments

close

public void close()
           throws java.io.IOException
Public API - Close this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'. Waits for all the close operations to complete.

Specified by:
close in interface Connection
Throws:
java.io.IOException - if an I/O problem is encountered

close

public void close(int timeout)
           throws java.io.IOException
Public API - Close this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'. This method behaves in a similar way as Connection.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.

Specified by:
close in interface Connection
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

public void close(int closeCode,
                  java.lang.String closeMessage)
           throws java.io.IOException
Public API - Close this connection and all its channels. Waits for all the close operations to complete.

Specified by:
close in interface Connection
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

public void close(int closeCode,
                  java.lang.String closeMessage,
                  int timeout)
           throws java.io.IOException
Public API - Close 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.

Specified by:
close in interface Connection
Parameters:
closeCode - the close code (See under "Reply Codes" in the AMQP specification)
closeMessage - a message indicating the reason for closing the connection
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

abort

public void abort()
Public API - Abort this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'. Forces the connection to close. Any encountered exceptions in the close operations are silently discarded.

Specified by:
abort in interface Connection

abort

public void abort(int closeCode,
                  java.lang.String closeMessage)
Public API - 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.

Specified by:
abort in interface Connection
Parameters:
closeCode - the close code (See under "Reply Codes" in the AMQP specification)
closeMessage - a message indicating the reason for closing the connection

abort

public void abort(int timeout)
Public API - Abort this connection and all its channels with the AMQP.REPLY_SUCCESS close code and message 'OK'. This method behaves in a similar way as Connection.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.

Specified by:
abort in interface Connection
Parameters:
timeout - timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity

abort

public void abort(int closeCode,
                  java.lang.String closeMessage,
                  int timeout)
Public API - 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.

Specified by:
abort in interface Connection
Parameters:
closeCode - the close code (See under "Reply Codes" in the AMQP specification)
closeMessage - a message indicating the reason for closing the connection
timeout - timeout (in milliseconds) for completing all the close-related operations, use -1 for infinity

close

public void close(int closeCode,
                  java.lang.String closeMessage,
                  boolean initiatedByApplication,
                  java.lang.Throwable cause)
           throws java.io.IOException
Protected API - Delegates to the six-argument close method, passing 0 for the timeout, and false for the abort flag.

Throws:
java.io.IOException

close

public void close(int closeCode,
                  java.lang.String closeMessage,
                  boolean initiatedByApplication,
                  java.lang.Throwable cause,
                  int timeout,
                  boolean abort)
           throws java.io.IOException
Protected API - Close this connection with the given code, message, source and timeout value for all the close operations to complete. Specifies if any encountered exceptions should be ignored.

Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object