Spring AMQP

org.springframework.amqp.rabbit.listener
Class AbstractMessageListenerContainer

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

public abstract class AbstractMessageListenerContainer
extends AbstractRabbitListeningContainer

Author:
Mark Pollack, Mark Fisher

Nested Class Summary
 
Nested classes/interfaces inherited from class org.springframework.amqp.rabbit.listener.AbstractRabbitListeningContainer
AbstractRabbitListeningContainer.SharedConnectionNotInitializedException
 
Field Summary
 
Fields inherited from class org.springframework.amqp.rabbit.support.RabbitAccessor
logger
 
Constructor Summary
AbstractMessageListenerContainer()
           
 
Method Summary
protected  void checkMessageListener(Object messageListener)
          Check the given message listener, throwing an exception if it does not correspond to a supported listener type.
protected  void commitIfNecessary(com.rabbitmq.client.Channel channel, Message message)
          Perform a commit or message acknowledgement, as appropriate.
protected  void doExecuteListener(com.rabbitmq.client.Channel channel, Message message)
          Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).
protected  void doInvokeListener(ChannelAwareMessageListener listener, com.rabbitmq.client.Channel channel, Message message)
          Invoke the specified listener as Spring ChannelAwareMessageListener, exposing a new Rabbit Session (potentially with its own transaction) to the listener if demanded.
protected  void doInvokeListener(MessageListener listener, Message message)
          Invoke the specified listener as Spring Rabbit MessageListener.
protected  void executeListener(com.rabbitmq.client.Channel channel, Message message)
          Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).
 Object getMessageListener()
          Return the message listener object to register.
 String getQueueName()
          Return the name of the queue to receive messages from.
protected  String getRequiredQueueName()
           
protected  void handleListenerException(Throwable ex)
          Handle the given exception that arose during listener execution.
protected  void invokeErrorHandler(Throwable ex)
          Invoke the registered ErrorHandler, if any.
protected  void invokeListener(com.rabbitmq.client.Channel channel, Message message)
          Invoke the specified listener: either as standard MessageListener or (preferably) as SessionAwareMessageListener.
protected  boolean isChannelLocallyTransacted(com.rabbitmq.client.Channel channel)
          Check whether the given Channel is locally transacted, that is, whether its transaction is managed by this listener container's Channel handling and not by an external transaction coordinator.
 boolean isExposeListenerChannel()
          Return whether to expose the listener Channel to a registered ChannelAwareMessageListener.
protected  void rollbackIfNecessary(com.rabbitmq.client.Channel channel)
          Perform a rollback, if appropriate.
protected  void rollbackOnExceptionIfNecessary(com.rabbitmq.client.Channel channel, Message message, Throwable ex)
          Perform a rollback, handling rollback exceptions properly.
 void setErrorHandler(ErrorHandler errorHandler)
          Set an ErrorHandler to be invoked in case of any uncaught exceptions thrown while processing a Message.
 void setExposeListenerChannel(boolean exposeListenerChannel)
          Set whether to expose the listener Rabbit Channel to a registered ChannelAwareMessageListener as well as to RabbitTemplate calls.
 void setMessageListener(Object messageListener)
          Set the message listener implementation to register.
 void setQueueName(String queueName)
          Set the name of the queue to receive messages from.
 void setQueues(Queue... queues)
           
 
Methods inherited from class org.springframework.amqp.rabbit.listener.AbstractRabbitListeningContainer
afterPropertiesSet, createSharedConnection, destroy, doInitialize, doShutdown, doStart, doStop, establishSharedConnection, getBeanName, getPhase, getSharedConnection, initialize, isActive, isAutoStartup, isRunning, prepareSharedConnection, refreshSharedConnection, runningAllowed, setAutoStartup, setBeanName, setPhase, sharedConnectionEnabled, shutdown, start, stop, stop, stopSharedConnection, validateConfiguration
 
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

AbstractMessageListenerContainer

public AbstractMessageListenerContainer()
Method Detail

setQueueName

public void setQueueName(String queueName)
Set the name of the queue to receive messages from.

Parameters:
queueName - the desired queue (can not be null)

setQueues

public void setQueues(Queue... queues)

getQueueName

public String getQueueName()
Return the name of the queue to receive messages from.


getRequiredQueueName

protected String getRequiredQueueName()

isExposeListenerChannel

public boolean isExposeListenerChannel()
Return whether to expose the listener Channel to a registered ChannelAwareMessageListener.


setExposeListenerChannel

public void setExposeListenerChannel(boolean exposeListenerChannel)
Set whether to expose the listener Rabbit Channel to a registered ChannelAwareMessageListener as well as to RabbitTemplate calls.

Default is "true", reusing the listener's Channel. Turn this off to expose a fresh Rabbit Channel fetched from the same underlying Rabbit Connection instead.

Note that Channels managed by an external transaction manager will always get exposed to RabbitTemplate calls. So in terms of RabbitTemplate exposure, this setting only affects locally transacted Channels.

See Also:
ChannelAwareMessageListener

setMessageListener

public void setMessageListener(Object messageListener)
Set the message listener implementation to register. This can be either a Spring MessageListener object or a Spring ChannelAwareMessageListener object.

Throws:
IllegalArgumentException - if the supplied listener is not a MessageListener or a ChannelAwareMessageListener
See Also:
MessageListener, ChannelAwareMessageListener

checkMessageListener

protected void checkMessageListener(Object messageListener)
Check the given message listener, throwing an exception if it does not correspond to a supported listener type.

By default, only a Spring MessageListener object or a Spring SessionAwareMessageListener object will be accepted.

Parameters:
messageListener - the message listener object to check
Throws:
IllegalArgumentException - if the supplied listener is not a MessageListener or SessionAwareMessageListener
See Also:
MessageListener, ChannelAwareMessageListener

getMessageListener

public Object getMessageListener()
Return the message listener object to register.


setErrorHandler

public void setErrorHandler(ErrorHandler errorHandler)
Set an ErrorHandler to be invoked in case of any uncaught exceptions thrown while processing a Message. By default there will be no ErrorHandler so that error-level logging is the only result.


invokeErrorHandler

protected void invokeErrorHandler(Throwable ex)
Invoke the registered ErrorHandler, if any. Log at error level otherwise.

Parameters:
ex - the uncaught error that arose during Rabbit processing.
See Also:
setErrorHandler(org.springframework.util.ErrorHandler)

executeListener

protected void executeListener(com.rabbitmq.client.Channel channel,
                               Message message)
                        throws Throwable
Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).

Parameters:
channel - the Rabbit Channel to operate on
message - the received Rabbit Message
Throws:
Throwable
See Also:
invokeListener(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message), commitIfNecessary(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message), rollbackOnExceptionIfNecessary(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message, java.lang.Throwable), handleListenerException(java.lang.Throwable)

doExecuteListener

protected void doExecuteListener(com.rabbitmq.client.Channel channel,
                                 Message message)
                          throws Throwable
Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).

Parameters:
channel - the Rabbit Channel to operate on
message - the received Rabbit Message
Throws:
Throwable
Exception - if thrown by Rabbit API methods
See Also:
invokeListener(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message), commitIfNecessary(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message), rollbackOnExceptionIfNecessary(com.rabbitmq.client.Channel, org.springframework.amqp.core.Message, java.lang.Throwable), RabbitAccessor.convertRabbitAccessException(java.lang.Exception)

invokeListener

protected void invokeListener(com.rabbitmq.client.Channel channel,
                              Message message)
                       throws Exception
Invoke the specified listener: either as standard MessageListener or (preferably) as SessionAwareMessageListener.

Parameters:
channel - the Rabbit Channel to operate on
message - the received Rabbit Message
Throws:
Exception
Exception - if thrown by Rabbit API methods
See Also:
setMessageListener(java.lang.Object)

doInvokeListener

protected void doInvokeListener(ChannelAwareMessageListener listener,
                                com.rabbitmq.client.Channel channel,
                                Message message)
                         throws Exception
Invoke the specified listener as Spring ChannelAwareMessageListener, exposing a new Rabbit Session (potentially with its own transaction) to the listener if demanded.

Parameters:
listener - the Spring ChannelAwareMessageListener to invoke
channel - the Rabbit Channel to operate on
message - the received Rabbit Message
Throws:
Exception - if thrown by Rabbit API methods
See Also:
ChannelAwareMessageListener, setExposeListenerChannel(boolean)

doInvokeListener

protected void doInvokeListener(MessageListener listener,
                                Message message)
Invoke the specified listener as Spring Rabbit MessageListener.

Default implementation performs a plain invocation of the onMessage method.

Parameters:
listener - the Rabbit MessageListener to invoke
message - the received Rabbit Message
See Also:
MessageListener.onMessage(org.springframework.amqp.core.Message)

commitIfNecessary

protected void commitIfNecessary(com.rabbitmq.client.Channel channel,
                                 Message message)
                          throws IOException
Perform a commit or message acknowledgement, as appropriate.

Parameters:
channel - the Rabbit channel to commit
message - the Message to acknowledge
Throws:
IOException

rollbackIfNecessary

protected void rollbackIfNecessary(com.rabbitmq.client.Channel channel)
Perform a rollback, if appropriate.

Parameters:
channel - the Rabbit Channel to roll back

rollbackOnExceptionIfNecessary

protected void rollbackOnExceptionIfNecessary(com.rabbitmq.client.Channel channel,
                                              Message message,
                                              Throwable ex)
                                       throws Exception
Perform a rollback, handling rollback exceptions properly.

Parameters:
channel - the Rabbit Channel to roll back
ex - the thrown application exception or error
Throws:
Exception - in case of a rollback error

isChannelLocallyTransacted

protected boolean isChannelLocallyTransacted(com.rabbitmq.client.Channel channel)
Check whether the given Channel is locally transacted, that is, whether its transaction is managed by this listener container's Channel handling and not by an external transaction coordinator.

Note:This method is about finding out whether the Channel's transaction is local or externally coordinated.

Parameters:
channel - the Channel to check
Returns:
whether the given Channel is locally transacted
See Also:
RabbitAccessor.isChannelTransacted()

handleListenerException

protected void handleListenerException(Throwable ex)
Handle the given exception that arose during listener execution.

The default implementation logs the exception at error level, not propagating it to the Rabbit provider - assuming that all handling of acknowledgment and/or transactions is done by this listener container. This can be overridden in subclasses.

Parameters:
ex - the exception to handle

Spring AMQP

Copyright © 2010. All Rights Reserved.