Package gov.usgs.earthquake.distribution
Class ExecutorListenerNotifier
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.distribution.ExecutorListenerNotifier
-
- All Implemented Interfaces:
ListenerNotifier
,Configurable
- Direct Known Subclasses:
FutureListenerNotifier
public class ExecutorListenerNotifier extends DefaultConfigurable implements ListenerNotifier
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
acceptBeforeQueuing
Make sure listener will accept notification before queueing it for processing.protected java.util.Map<NotificationListener,java.util.concurrent.ExecutorService>
notificationListeners
Notification listeners registered to receive notifications, and an ExecutorService that delivers Notifications to each in a separate thread.protected java.util.Timer
retryTimer
Timer used to retry tasks when they fail and listeners have configured retryDelay.protected int
throttleStartThreshold
When queue size reaches this level, start throttlingprotected int
throttleStopThreshold
When queue size reaches this level, stop throttlingprotected long
throttleWaitInterval
When throttling, wait this many milliseconds between queue size checks.
-
Constructor Summary
Constructors Constructor Description ExecutorListenerNotifier(DefaultNotificationReceiver receiver)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNotificationListener(NotificationListener listener)
Add a new notification listener.java.util.Map<NotificationListener,java.util.concurrent.ExecutorService>
getExecutors()
NOTE: messing with the executors map is not a good idea.java.lang.Integer
getMaxQueueSize()
Check queue status and return length of longest queue.DefaultNotificationReceiver
getReceiver()
java.util.Map<java.lang.String,java.lang.Integer>
getStatus()
int
getThrottleStartThreshold()
int
getThrottleStopThreshold()
long
getThrottleWaitInterval()
void
notifyListeners(NotificationEvent event)
Send a notification to all registered NotificationListeners.void
notifyListeners(NotificationEvent event, java.util.Collection<NotificationListener> listeners)
Calls queueNotification with event and listener for each listenerprotected void
queueNotification(NotificationListener listener, NotificationEvent event)
void
removeNotificationListener(NotificationListener listener)
Remove an existing notification listener.void
setReceiver(DefaultNotificationReceiver receiver)
void
setThrottleStartThreshold(int n)
void
setThrottleStopThreshold(int n)
void
setThrottleWaitInterval(long ms)
void
shutdown()
Stop any processing/background threads.void
startup()
Start any processing/background threads.void
throttleQueues()
If longest queue has more than 50k notifications, wait until longest queue has 25k notifications before returning.void
throttleQueues(java.lang.Integer remaining)
If longest queue has more than 50k notifications, wait until longest queue has 25k notifications before returning.-
Methods inherited from class gov.usgs.util.DefaultConfigurable
configure, getName, setName
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gov.usgs.util.Configurable
configure, getName, setName
-
-
-
-
Field Detail
-
notificationListeners
protected java.util.Map<NotificationListener,java.util.concurrent.ExecutorService> notificationListeners
Notification listeners registered to receive notifications, and an ExecutorService that delivers Notifications to each in a separate thread.
-
acceptBeforeQueuing
protected boolean acceptBeforeQueuing
Make sure listener will accept notification before queueing it for processing.
-
retryTimer
protected java.util.Timer retryTimer
Timer used to retry tasks when they fail and listeners have configured retryDelay.
-
throttleStartThreshold
protected int throttleStartThreshold
When queue size reaches this level, start throttling
-
throttleStopThreshold
protected int throttleStopThreshold
When queue size reaches this level, stop throttling
-
throttleWaitInterval
protected long throttleWaitInterval
When throttling, wait this many milliseconds between queue size checks.
-
-
Constructor Detail
-
ExecutorListenerNotifier
public ExecutorListenerNotifier(DefaultNotificationReceiver receiver)
Constructor- Parameters:
receiver
- DefaultNotificationReceiver
-
-
Method Detail
-
addNotificationListener
public void addNotificationListener(NotificationListener listener) throws java.lang.Exception
Add a new notification listener.- Specified by:
addNotificationListener
in interfaceListenerNotifier
- Parameters:
listener
- the listener to add. When notifications are received, this listener will be notified.- Throws:
java.lang.Exception
- if error occurs
-
removeNotificationListener
public void removeNotificationListener(NotificationListener listener) throws java.lang.Exception
Remove an existing notification listener. Any currently queued notifications are processed before shutting down.- Specified by:
removeNotificationListener
in interfaceListenerNotifier
- Parameters:
listener
- the listener to remove. When notifications are receive, this listener will no longer be notified.- Throws:
java.lang.Exception
- if error occurs
-
notifyListeners
public void notifyListeners(NotificationEvent event) throws java.lang.Exception
Send a notification to all registered NotificationListeners. Creates a NotificationEvent, with a reference to this object and calls each notificationListeners onNotification method in separate threads. This method usually returns before registered NotificationListeners have completed processing a notification.- Specified by:
notifyListeners
in interfaceListenerNotifier
- Parameters:
event
- the notification being sent to listeners.- Throws:
java.lang.Exception
- if error occurs
-
notifyListeners
public void notifyListeners(NotificationEvent event, java.util.Collection<NotificationListener> listeners) throws java.lang.Exception
Calls queueNotification with event and listener for each listener- Parameters:
event
- NotificationEventlisteners
- Collection of NotificationListeners- Throws:
java.lang.Exception
- if error occurs
-
queueNotification
protected void queueNotification(NotificationListener listener, NotificationEvent event)
- Parameters:
listener
- NotificationListenerevent
- NotificationEvent
-
shutdown
public void shutdown() throws java.lang.Exception
Description copied from class:DefaultConfigurable
Stop any processing/background threads.- Specified by:
shutdown
in interfaceConfigurable
- Overrides:
shutdown
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if exceptions occur while starting.
-
startup
public void startup() throws java.lang.Exception
Description copied from class:DefaultConfigurable
Start any processing/background threads.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if exceptions occur while starting.
-
getReceiver
public DefaultNotificationReceiver getReceiver()
- Returns:
- default notification receiver
-
setReceiver
public void setReceiver(DefaultNotificationReceiver receiver)
- Parameters:
receiver
- of the default notification variety
-
getStatus
public java.util.Map<java.lang.String,java.lang.Integer> getStatus()
- Returns:
- map of status
-
getMaxQueueSize
public java.lang.Integer getMaxQueueSize()
Check queue status and return length of longest queue.- Returns:
- length of longest queue, or null if no queue lengths.
-
throttleQueues
public void throttleQueues() throws java.lang.InterruptedException
If longest queue has more than 50k notifications, wait until longest queue has 25k notifications before returning.- Throws:
java.lang.InterruptedException
- if error occurs
-
throttleQueues
public void throttleQueues(java.lang.Integer remaining) throws java.lang.InterruptedException
If longest queue has more than 50k notifications, wait until longest queue has 25k notifications before returning.- Parameters:
remaining
- integer- Throws:
java.lang.InterruptedException
- if error occurs
-
getExecutors
public java.util.Map<NotificationListener,java.util.concurrent.ExecutorService> getExecutors()
NOTE: messing with the executors map is not a good idea.- Returns:
- the map of listeners and their executors.
-
getThrottleStartThreshold
public int getThrottleStartThreshold()
- Returns:
- int throttle start threshold
-
setThrottleStartThreshold
public void setThrottleStartThreshold(int n)
- Parameters:
n
- int throttle start threshold
-
getThrottleStopThreshold
public int getThrottleStopThreshold()
- Returns:
- int throttle stop threshold
-
setThrottleStopThreshold
public void setThrottleStopThreshold(int n)
- Parameters:
n
- int throttle stop threshold
-
getThrottleWaitInterval
public long getThrottleWaitInterval()
- Returns:
- int throttle wait interval
-
setThrottleWaitInterval
public void setThrottleWaitInterval(long ms)
- Parameters:
ms
- long throttle wait interval in ms
-
-