org.springframework.data.gemfire.listener
Class ContinuousQueryListenerContainer

java.lang.Object
  extended by org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer
All Implemented Interfaces:
Aware, BeanNameAware, DisposableBean, InitializingBean, Lifecycle, Phased, SmartLifecycle

public class ContinuousQueryListenerContainer
extends Object
implements BeanNameAware, InitializingBean, DisposableBean, SmartLifecycle

Container providing asynchronous behaviour for GemFire continuous queries.

See Also:
BeanNameAware, DisposableBean, InitializingBean, SmartLifecycle, SimpleAsyncTaskExecutor, TaskExecutor, RegionService, Pool, PoolManager, CqEvent, CqListener, CqQuery, QueryService

Field Summary
static String DEFAULT_THREAD_NAME_PREFIX
           
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
ContinuousQueryListenerContainer()
           
 
Method Summary
 void addListener(ContinuousQueryDefinition definition)
          Adds a Continuous Query (CQ) definition to the (potentially running) container.
 void afterPropertiesSet()
           
protected  TaskExecutor createDefaultTaskExecutor()
          Creates a default TaskExecutor.
 void destroy()
           
protected  void executeListener(ContinuousQueryListener listener, com.gemstone.gemfire.cache.query.CqEvent event)
          Execute the specified listener.
 int getPhase()
          Gets the phase in which this CQ listener container will start in the Spring container.
protected  void handleListenerException(Throwable e)
          Handle the given exception that arose during listener execution.
protected  void invokeErrorHandler(Throwable e)
          Invoke the registered ErrorHandler, if any.
 boolean isActive()
          Determines whether this container is currently active, that is, whether it has been setup (initialized) but not shutdown yet.
 boolean isAutoStartup()
          Determines whether this CQ listener container will automatically start on startup.
 boolean isRunning()
          Determines whether the container has be started and is currently running.
 void setAutoStartup(boolean autoStartup)
          Sets whether the CQ listener container should automatically start on startup.
 void setBeanName(String name)
          Set the name of the bean in the bean factory that created this bean.
 void setCache(com.gemstone.gemfire.cache.RegionService cache)
          Set the underlying RegionService (GemFire Cache) used for registering Queries.
 void setErrorHandler(ErrorHandler errorHandler)
          Set an ErrorHandler to be invoked in case of any uncaught exceptions thrown while processing a CQ event.
 void setPhase(int phase)
          Sets the phase in which this CQ listener container will start in the Spring container.
 void setPoolName(String poolName)
          Set the name of the Pool used for performing the queries by this container.
 void setQueryListeners(Set<ContinuousQueryDefinition> queries)
          Attaches the given query definitions.
 void setQueryService(com.gemstone.gemfire.cache.query.QueryService service)
          Set the GemFire QueryService used by this container to create ContinuousQueries (CQ).
 void setTaskExecutor(Executor taskExecutor)
          Sets the Task Executor used for running the event listeners when messages are received.
 void start()
           
 void stop()
           
 void stop(Runnable callback)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_THREAD_NAME_PREFIX

public static final String DEFAULT_THREAD_NAME_PREFIX

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

ContinuousQueryListenerContainer

public ContinuousQueryListenerContainer()
Method Detail

afterPropertiesSet

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

createDefaultTaskExecutor

protected TaskExecutor createDefaultTaskExecutor()
Creates a default TaskExecutor. Called if no explicit TaskExecutor has been configured.

The default implementation builds a SimpleAsyncTaskExecutor with the specified bean name (or the class name, if no bean name is specified) as thread name prefix.

Returns:
an instance of the TaskExecutor used to process CQ events asynchronously.
See Also:
SimpleAsyncTaskExecutor.SimpleAsyncTaskExecutor(String)

start

public void start()
Specified by:
start in interface Lifecycle

stop

public void stop()
Specified by:
stop in interface Lifecycle

stop

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

destroy

public void destroy()
             throws Exception
Specified by:
destroy in interface DisposableBean
Throws:
Exception

isActive

public final boolean isActive()
Determines whether this container is currently active, that is, whether it has been setup (initialized) but not shutdown yet.

Returns:
a boolean indicating whether the container is active.

isRunning

public boolean isRunning()
Determines whether the container has be started and is currently running.

Specified by:
isRunning in interface Lifecycle
Returns:
a boolean value indicating whether the container has been started and is currently running.

isAutoStartup

public boolean isAutoStartup()
Determines whether this CQ listener container will automatically start on startup.

Specified by:
isAutoStartup in interface SmartLifecycle
Returns:
a boolean value indicating whether this CQ listener container automatically starts.
See Also:
SmartLifecycle.isAutoStartup()

setAutoStartup

public void setAutoStartup(boolean autoStartup)
Sets whether the CQ listener container should automatically start on startup.

Parameters:
autoStartup - a boolean value indicating whether this CQ listener container should automatically start.

setBeanName

public void setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanName in interface BeanNameAware
Parameters:
name - the name of the bean in the factory.

setCache

public void setCache(com.gemstone.gemfire.cache.RegionService cache)
Set the underlying RegionService (GemFire Cache) used for registering Queries.

Parameters:
cache - the RegionService (GemFire Cache) used for registering Queries.
See Also:
RegionService

setErrorHandler

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

Parameters:
errorHandler - the ErrorHandler invoked when uncaught exceptions are thrown while processing the CQ event.
See Also:
ErrorHandler

getPhase

public int getPhase()
Gets the phase in which this CQ listener container will start in the Spring container.

Specified by:
getPhase in interface Phased
Returns:
the phase value of this CQ listener container.
See Also:
Phased.getPhase()

setPhase

public void setPhase(int phase)
Sets the phase in which this CQ listener container will start in the Spring container.

Parameters:
phase - the phase value of this CQ listener container.

setPoolName

public void setPoolName(String poolName)
Set the name of the Pool used for performing the queries by this container.

Parameters:
poolName - the name of the pool to be used by the container

setQueryListeners

public void setQueryListeners(Set<ContinuousQueryDefinition> queries)
Attaches the given query definitions.

Parameters:
queries - set of queries

setQueryService

public void setQueryService(com.gemstone.gemfire.cache.query.QueryService service)
Set the GemFire QueryService used by this container to create ContinuousQueries (CQ).

Parameters:
service - the GemFire QueryService object used by the container to create ContinuousQueries (CQ).
See Also:
QueryService

setTaskExecutor

public void setTaskExecutor(Executor taskExecutor)
Sets the Task Executor used for running the event listeners when messages are received. If no task executor is set, an instance of SimpleAsyncTaskExecutor will be used by default. The task executor can be adjusted depending on the work done by the listeners and the number of messages coming in.

Parameters:
taskExecutor - The Task Executor used to run event listeners when query results messages are received.
See Also:
Executor

addListener

public void addListener(ContinuousQueryDefinition definition)
Adds a Continuous Query (CQ) definition to the (potentially running) container. If the container is running, the listener starts receiving (matching) messages as soon as possible.

Parameters:
definition - Continuous Query (CQ) definition
See Also:
ContinuousQueryDefinition, doAddListener(ContinuousQueryDefinition)

executeListener

protected void executeListener(ContinuousQueryListener listener,
                               com.gemstone.gemfire.cache.query.CqEvent event)
Execute the specified listener.

Parameters:
listener - the ContinuousQueryListener to notify of the CQ event.
event - the CQ event.
See Also:
handleListenerException(Throwable)

handleListenerException

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

The default implementation logs the exception at error level. This can be overridden in subclasses.

Parameters:
e - the exception to handle

invokeErrorHandler

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

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