org.apache.qpid.client
Class AMQConnection

java.lang.Object
  extended by org.apache.qpid.client.Closeable
      extended by org.apache.qpid.client.AMQConnection
All Implemented Interfaces:
Connection, QueueConnection, TopicConnection, Referenceable
Direct Known Subclasses:
XAConnectionImpl

public class AMQConnection
extends Closeable
implements Connection, QueueConnection, TopicConnection, Referenceable


Nested Class Summary
static class AMQConnection.ChannelToSessionMap
           
 
Field Summary
protected  boolean _connected
           
protected  AMQConnectionDelegate _delegate
           
protected  ExceptionListener _exceptionListener
           
protected  FailoverPolicy _failoverPolicy
          Policy dictating how to failover
protected  long _maximumChannelCount
          A channel is roughly analogous to a session.
protected  AMQProtocolHandler _protocolHandler
          The protocol handler dispatches protocol events for this connection.
protected  boolean _started
          Whether this connection is started, i.e.
 
Fields inherited from class org.apache.qpid.client.Closeable
_closed, _closing
 
Constructor Summary
  AMQConnection(ConnectionURL connectionURL, SSLConfiguration sslConfig)
           
  AMQConnection(String connection)
           
  AMQConnection(String host, int port, boolean useSSL, String username, String password, String clientName, String virtualHost, SSLConfiguration sslConfig)
           
  AMQConnection(String host, int port, String username, String password, String clientName, String virtualHost)
           
  AMQConnection(String host, int port, String username, String password, String clientName, String virtualHost, SSLConfiguration sslConfig)
           
  AMQConnection(String connection, SSLConfiguration sslConfig)
           
protected AMQConnection(String username, String password, String clientName, String virtualHost)
           
  AMQConnection(String broker, String username, String password, String clientName, String virtualHost)
           
  AMQConnection(String broker, String username, String password, String clientName, String virtualHost, SSLConfiguration sslConfig)
           
 
Method Summary
 boolean attemptReconnection()
           
 boolean attemptReconnection(String host, int port)
           
 void blockUntilNotFailingOver()
          If failover is taking place this will block until it has completed.
 void bytesReceived(long receivedBytes)
           
 void bytesSent(long writtenBytes)
           
 boolean channelLimitReached()
           
protected  boolean checkException(Throwable thrown)
           
 void close()
          Closes this object.
 void close(List<AMQSession> sessions, long timeout)
           
 void close(long timeout)
           
 ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool sessionPool, int maxMessages)
           
 ConnectionConsumer createConnectionConsumer(Queue queue, String messageSelector, ServerSessionPool sessionPool, int maxMessages)
           
 ConnectionConsumer createConnectionConsumer(Topic topic, String messageSelector, ServerSessionPool sessionPool, int maxMessages)
           
 ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages)
           
 QueueSession createQueueSession(boolean transacted, int acknowledgeMode)
          Returns an AMQQueueSessionAdaptor which wraps an AMQSession and throws IllegalStateExceptions where specified in the JMS spec
 Session createSession(boolean transacted, int acknowledgeMode)
           
 Session createSession(boolean transacted, int acknowledgeMode, int prefetch)
          Create a session specifying the prefetch limit of messages.
 Session createSession(boolean transacted, int acknowledgeMode, int prefetchHigh, int prefetchLow)
          Create a session specifying the prefetch limit of messages.
 TopicSession createTopicSession(boolean transacted, int acknowledgeMode)
          Returns an AMQTopicSessionAdapter which wraps an AMQSession and throws IllegalStateExceptions where specified in the JMS spec
 void deregisterSession(int channelId)
           
 void exceptionReceived(Throwable cause)
          Invoked by the AMQProtocolSession when a protocol session exception has occurred.
<T,E extends Exception>
T
executeRetrySupport(FailoverProtectedOperation<T,E> operation)
           
 boolean failoverAllowed()
           
 void failoverPrep()
           
 void fireFailoverComplete()
          Fires a failover complete event to the registered connection listener (if any).
 boolean firePreFailover(boolean redirect)
          Fire the preFailover event to the registered connection listener (if any)
 boolean firePreResubscribe()
          Fire the preResubscribe event to the registered connection listener (if any).
 BrokerDetails getActiveBrokerDetails()
          Get the details of the currently active broker
 String getClientID()
           
 ConnectionListener getConnectionListener()
          Get the connection listener that has been registered with this connection, if any
 org.apache.qpid.framing.AMQShortString getDefaultQueueExchangeName()
           
 org.apache.qpid.framing.AMQShortString getDefaultTopicExchangeName()
           
 ExceptionListener getExceptionListener()
           
 Object getFailoverMutex()
          In order to protect the consistency of the connection and its child sessions, consumers and producers, the "failover mutex" must be held when doing any operations that could be corrupted during failover.
 FailoverPolicy getFailoverPolicy()
           
 long getMaximumChannelCount()
           
 long getMaximumFrameSize()
           
 long getMaxPrefetch()
          Get the maximum number of messages that this connection can pre-fetch.
 ConnectionMetaData getMetaData()
           
 int getNextChannelID()
           
 String getPassword()
           
 AMQProtocolHandler getProtocolHandler()
           
 org.apache.qpid.framing.ProtocolVersion getProtocolVersion()
           
 Reference getReference()
           
 AMQSession getSession(int channelId)
           
 AMQConnection.ChannelToSessionMap getSessions()
           
 SSLConfiguration getSSLConfiguration()
           
 boolean getSyncAck()
          Indicates whether we need to sync on every message ack
 boolean getSyncPersistence()
          Indicates whether persistent messages are synchronized
 String getSyncPublish()
           
 org.apache.qpid.framing.AMQShortString getTemporaryQueueExchangeName()
           
 org.apache.qpid.framing.AMQShortString getTemporaryTopicExchangeName()
           
 String getUsername()
           
 String getVirtualHost()
           
 boolean isFailingOver()
           
 boolean isUseLegacyMapMessageFormat()
           
 org.apache.qpid.framing.ProtocolVersion makeBrokerConnection(BrokerDetails brokerDetail)
           
 void performConnectionTask(Runnable task)
           
(package private)  void registerSession(int channelId, AMQSession session)
           
 void resubscribeSessions()
           
 void setClientID(String clientID)
           
 void setConnectionListener(ConnectionListener listener)
           
 void setDefaultQueueExchangeName(org.apache.qpid.framing.AMQShortString defaultQueueExchangeName)
           
 void setDefaultTopicExchangeName(org.apache.qpid.framing.AMQShortString defaultTopicExchangeName)
           
 void setExceptionListener(ExceptionListener listener)
           
 void setFailoverPolicy(FailoverPolicy policy)
           
 void setMaximumChannelCount(long maximumChannelCount)
           
 void setMaximumFrameSize(long frameMax)
           
 void setTemporaryQueueExchangeName(org.apache.qpid.framing.AMQShortString temporaryQueueExchangeName)
           
 void setTemporaryTopicExchangeName(org.apache.qpid.framing.AMQShortString temporaryTopicExchangeName)
           
 void setUsername(String id)
           
 void start()
          Start the connection, i.e.
 boolean started()
           
 void stop()
           
 String toString()
           
 String toURL()
           
 
Methods inherited from class org.apache.qpid.client.Closeable
checkNotClosed, isClosed, isClosing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_maximumChannelCount

protected long _maximumChannelCount
A channel is roughly analogous to a session. The server can negotiate the maximum number of channels per session and we must prevent the client from opening too many. Zero means unlimited.


_protocolHandler

protected AMQProtocolHandler _protocolHandler
The protocol handler dispatches protocol events for this connection. For example, when the connection is dropped the handler deals with this. It also deals with the initial dispatch of any protocol frames to their appropriate handler.


_exceptionListener

protected ExceptionListener _exceptionListener

_started

protected volatile boolean _started
Whether this connection is started, i.e. whether messages are flowing to consumers. It has no meaning for message publication.


_failoverPolicy

protected FailoverPolicy _failoverPolicy
Policy dictating how to failover


_connected

protected boolean _connected

_delegate

protected AMQConnectionDelegate _delegate
Constructor Detail

AMQConnection

public AMQConnection(String broker,
                     String username,
                     String password,
                     String clientName,
                     String virtualHost)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Parameters:
broker - brokerdetails
username - username
password - password
clientName - clientid
virtualHost - virtualhost
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String broker,
                     String username,
                     String password,
                     String clientName,
                     String virtualHost,
                     SSLConfiguration sslConfig)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Parameters:
broker - brokerdetails
username - username
password - password
clientName - clientid
virtualHost - virtualhost
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String host,
                     int port,
                     String username,
                     String password,
                     String clientName,
                     String virtualHost)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String host,
                     int port,
                     String username,
                     String password,
                     String clientName,
                     String virtualHost,
                     SSLConfiguration sslConfig)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String host,
                     int port,
                     boolean useSSL,
                     String username,
                     String password,
                     String clientName,
                     String virtualHost,
                     SSLConfiguration sslConfig)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String connection)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(String connection,
                     SSLConfiguration sslConfig)
              throws org.apache.qpid.AMQException,
                     org.apache.qpid.url.URLSyntaxException
Throws:
org.apache.qpid.AMQException
org.apache.qpid.url.URLSyntaxException

AMQConnection

public AMQConnection(ConnectionURL connectionURL,
                     SSLConfiguration sslConfig)
              throws org.apache.qpid.AMQException
Throws:
org.apache.qpid.AMQException
Todo:
Some horrible stuff going on here with setting exceptions to be non-null to detect if an exception was thrown during the connection! Intention not clear. Use a flag anyway, not exceptions... Will fix soon.

AMQConnection

protected AMQConnection(String username,
                        String password,
                        String clientName,
                        String virtualHost)
Method Detail

checkException

protected boolean checkException(Throwable thrown)

attemptReconnection

public boolean attemptReconnection(String host,
                                   int port)

attemptReconnection

public boolean attemptReconnection()

makeBrokerConnection

public org.apache.qpid.framing.ProtocolVersion makeBrokerConnection(BrokerDetails brokerDetail)
                                                             throws IOException,
                                                                    org.apache.qpid.AMQException
Throws:
IOException
org.apache.qpid.AMQException

executeRetrySupport

public <T,E extends Exception> T executeRetrySupport(FailoverProtectedOperation<T,E> operation)
                      throws E extends Exception
Throws:
E extends Exception

getActiveBrokerDetails

public BrokerDetails getActiveBrokerDetails()
Get the details of the currently active broker

Returns:
null if no broker is active (i.e. no successful connection has been made, or the BrokerDetail instance otherwise

failoverAllowed

public boolean failoverAllowed()

createSession

public Session createSession(boolean transacted,
                             int acknowledgeMode)
                      throws JMSException
Specified by:
createSession in interface Connection
Throws:
JMSException

createSession

public Session createSession(boolean transacted,
                             int acknowledgeMode,
                             int prefetch)
                      throws JMSException
Description copied from interface: Connection
Create a session specifying the prefetch limit of messages.

prefetch - the maximum number of messages to buffer in the client. This applies as a total across all consumers
Returns:
Throws:
JMSException

createSession

public Session createSession(boolean transacted,
                             int acknowledgeMode,
                             int prefetchHigh,
                             int prefetchLow)
                      throws JMSException
Description copied from interface: Connection
Create a session specifying the prefetch limit of messages.

prefetchHigh - the maximum number of messages to buffer in the client. This applies as a total across all consumers
prefetchLow - the number of messages that must be in the buffer in the client to renable message flow. This applies as a total across all consumers
Returns:
Throws:
JMSException

setFailoverPolicy

public void setFailoverPolicy(FailoverPolicy policy)

getFailoverPolicy

public FailoverPolicy getFailoverPolicy()

createQueueSession

public QueueSession createQueueSession(boolean transacted,
                                       int acknowledgeMode)
                                throws JMSException
Returns an AMQQueueSessionAdaptor which wraps an AMQSession and throws IllegalStateExceptions where specified in the JMS spec

Specified by:
createQueueSession in interface QueueConnection
Parameters:
transacted -
acknowledgeMode -
Returns:
QueueSession
Throws:
JMSException

createTopicSession

public TopicSession createTopicSession(boolean transacted,
                                       int acknowledgeMode)
                                throws JMSException
Returns an AMQTopicSessionAdapter which wraps an AMQSession and throws IllegalStateExceptions where specified in the JMS spec

Specified by:
createTopicSession in interface TopicConnection
Parameters:
transacted -
acknowledgeMode -
Returns:
TopicSession
Throws:
JMSException

channelLimitReached

public boolean channelLimitReached()

getClientID

public String getClientID()
                   throws JMSException
Specified by:
getClientID in interface Connection
Throws:
JMSException

setClientID

public void setClientID(String clientID)
                 throws JMSException
Specified by:
setClientID in interface Connection
Throws:
JMSException

getMetaData

public ConnectionMetaData getMetaData()
                               throws JMSException
Specified by:
getMetaData in interface Connection
Throws:
JMSException

getExceptionListener

public ExceptionListener getExceptionListener()
                                       throws JMSException
Specified by:
getExceptionListener in interface Connection
Throws:
JMSException

setExceptionListener

public void setExceptionListener(ExceptionListener listener)
                          throws JMSException
Specified by:
setExceptionListener in interface Connection
Throws:
JMSException

start

public void start()
           throws JMSException
Start the connection, i.e. start flowing messages. Note that this method must be called only from a single thread and is not thread safe (which is legal according to the JMS specification).

Specified by:
start in interface Connection
Throws:
JMSException

stop

public void stop()
          throws JMSException
Specified by:
stop in interface Connection
Throws:
JMSException

close

public void close()
           throws JMSException
Description copied from class: Closeable
Closes this object.

Specified by:
close in interface Connection
Specified by:
close in class Closeable
Throws:
JMSException - If this cannot be closed for any reason.

close

public void close(long timeout)
           throws JMSException
Throws:
JMSException

close

public void close(List<AMQSession> sessions,
                  long timeout)
           throws JMSException
Throws:
JMSException

createConnectionConsumer

public ConnectionConsumer createConnectionConsumer(Destination destination,
                                                   String messageSelector,
                                                   ServerSessionPool sessionPool,
                                                   int maxMessages)
                                            throws JMSException
Specified by:
createConnectionConsumer in interface Connection
Throws:
JMSException

createConnectionConsumer

public ConnectionConsumer createConnectionConsumer(Queue queue,
                                                   String messageSelector,
                                                   ServerSessionPool sessionPool,
                                                   int maxMessages)
                                            throws JMSException
Specified by:
createConnectionConsumer in interface QueueConnection
Throws:
JMSException

createConnectionConsumer

public ConnectionConsumer createConnectionConsumer(Topic topic,
                                                   String messageSelector,
                                                   ServerSessionPool sessionPool,
                                                   int maxMessages)
                                            throws JMSException
Specified by:
createConnectionConsumer in interface TopicConnection
Throws:
JMSException

createDurableConnectionConsumer

public ConnectionConsumer createDurableConnectionConsumer(Topic topic,
                                                          String subscriptionName,
                                                          String messageSelector,
                                                          ServerSessionPool sessionPool,
                                                          int maxMessages)
                                                   throws JMSException
Specified by:
createDurableConnectionConsumer in interface Connection
Specified by:
createDurableConnectionConsumer in interface TopicConnection
Throws:
JMSException

getMaximumChannelCount

public long getMaximumChannelCount()
                            throws JMSException
Returns:
the maximum number of sessions supported by this Connection
Throws:
JMSException

setConnectionListener

public void setConnectionListener(ConnectionListener listener)

getConnectionListener

public ConnectionListener getConnectionListener()
Description copied from interface: Connection
Get the connection listener that has been registered with this connection, if any

Returns:
the listener or null if none has been set

setMaximumChannelCount

public void setMaximumChannelCount(long maximumChannelCount)

setMaximumFrameSize

public void setMaximumFrameSize(long frameMax)

getMaximumFrameSize

public long getMaximumFrameSize()

getSessions

public AMQConnection.ChannelToSessionMap getSessions()

getUsername

public String getUsername()

setUsername

public void setUsername(String id)

getPassword

public String getPassword()

getVirtualHost

public String getVirtualHost()

getProtocolHandler

public AMQProtocolHandler getProtocolHandler()

started

public boolean started()

bytesSent

public void bytesSent(long writtenBytes)

bytesReceived

public void bytesReceived(long receivedBytes)

firePreFailover

public boolean firePreFailover(boolean redirect)
Fire the preFailover event to the registered connection listener (if any)

Parameters:
redirect - true if this is the result of a redirect request rather than a connection error
Returns:
true if no listener or listener does not veto change

firePreResubscribe

public boolean firePreResubscribe()
                           throws JMSException
Fire the preResubscribe event to the registered connection listener (if any). If the listener vetoes resubscription then all the sessions are closed.

Returns:
true if no listener or listener does not veto resubscription.
Throws:
JMSException

fireFailoverComplete

public void fireFailoverComplete()
Fires a failover complete event to the registered connection listener (if any).


getFailoverMutex

public final Object getFailoverMutex()
In order to protect the consistency of the connection and its child sessions, consumers and producers, the "failover mutex" must be held when doing any operations that could be corrupted during failover.

Returns:
a mutex. Guaranteed never to change for the lifetime of this connection even if failover occurs.

failoverPrep

public void failoverPrep()

resubscribeSessions

public void resubscribeSessions()
                         throws JMSException,
                                org.apache.qpid.AMQException,
                                FailoverException
Throws:
JMSException
org.apache.qpid.AMQException
FailoverException

blockUntilNotFailingOver

public void blockUntilNotFailingOver()
                              throws InterruptedException
If failover is taking place this will block until it has completed. If failover is not taking place it will return immediately.

Throws:
InterruptedException

exceptionReceived

public void exceptionReceived(Throwable cause)
Invoked by the AMQProtocolSession when a protocol session exception has occurred. This method sends the exception to a JMS exception listener, if configured, and propagates the exception to sessions, which in turn will propagate to consumers. This allows synchronous consumers to have exceptions thrown to them.

Parameters:
cause - the exception

registerSession

void registerSession(int channelId,
                     AMQSession session)

deregisterSession

public void deregisterSession(int channelId)

toString

public String toString()
Overrides:
toString in class Object

toURL

public String toURL()

getReference

public Reference getReference()
                       throws NamingException
Specified by:
getReference in interface Referenceable
Throws:
NamingException

getSSLConfiguration

public SSLConfiguration getSSLConfiguration()

getDefaultTopicExchangeName

public org.apache.qpid.framing.AMQShortString getDefaultTopicExchangeName()

setDefaultTopicExchangeName

public void setDefaultTopicExchangeName(org.apache.qpid.framing.AMQShortString defaultTopicExchangeName)

getDefaultQueueExchangeName

public org.apache.qpid.framing.AMQShortString getDefaultQueueExchangeName()

setDefaultQueueExchangeName

public void setDefaultQueueExchangeName(org.apache.qpid.framing.AMQShortString defaultQueueExchangeName)

getTemporaryTopicExchangeName

public org.apache.qpid.framing.AMQShortString getTemporaryTopicExchangeName()

getTemporaryQueueExchangeName

public org.apache.qpid.framing.AMQShortString getTemporaryQueueExchangeName()

setTemporaryTopicExchangeName

public void setTemporaryTopicExchangeName(org.apache.qpid.framing.AMQShortString temporaryTopicExchangeName)

setTemporaryQueueExchangeName

public void setTemporaryQueueExchangeName(org.apache.qpid.framing.AMQShortString temporaryQueueExchangeName)

performConnectionTask

public void performConnectionTask(Runnable task)

getSession

public AMQSession getSession(int channelId)

getProtocolVersion

public org.apache.qpid.framing.ProtocolVersion getProtocolVersion()

isFailingOver

public boolean isFailingOver()

getMaxPrefetch

public long getMaxPrefetch()
Get the maximum number of messages that this connection can pre-fetch.

Returns:
The maximum number of messages that this connection can pre-fetch.

getSyncPersistence

public boolean getSyncPersistence()
Indicates whether persistent messages are synchronized

Returns:
true if persistent messages are synchronized false otherwise

getSyncAck

public boolean getSyncAck()
Indicates whether we need to sync on every message ack


getSyncPublish

public String getSyncPublish()

getNextChannelID

public int getNextChannelID()

isUseLegacyMapMessageFormat

public boolean isUseLegacyMapMessageFormat()


Licensed to the Apache Software Foundation