org.springframework.data.gemfire.listener.adapter
Class ContinuousQueryListenerAdapter

java.lang.Object
  extended by org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter
All Implemented Interfaces:
ContinuousQueryListener

public class ContinuousQueryListenerAdapter
extends Object
implements ContinuousQueryListener

Event listener adapter that delegates the handling of messages to target listener methods via reflection, with flexible event type conversion. Allows listener methods to operate on event content types, completely independent from the GemFire API.

Modeled as much as possible after the JMS MessageListenerAdapter in Spring Framework.

By default, the content of incoming GemFire events gets extracted before being passed into the target listener method, to let the target method operate on event content types such as Object or Operation instead of the raw CqEvent.

Find below some examples of method signatures compliant with this adapter class. This first example handles all CqEvent types and gets passed the contents of each event type as an argument.

public interface PojoListener {
    void handleEvent(CqEvent event);
    void handleEvent(Operation baseOp);
    void handleEvent(Object key);
    void handleEvent(Object key, Object newValue);
    void handleEvent(Throwable th);
    void handleEvent(CqEvent event, Operation baseOp, byte[] deltaValue);
    void handleEvent(CqEvent event, Operation baseOp, Operation queryOp, Object key, Object newValue);
 }

See Also:
MessageListenerAdapter

Field Summary
protected  org.apache.commons.logging.Log logger
          Logger available to subclasses
static String ORIGINAL_DEFAULT_LISTENER_METHOD
          Out-of-the-box value for the default listener method: "handleEvent".
 
Constructor Summary
ContinuousQueryListenerAdapter()
          Create a new ContinuousQueryListenerAdapter with default settings.
ContinuousQueryListenerAdapter(Object delegate)
          Create a new ContinuousQueryListenerAdapter for the given delegate.
 
Method Summary
protected  String getDefaultListenerMethod()
          Return the name of the default listener method to delegate to.
 Object getDelegate()
          Returns the target object to delegate event listening to.
protected  String getListenerMethodName(com.gemstone.gemfire.cache.query.CqEvent event)
          Determine the name of the listener method that is supposed to handle the given event.
protected  void handleListenerException(Throwable ex)
          Handle the given exception that arose during listener execution.
protected  void invokeListenerMethod(com.gemstone.gemfire.cache.query.CqEvent event, String methodName)
          Invoke the specified listener method.
 void onEvent(com.gemstone.gemfire.cache.query.CqEvent event)
          Standard ContinuousQueryListener entry point.
 void setDefaultListenerMethod(String defaultListenerMethod)
          Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined.
 void setDelegate(Object delegate)
          Set a target object to delegate events listening to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ORIGINAL_DEFAULT_LISTENER_METHOD

public static final String ORIGINAL_DEFAULT_LISTENER_METHOD
Out-of-the-box value for the default listener method: "handleEvent".

See Also:
Constant Field Values

logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses

Constructor Detail

ContinuousQueryListenerAdapter

public ContinuousQueryListenerAdapter()
Create a new ContinuousQueryListenerAdapter with default settings.


ContinuousQueryListenerAdapter

public ContinuousQueryListenerAdapter(Object delegate)
Create a new ContinuousQueryListenerAdapter for the given delegate.

Parameters:
delegate - the delegate object
Method Detail

setDelegate

public void setDelegate(Object delegate)
Set a target object to delegate events listening to. Specified listener methods have to be present on this target object.

If no explicit delegate object has been specified, listener methods are expected to present on this adapter instance, that is, on a custom subclass of this adapter, defining listener methods.

Parameters:
delegate - delegate object

getDelegate

public Object getDelegate()
Returns the target object to delegate event listening to.

Returns:
event listening delegation

setDefaultListenerMethod

public void setDefaultListenerMethod(String defaultListenerMethod)
Specify the name of the default listener method to delegate to, for the case where no specific listener method has been determined. Out-of-the-box value is "handleEvent.

See Also:
getListenerMethodName(com.gemstone.gemfire.cache.query.CqEvent)

getDefaultListenerMethod

protected String getDefaultListenerMethod()
Return the name of the default listener method to delegate to.


onEvent

public void onEvent(com.gemstone.gemfire.cache.query.CqEvent event)
Standard ContinuousQueryListener entry point.

Delegates the event to the target listener method, with appropriate conversion of the event argument. In case of an exception, the handleListenerException(Throwable) method will be invoked.

Specified by:
onEvent in interface ContinuousQueryListener
Parameters:
event - the incoming GemFire event
See Also:
handleListenerException(java.lang.Throwable)

handleListenerException

protected void handleListenerException(Throwable ex)
Handle the given exception that arose during listener execution. The default implementation logs the exception at error level.

Parameters:
ex - the exception to handle

getListenerMethodName

protected String getListenerMethodName(com.gemstone.gemfire.cache.query.CqEvent event)
Determine the name of the listener method that is supposed to handle the given event.

The default implementation simply returns the configured default listener method, if any.

Parameters:
event - the GemFire event
Returns:
the name of the listener method (never null)
See Also:
setDefaultListenerMethod(java.lang.String)

invokeListenerMethod

protected void invokeListenerMethod(com.gemstone.gemfire.cache.query.CqEvent event,
                                    String methodName)
Invoke the specified listener method.

Parameters:
event - the event arguments to be passed in
methodName - the method to invoke
See Also:
getListenerMethodName(com.gemstone.gemfire.cache.query.CqEvent)