GigaSpaces - Open Spaces

org.openspaces.events.polling
Class AbstractPollingEventListenerContainer

java.lang.Object
  extended by org.openspaces.events.AbstractSpaceListeningContainer
      extended by org.openspaces.events.AbstractEventListenerContainer
          extended by org.openspaces.events.AbstractTemplateEventListenerContainer
              extended by org.openspaces.events.polling.AbstractPollingEventListenerContainer
All Implemented Interfaces:
EventListener, BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, ApplicationListener, Lifecycle
Direct Known Subclasses:
SimplePollingEventListenerContainer

public abstract class AbstractPollingEventListenerContainer
extends AbstractTemplateEventListenerContainer

Base class for listener container implementations which are based on polling. Provides support for listener handling based on Space take operations.

This listener container variant is built for repeated polling attempts, each invoking the receiveAndExecute(org.openspaces.events.SpaceDataEventListener) method. The receive timeout for each attempt can be configured through the "receiveTimeout" property.

The container allows to set the template object used for the operations. Note, this can be a Pojo based template, or one of GigaSpace's query classes such as SQLQuery.

A pluggable receive operation handler can be provided by setting setReceiveOperationHandler(org.openspaces.events.polling.receive.ReceiveOperationHandler). The default handler used it SingleTakeReceiveOperationHandler.

Event reception and listener execution can automatically be wrapped in transactions through passing a Spring PlatformTransactionManager into the transactionManager property. This will usually be a LocalJiniTransactionManager.

This base class does not assume any specific mechanism for asynchronous execution of polling invokers. Check out SimplePollingEventListenerContainer for a concrete implementation which is based on Spring's TaskExecutor abstraction, including dynamic scaling of concurrent consumers and automatic self recovery.

The AbstractTemplateEventListenerContainer.setTemplate(Object) parameter is required in order to perform matching on which events to receive. If the AbstractEventListenerContainer.setEventListener(org.openspaces.events.SpaceDataEventListener) implements EventTemplateProvider and the template is directly set, the event listener will be used to get the template. This feature helps when event listeners directly can only work with a certain template and removes the requirement of configuring the template as well.

An advance feature allows for pluggable triggerOperationHandler which mainly makes sense when using transactions. The trigger operations handler allows to perform a trigger receive outside of a transaction scope, and if it returned a value, perform the take within a transaction. A useful implementation of it is ReadTriggerOperationHandler.

Author:
kimchy

Field Summary
static long DEFAULT_RECEIVE_TIMEOUT
          The default receive timeout: 60000 ms = 60 seconds = 1 minute.
 
Fields inherited from class org.openspaces.events.AbstractSpaceListeningContainer
logger
 
Constructor Summary
AbstractPollingEventListenerContainer()
           
 
Method Summary
protected  boolean doReceiveAndExecute(SpaceDataEventListener eventListener, Object template, TransactionStatus status)
           
protected  void eventReceived(Object event)
          Template method that gets called right when a new message has been received, before attempting to process it.
protected  long getReceiveTimeout()
          Returns the timeout used for receive calls, in millisecond.
protected  PlatformTransactionManager getTransactionManager()
          Return the Spring PlatformTransactionManager to use for transactional wrapping of message reception plus listener execution.
 void initialize()
          Initialize this container.
protected  boolean receiveAndExecute(SpaceDataEventListener eventListener)
          Execute the listener for a message received from the given consumer, wrapping the entire operation in an external transaction if demanded.
protected  Object receiveEvent(Object template)
          Receive an event
 void setDisableTransactionValidation(boolean disableTransactionValidation)
          Should transaction validation be enabled or not (verify and fail if transaction manager is provided and the GigaSpace is not transactional).
 void setPassArrayAsIs(boolean passArrayAsIs)
          If set to true will pass an array value returned from a ReceiveOperationHandler directly to the listener without "serializing" it as one array element each time.
 void setReceiveOperationHandler(ReceiveOperationHandler receiveOperationHandler)
          Allows to set a receive operation handler that will perform the actual receive operation.
 void setReceiveTimeout(long receiveTimeout)
          Set the timeout to use for receive calls, in milliseconds.
 void setTransactionIsolationLevel(int transactionIsolationLevel)
          Specify the transaction isolation to use for transactional wrapping.
 void setTransactionIsolationLevelName(String transactionIsolationLevelName)
          Specify the transaction isolation to use for transactional wrapping.
 void setTransactionManager(PlatformTransactionManager transactionManager)
          Specify the Spring PlatformTransactionManager to use for transactional wrapping of event reception plus listener execution.
 void setTransactionName(String transactionName)
          Specify the transaction name to use for transactional wrapping.
 void setTransactionTimeout(int transactionTimeout)
          Specify the transaction timeout to use for transactional wrapping, in seconds.
 void setTriggerOperationHandler(TriggerOperationHandler triggerOperationHandler)
          An advance feature allows for pluggable triggerOperationHandler which mainly makes sense when using transactions.
protected  void validateConfiguration()
          Validate the configuration of this container.
 
Methods inherited from class org.openspaces.events.AbstractTemplateEventListenerContainer
afterPropertiesSet, getReceiveTemplate, getTemplate, setPerformSnapshot, setTemplate
 
Methods inherited from class org.openspaces.events.AbstractEventListenerContainer
doStart, executeListener, getActualEventListener, getApplicationContext, getEventListener, getEventListenerClass, handleListenerException, invokeExceptionListener, invokeListener, setApplicationContext, setEventListener, setEventListenerRef
 
Methods inherited from class org.openspaces.events.AbstractSpaceListeningContainer
destroy, doAfterStart, doBeforeStop, doInitialize, doRescheduleTask, doShutdown, doStop, getBeanName, getGigaSpace, isActive, isRunning, message, onApplicationEvent, rescheduleTaskIfNecessary, setActiveWhenPrimary, setAutoStart, setBeanName, setGigaSpace, setRegisterSpaceModeListener, shutdown, start, stop, waitWhileNotRunning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RECEIVE_TIMEOUT

public static final long DEFAULT_RECEIVE_TIMEOUT
The default receive timeout: 60000 ms = 60 seconds = 1 minute.

See Also:
Constant Field Values
Constructor Detail

AbstractPollingEventListenerContainer

public AbstractPollingEventListenerContainer()
Method Detail

setPassArrayAsIs

public void setPassArrayAsIs(boolean passArrayAsIs)
If set to true will pass an array value returned from a ReceiveOperationHandler directly to the listener without "serializing" it as one array element each time. Defaults to false


setTransactionManager

public void setTransactionManager(PlatformTransactionManager transactionManager)
Specify the Spring PlatformTransactionManager to use for transactional wrapping of event reception plus listener execution.

Default is none, not performing any transactional wrapping.


getTransactionManager

protected final PlatformTransactionManager getTransactionManager()
Return the Spring PlatformTransactionManager to use for transactional wrapping of message reception plus listener execution.


setTransactionName

public void setTransactionName(String transactionName)
Specify the transaction name to use for transactional wrapping. Default is the bean name of this listener container, if any.

See Also:
TransactionDefinition.getName()

setTransactionTimeout

public void setTransactionTimeout(int transactionTimeout)
Specify the transaction timeout to use for transactional wrapping, in seconds. Default is none, using the transaction manager's default timeout.

See Also:
TransactionDefinition.getTimeout(), setReceiveTimeout(long)

setTransactionIsolationLevel

public void setTransactionIsolationLevel(int transactionIsolationLevel)
Specify the transaction isolation to use for transactional wrapping.

See Also:
DefaultTransactionDefinition.setIsolationLevel(int)

setTransactionIsolationLevelName

public void setTransactionIsolationLevelName(String transactionIsolationLevelName)
Specify the transaction isolation to use for transactional wrapping.

See Also:
DefaultTransactionDefinition.setIsolationLevelName(String)

setReceiveTimeout

public void setReceiveTimeout(long receiveTimeout)
Set the timeout to use for receive calls, in milliseconds. The default is 60000 ms, that is, 1 minute.

NOTE: This value needs to be smaller than the transaction timeout used by the transaction manager (in the appropriate unit, of course).

See Also:
GigaSpace.take(Object,long)

getReceiveTimeout

protected long getReceiveTimeout()
Returns the timeout used for receive calls, in millisecond. The default is 60000 ms, that is, 1 minute.


setReceiveOperationHandler

public void setReceiveOperationHandler(ReceiveOperationHandler receiveOperationHandler)
Allows to set a receive operation handler that will perform the actual receive operation. Defaults to SingleTakeReceiveOperationHandler.


setTriggerOperationHandler

public void setTriggerOperationHandler(TriggerOperationHandler triggerOperationHandler)
An advance feature allows for pluggable triggerOperationHandler which mainly makes sense when using transactions. The trigger operations handler allows to perform a trigger receive outside of a transaction scope, and if it returned a value, perform the take within a transaction. A useful implementation of it is ReadTriggerOperationHandler. Defaults to null.


setDisableTransactionValidation

public void setDisableTransactionValidation(boolean disableTransactionValidation)
Should transaction validation be enabled or not (verify and fail if transaction manager is provided and the GigaSpace is not transactional). Default to false.


initialize

public void initialize()
Description copied from class: AbstractSpaceListeningContainer
Initialize this container. If this container is not configured with "activeWhenPrimary" flag set to true will call AbstractSpaceListeningContainer.doStart() (if it is set to true, lifecycle of the container will be controlled by the current space mode). AbstractSpaceListeningContainer.doInitialize() will be called for additional initialization after the possible AbstractSpaceListeningContainer.doStart() call.

Overrides:
initialize in class AbstractTemplateEventListenerContainer
See Also:
AbstractSpaceListeningContainer.onApplicationEvent(org.springframework.context.ApplicationEvent)

validateConfiguration

protected void validateConfiguration()
Description copied from class: AbstractSpaceListeningContainer
Validate the configuration of this container.

Overrides:
validateConfiguration in class AbstractSpaceListeningContainer

receiveAndExecute

protected boolean receiveAndExecute(SpaceDataEventListener eventListener)
                             throws DataAccessException,
                                    TransactionException
Execute the listener for a message received from the given consumer, wrapping the entire operation in an external transaction if demanded.

Throws:
DataAccessException
TransactionException
See Also:
doReceiveAndExecute(org.openspaces.events.SpaceDataEventListener, java.lang.Object, org.springframework.transaction.TransactionStatus)

doReceiveAndExecute

protected boolean doReceiveAndExecute(SpaceDataEventListener eventListener,
                                      Object template,
                                      TransactionStatus status)

receiveEvent

protected Object receiveEvent(Object template)
                       throws DataAccessException
Receive an event

Throws:
DataAccessException

eventReceived

protected void eventReceived(Object event)
Template method that gets called right when a new message has been received, before attempting to process it. Allows subclasses to react to the event of an actual incoming message, for example adapting their consumer count.


GigaSpaces - Open Spaces

Copyright © GigaSpaces.