Spring AMQP

org.springframework.amqp.rabbit.listener
Class AbstractRabbitListeningContainer

java.lang.Object
  extended by org.springframework.amqp.rabbit.support.RabbitAccessor
      extended by org.springframework.amqp.rabbit.listener.AbstractRabbitListeningContainer
All Implemented Interfaces:
BeanNameAware, DisposableBean, InitializingBean, Lifecycle, Phased, SmartLifecycle
Direct Known Subclasses:
AbstractMessageListenerContainer

public abstract class AbstractRabbitListeningContainer
extends RabbitAccessor
implements BeanNameAware, DisposableBean, SmartLifecycle

Author:
Mark Pollack

Nested Class Summary
static class AbstractRabbitListeningContainer.SharedConnectionNotInitializedException
          Exception that indicates that the initial setup of this container's shared Rabbit Connection failed.
 
Field Summary
 
Fields inherited from class org.springframework.amqp.rabbit.support.RabbitAccessor
logger
 
Constructor Summary
AbstractRabbitListeningContainer()
           
 
Method Summary
 void afterPropertiesSet()
          Delegates to validateConfiguration() and initialize().
protected  Connection createSharedConnection()
          Create a shared Connection for this container.
 void destroy()
          Calls shutdown() when the BeanFactory destroys the container instance.
protected abstract  void doInitialize()
          Register any invokers within this container.
protected abstract  void doShutdown()
          Close the registered invokers.
protected  void doStart()
          Start the shared Connection, if any, and notify all invoker tasks.
protected  void doStop()
          Notify all invoker tasks and stop the shared Connection, if any.
protected  void establishSharedConnection()
          Establish a shared Connection for this container.
protected  String getBeanName()
          Return the bean name that this listener container has been assigned in its containing bean factory, if any.
 int getPhase()
          Return the phase in which this container will be started and stopped.
protected  Connection getSharedConnection()
          Return the shared Rabbit Connection maintained by this container.
 void initialize()
          Initialize this container.
 boolean isActive()
          Return whether this container is currently active, that is, whether it has been set up but not shut down yet.
 boolean isAutoStartup()
           
 boolean isRunning()
          Determine whether this container is currently running, that is, whether it has been started and not stopped yet.
protected  void prepareSharedConnection(Connection connection)
          Prepare the given Connection, which is about to be registered as shared Connection for this container.
protected  void refreshSharedConnection()
          Refresh the shared Connection that this container holds.
protected  boolean runningAllowed()
          Check whether this container's listeners are generally allowed to run.
 void setAutoStartup(boolean autoStartup)
          Set whether to automatically start the container after initialization.
 void setBeanName(String beanName)
           
 void setPhase(int phase)
          Specify the phase in which this container should be started and stopped.
protected abstract  boolean sharedConnectionEnabled()
          Return whether a shared Rabbit Connection should be maintained by this container base class.
 void shutdown()
          Stop the shared Connection, call doShutdown(), and close this container.
 void start()
          Start this container.
 void stop()
          Stop this container.
 void stop(Runnable callback)
           
protected  void stopSharedConnection()
          Stop the shared Connection, logging any exception thrown by Rabbit API methods.
protected  void validateConfiguration()
          Validate the configuration of this container.
 
Methods inherited from class org.springframework.amqp.rabbit.support.RabbitAccessor
convertRabbitAccessException, createChannel, createConnection, getChannel, getConnection, getConnectionFactory, getTransactionalResourceHolder, isChannelTransacted, setChannelTransacted, setConnectionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractRabbitListeningContainer

public AbstractRabbitListeningContainer()
Method Detail

setAutoStartup

public void setAutoStartup(boolean autoStartup)
Set whether to automatically start the container after initialization.

Default is "true"; set this to "false" to allow for manual startup through the start() method.


isAutoStartup

public boolean isAutoStartup()
Specified by:
isAutoStartup in interface SmartLifecycle

setPhase

public void setPhase(int phase)
Specify the phase in which this container should be started and stopped. The startup order proceeds from lowest to highest, and the shutdown order is the reverse of that. By default this value is Integer.MAX_VALUE meaning that this container starts as late as possible and stops as soon as possible.


getPhase

public int getPhase()
Return the phase in which this container will be started and stopped.

Specified by:
getPhase in interface Phased

setBeanName

public void setBeanName(String beanName)
Specified by:
setBeanName in interface BeanNameAware

getBeanName

protected final String getBeanName()
Return the bean name that this listener container has been assigned in its containing bean factory, if any.


afterPropertiesSet

public void afterPropertiesSet()
Delegates to validateConfiguration() and initialize().

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class RabbitAccessor

validateConfiguration

protected void validateConfiguration()
Validate the configuration of this container.

The default implementation is empty. To be overridden in subclasses.


destroy

public void destroy()
Calls shutdown() when the BeanFactory destroys the container instance.

Specified by:
destroy in interface DisposableBean
See Also:
shutdown()

initialize

public void initialize()
Initialize this container.

Creates a Rabbit Connection and calls doInitialize().


shutdown

public void shutdown()
Stop the shared Connection, call doShutdown(), and close this container.


isActive

public final boolean isActive()
Return whether this container is currently active, that is, whether it has been set up but not shut down yet.


start

public void start()
Start this container.

Specified by:
start in interface Lifecycle
See Also:
doStart()

doStart

protected void doStart()
                throws Exception
Start the shared Connection, if any, and notify all invoker tasks.

Throws:
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException - if thrown by Rabbit API methods
Exception
See Also:
establishSharedConnection()

stop

public void stop()
Stop this container.

Specified by:
stop in interface Lifecycle
See Also:
doStop()

stop

public void stop(Runnable callback)
Specified by:
stop in interface SmartLifecycle

doStop

protected void doStop()
Notify all invoker tasks and stop the shared Connection, if any.

See Also:
stopSharedConnection()

isRunning

public final boolean isRunning()
Determine whether this container is currently running, that is, whether it has been started and not stopped yet.

Specified by:
isRunning in interface Lifecycle
See Also:
start(), stop(), runningAllowed()

runningAllowed

protected boolean runningAllowed()
Check whether this container's listeners are generally allowed to run.

This implementation always returns true; the default 'running' state is purely determined by start() / stop().

Subclasses may override this method to check against temporary conditions that prevent listeners from actually running. In other words, they may apply further restrictions to the 'running' state, returning false if such a restriction prevents listeners from running.


establishSharedConnection

protected void establishSharedConnection()
                                  throws Exception
Establish a shared Connection for this container.

The default implementation delegates to createSharedConnection(), which does one immediate attempt and throws an exception if it fails. Can be overridden to have a recovery process in place, retrying until a Connection can be successfully established.

Throws:
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException - if thrown by Rabbit API methods
Exception

refreshSharedConnection

protected final void refreshSharedConnection()
                                      throws Exception
Refresh the shared Connection that this container holds.

Called on startup and also after an infrastructure exception that occurred during invoker setup and/or execution.

Throws:
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException - if thrown by Rabbit API methods
Exception

createSharedConnection

protected Connection createSharedConnection()
                                     throws Exception
Create a shared Connection for this container.

The default implementation creates a standard Connection and prepares it through prepareSharedConnection(org.springframework.amqp.rabbit.connection.Connection).

Returns:
the prepared Connection
Throws:
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException - if the creation failed
Exception

prepareSharedConnection

protected void prepareSharedConnection(Connection connection)
Prepare the given Connection, which is about to be registered as shared Connection for this container.

The default implementation sets the specified client id, if any. Subclasses can override this to apply further settings.

Parameters:
connection - the Connection to prepare

stopSharedConnection

protected void stopSharedConnection()
Stop the shared Connection, logging any exception thrown by Rabbit API methods.


getSharedConnection

protected final Connection getSharedConnection()
Return the shared Rabbit Connection maintained by this container. Available after initialization.

Returns:
the shared Connection (never null)
Throws:
IllegalStateException - if this container does not maintain a shared Connection, or if the Connection hasn't been initialized yet
See Also:
sharedConnectionEnabled()

sharedConnectionEnabled

protected abstract boolean sharedConnectionEnabled()
Return whether a shared Rabbit Connection should be maintained by this container base class.

See Also:
getSharedConnection()

doInitialize

protected abstract void doInitialize()
                              throws Exception
Register any invokers within this container.

Subclasses need to implement this method for their specific invoker management process.

A shared Rabbit Connection

Throws:
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException
Exception
See Also:
getSharedConnection()

doShutdown

protected abstract void doShutdown()
Close the registered invokers.

Subclasses need to implement this method for their specific invoker management process.

A shared Rabbit Connection, if any, will automatically be closed afterwards.

See Also:
shutdown()

Spring AMQP

Copyright © 2010. All Rights Reserved.