GigaSpaces XAP 9.7.2 API

org.openspaces.events.adapter
Class AbstractReflectionEventListenerAdapter

java.lang.Object
  extended by org.openspaces.events.adapter.AbstractResultEventListenerAdapter
      extended by org.openspaces.events.adapter.AbstractReflectionEventListenerAdapter
All Implemented Interfaces:
EventListenerAdapter, SpaceDataEventListener, InitializingBean
Direct Known Subclasses:
AnnotationEventListenerAdapter, MethodEventListenerAdapter

public abstract class AbstractReflectionEventListenerAdapter
extends AbstractResultEventListenerAdapter
implements InitializingBean, EventListenerAdapter

Base class for reflection driven invocation of event listener methods. Handles cases where a single event listener method is configured, or several method event listeners are configured (all must have the same name). The event listener methods are found and delegated to an object configured using setDelegate(Object).

Subclasses must implement the doGetListenerMethods() in order to list all the possible event listener methods. If more than one event listener method is found, all event listener methods must have the same name.

For best performance a single event listener method should be used. If a single event listener is found (by subclasses), caching of the method can be done in order to perform the reflection execution faster. If more than one event listener method is found - dynamic discovery of the appropriate method is done for each listener invocation.

Event listening methods can have no parameters or one or more parameters mapping to SpaceDataEventListener.onEvent(Object,org.openspaces.core.GigaSpace,org.springframework.transaction.TransactionStatus,Object) parameters order. If the method has a return value it will be handled thanks to AbstractResultEventListenerAdapter.

Having more than one event listening method allows for writing specific listener methods handling different data event types (usually different types within the same inheritance tree). This allows to remove the need for instanceof checks within the listener code. If a single listening method is used, it can still have a specific class type for the event data object thanks to Java reflection, though if the event listener will be invoked with a different type a reflection exception will be thrown.

Author:
kimchy

Field Summary
protected  Log logger
          Logger available to subclasses
 
Constructor Summary
AbstractReflectionEventListenerAdapter()
           
 
Method Summary
 void afterPropertiesSet()
           
protected abstract  Method[] doGetListenerMethods()
          Subclasses should implement this in order to provide a list of all the possible event listener delegate methods.
 Object getActualEventListener()
           
protected  Object getDelegate()
          Returns the event listener delegate.
protected  Object onEventWithResult(Object data, GigaSpace gigaSpace, TransactionStatus txStatus, Object source)
          Delegates the event listener invocation to the appropriate method of the configured setDelegate(Object).
 void setDelegate(Object delegate)
          The event listener delegate that will be searched for event listening methods and will have its method executed.
 void setUseFastReflection(boolean useFastReflection)
          Controls if the listener will be invoked using fast reflection or not.
 
Methods inherited from class org.openspaces.events.adapter.AbstractResultEventListenerAdapter
handleResult, onEvent, setUpdateOrWrite, setUpdateTimeout, setWriteLease
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Logger available to subclasses

Constructor Detail

AbstractReflectionEventListenerAdapter

public AbstractReflectionEventListenerAdapter()
Method Detail

setDelegate

public void setDelegate(Object delegate)
The event listener delegate that will be searched for event listening methods and will have its method executed.


getDelegate

protected Object getDelegate()
Returns the event listener delegate.


setUseFastReflection

public void setUseFastReflection(boolean useFastReflection)
Controls if the listener will be invoked using fast reflection or not. Defaults to true.


afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean

onEventWithResult

protected Object onEventWithResult(Object data,
                                   GigaSpace gigaSpace,
                                   TransactionStatus txStatus,
                                   Object source)
Delegates the event listener invocation to the appropriate method of the configured setDelegate(Object). If a single event listener delegate method is found, uses the cached reflection Method. If more than one event listener delegate method is configured uses reflection to dynamically find the relevant event listener method.

Specified by:
onEventWithResult in class AbstractResultEventListenerAdapter
Parameters:
data - The event data object
gigaSpace - A GigaSpace instance that can be used to perform additional operations against the space
txStatus - An optional transaction status allowing to rollback a transaction programmatically
source - Optional additional data or the actual source event data object (where relevant)
Returns:
A result object that will be written back to the space

getActualEventListener

public Object getActualEventListener()
Specified by:
getActualEventListener in interface EventListenerAdapter

doGetListenerMethods

protected abstract Method[] doGetListenerMethods()
Subclasses should implement this in order to provide a list of all the possible event listener delegate methods.

Returns:
A list of all the event listener delegate methods

GigaSpaces XAP 9.7.2 API

Copyright © GigaSpaces.