Package gov.usgs.earthquake.distribution
Class DefaultNotificationReceiver
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.distribution.DefaultNotificationReceiver
-
- All Implemented Interfaces:
NotificationIndexCleanup.Listener
,NotificationReceiver
,Configurable
- Direct Known Subclasses:
AwsProductReceiver
,EIDSNotificationReceiver
,NATSStreamingNotificationReceiver
,SocketProductReceiver
,WebSocketNotificationReceiver
public class DefaultNotificationReceiver extends DefaultConfigurable implements NotificationReceiver, NotificationIndexCleanup.Listener
The core of product distribution. A DefaultNotificationReceiver receives notifications and notifies listeners of received notifications. NotificationListeners use the NotificationReceiver to retrieve products referenced by notifications. The NotificationReceiver uses a NotificationIndex to track received notifications, and a ProductStorage to store retrieved products. The DefaultNotificationReceiver implements the Configurable interface and uses the following configuration parameters: Each listener has a separate queue of notifications. Each listener is allocated one thread to process notifications from this queue.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CONNECT_TIMEOUT_PROPERTY
Property for connection Timeoutstatic java.lang.String
DEFAULT_CONNECT_TIMEOUT
Default connection timeout.static java.lang.String
DEFAULT_PRODUCT_STORAGE_MAX_AGE
Default max age to store products, 3600000 milliseconds = 1 hour.static java.lang.String
DEFAULT_READ_TIMEOUT
default read timeout.static java.lang.String
DEFAULT_RECEIVER_CLEANUP
Default time between checking for expired notifications/products, 900000 milliseconds = 15 minutes.static java.lang.String
EXECUTOR_LISTENER_NOTIFIER
Property for listener notifier to set to executorstatic java.lang.String
FUTURE_LISTENER_NOTIFIER
Property to listener notifier to set to futurestatic java.lang.String
INDEX_FILE_PROPERTY
Shortcut to create a SQLite JDBCNotificationIndex.static java.lang.String
LISTENER_NOTIFIER_PROPERTY
Property for listener notifierstatic java.lang.String
NOTIFICATION_INDEX_PROPERTY
Property referencing a notification index config section.static java.lang.String
PRODUCT_STORAGE_MAX_AGE_PROPERTY
Property referencing how long to store products in milliseconds.static java.lang.String
PRODUCT_STORAGE_PROPERTY
Property referencing a product storage config section.static java.lang.String
READ_TIMEOUT_PROPERTY
Property for read timeoutstatic java.lang.String
RECEIVER_CLEANUP_PROPERTY
Property referencing how long to wait until checking for expired notifications/products.static java.lang.String
ROUNDROBIN_LISTENER_NOTIFIER
Property to listener notifier to set to roundrobinstatic java.lang.String
STORAGE_DIRECTORY_PROPERTY
Shortcut to create a FileProductStorage.
-
Constructor Summary
Constructors Constructor Description DefaultNotificationReceiver()
Creates new ExecutorListenerNotifier to var notifier
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNotificationListener(NotificationListener listener)
Add a new notification listener.void
configure(Config config)
Process configuration settings.int
getConnectTimeout()
java.lang.String
getListenerQueueStatus()
NotificationIndex
getNotificationIndex()
ListenerNotifier
getNotifier()
ProductStorage
getProductStorage()
java.lang.Long
getProductStorageMaxAge()
java.util.Map<java.lang.String,java.lang.Integer>
getQueueStatus()
int
getReadTimeout()
java.lang.Long
getReceiverCleanupInterval()
protected void
notifyListeners(Notification notification)
Send a notification to all registered NotificationListeners.void
onExpiredNotification(Notification notification)
Callback from the NotificationIndexCleanup thread.void
receiveNotification(Notification notification)
Store a notification and notify listeners.void
removeExpiredNotifications()
Search the notification index for expired notifications, removing any that are found.void
removeNotificationListener(NotificationListener listener)
Remove an existing notification listener.Product
retrieveProduct(ProductId id)
Retrieve a product by id.void
sendNotifications(NotificationListener listener, java.util.List<java.lang.String> sources, java.util.List<java.lang.String> types, java.util.List<java.lang.String> codes)
Send matching notifications to listener.void
setConnectTimeout(int connectTimeout)
void
setNotificationIndex(NotificationIndex notificationIndex)
void
setNotifier(ListenerNotifier notifier)
void
setProductStorage(ProductStorage productStorage)
void
setProductStorageMaxAge(java.lang.Long productStorageMaxAge)
void
setReadTimeout(int readTimeout)
void
setReceiverCleanupInterval(java.lang.Long receiverCleanupInterval)
void
shutdown()
Stop any processing/background threads.void
startup()
Start any processing/background threads.protected Notification
storeProductSource(ProductSource source)
Calls the currentProductStorage.storeProductSource
method.void
throttleQueues()
Throttle notifier queues-
Methods inherited from class gov.usgs.util.DefaultConfigurable
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
getName, setName
-
-
-
-
Field Detail
-
NOTIFICATION_INDEX_PROPERTY
public static final java.lang.String NOTIFICATION_INDEX_PROPERTY
Property referencing a notification index config section.- See Also:
- Constant Field Values
-
INDEX_FILE_PROPERTY
public static final java.lang.String INDEX_FILE_PROPERTY
Shortcut to create a SQLite JDBCNotificationIndex.- See Also:
- Constant Field Values
-
PRODUCT_STORAGE_PROPERTY
public static final java.lang.String PRODUCT_STORAGE_PROPERTY
Property referencing a product storage config section.- See Also:
- Constant Field Values
-
STORAGE_DIRECTORY_PROPERTY
public static final java.lang.String STORAGE_DIRECTORY_PROPERTY
Shortcut to create a FileProductStorage.- See Also:
- Constant Field Values
-
PRODUCT_STORAGE_MAX_AGE_PROPERTY
public static final java.lang.String PRODUCT_STORAGE_MAX_AGE_PROPERTY
Property referencing how long to store products in milliseconds.- See Also:
- Constant Field Values
-
DEFAULT_PRODUCT_STORAGE_MAX_AGE
public static final java.lang.String DEFAULT_PRODUCT_STORAGE_MAX_AGE
Default max age to store products, 3600000 milliseconds = 1 hour.- See Also:
- Constant Field Values
-
RECEIVER_CLEANUP_PROPERTY
public static final java.lang.String RECEIVER_CLEANUP_PROPERTY
Property referencing how long to wait until checking for expired notifications/products.- See Also:
- Constant Field Values
-
DEFAULT_RECEIVER_CLEANUP
public static final java.lang.String DEFAULT_RECEIVER_CLEANUP
Default time between checking for expired notifications/products, 900000 milliseconds = 15 minutes.- See Also:
- Constant Field Values
-
CONNECT_TIMEOUT_PROPERTY
public static final java.lang.String CONNECT_TIMEOUT_PROPERTY
Property for connection Timeout- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT
public static final java.lang.String DEFAULT_CONNECT_TIMEOUT
Default connection timeout. 15 seconds- See Also:
- Constant Field Values
-
READ_TIMEOUT_PROPERTY
public static final java.lang.String READ_TIMEOUT_PROPERTY
Property for read timeout- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT
public static final java.lang.String DEFAULT_READ_TIMEOUT
default read timeout. 15 seconds- See Also:
- Constant Field Values
-
LISTENER_NOTIFIER_PROPERTY
public static final java.lang.String LISTENER_NOTIFIER_PROPERTY
Property for listener notifier- See Also:
- Constant Field Values
-
EXECUTOR_LISTENER_NOTIFIER
public static final java.lang.String EXECUTOR_LISTENER_NOTIFIER
Property for listener notifier to set to executor- See Also:
- Constant Field Values
-
FUTURE_LISTENER_NOTIFIER
public static final java.lang.String FUTURE_LISTENER_NOTIFIER
Property to listener notifier to set to future- See Also:
- Constant Field Values
-
ROUNDROBIN_LISTENER_NOTIFIER
public static final java.lang.String ROUNDROBIN_LISTENER_NOTIFIER
Property to listener notifier to set to roundrobin- See Also:
- Constant Field Values
-
-
Method Detail
-
addNotificationListener
public void addNotificationListener(NotificationListener listener) throws java.lang.Exception
Add a new notification listener.- Specified by:
addNotificationListener
in interfaceNotificationReceiver
- Parameters:
listener
- the listener to add. When notifications are received, this listener will be notified.- Throws:
java.lang.Exception
- exception
-
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 interfaceNotificationReceiver
- Parameters:
listener
- the listener to remove. When notifications are receive, this listener will no longer be notified.- Throws:
java.lang.Exception
- exception
-
receiveNotification
public void receiveNotification(Notification notification) throws java.lang.Exception
Store a notification and notify listeners. Updates the notification index before notifying listeners of the newly available product.- Specified by:
receiveNotification
in interfaceNotificationReceiver
- Parameters:
notification
- the notification being received.- Throws:
java.lang.Exception
- if the notificationIndex throws an Exception.
-
notifyListeners
protected void notifyListeners(Notification notification) 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.- Parameters:
notification
- the notification being sent to listeners.- Throws:
java.lang.Exception
- exception
-
getListenerQueueStatus
public java.lang.String getListenerQueueStatus()
- Returns:
- "Using notifier"
-
removeExpiredNotifications
public void removeExpiredNotifications() throws java.lang.Exception
Search the notification index for expired notifications, removing any that are found. When a notification in the index is not a URLNotification, it represents a product in storage that will also be removed.- Specified by:
removeExpiredNotifications
in interfaceNotificationReceiver
- Throws:
java.lang.Exception
- if NotificationIndexCleanup throws an Exception.
-
onExpiredNotification
public void onExpiredNotification(Notification notification) throws java.lang.Exception
Callback from the NotificationIndexCleanup thread. Checks if Notification refers to a product in storage, which should also be removed.- Specified by:
onExpiredNotification
in interfaceNotificationIndexCleanup.Listener
- Parameters:
notification
- expired notification about to be removed.- Throws:
java.lang.Exception
-
retrieveProduct
public Product retrieveProduct(ProductId id) throws java.lang.Exception
Retrieve a product by id. If this product is already in storage, load and return the product. Otherwise, search notifications for this product, and download the product into storage.- Specified by:
retrieveProduct
in interfaceNotificationReceiver
- Parameters:
id
- the product to retrieve- Returns:
- the retrieved product, or null if not available.
- Throws:
java.lang.Exception
- exception
-
storeProductSource
protected Notification storeProductSource(ProductSource source) throws java.lang.Exception
Calls the currentProductStorage.storeProductSource
method.- Parameters:
source
- TheProductSource
to store.- Returns:
- The
ProductId
of the product referenced by the givenProductSource
. - Throws:
java.lang.Exception
- exception- See Also:
ProductStorage
-
sendNotifications
public void sendNotifications(NotificationListener listener, java.util.List<java.lang.String> sources, java.util.List<java.lang.String> types, java.util.List<java.lang.String> codes) throws java.lang.Exception
Send matching notifications to listener. Searches the NotificationIndex for matching notifications, and sends a NotificationEvent for each notification found.- Specified by:
sendNotifications
in interfaceNotificationReceiver
- Parameters:
listener
- the listener to receive a NotificationEvent for each found notification.sources
- sources to include, or null for all.types
- types to include, or null for all.codes
- codes to include, or null for all.- Throws:
java.lang.Exception
- if the notification index or notification listener throw an exception.
-
configure
public void configure(Config config) throws java.lang.Exception
Description copied from class:DefaultConfigurable
Process configuration settings. Called before startup().- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classDefaultConfigurable
- Parameters:
config
- the Config object with settings.- Throws:
java.lang.Exception
- if configuration exceptions occur.
-
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.
-
getNotificationIndex
public NotificationIndex getNotificationIndex()
- Returns:
- the notificationIndex
-
setNotificationIndex
public void setNotificationIndex(NotificationIndex notificationIndex)
- Parameters:
notificationIndex
- the notificationIndex to set
-
getProductStorage
public ProductStorage getProductStorage()
- Returns:
- the productStorage
-
setProductStorage
public void setProductStorage(ProductStorage productStorage)
- Parameters:
productStorage
- the productStorage to set
-
getProductStorageMaxAge
public java.lang.Long getProductStorageMaxAge()
- Returns:
- the productStorageMaxAge
-
setProductStorageMaxAge
public void setProductStorageMaxAge(java.lang.Long productStorageMaxAge)
- Parameters:
productStorageMaxAge
- the productStorageMaxAge to set
-
getQueueStatus
public java.util.Map<java.lang.String,java.lang.Integer> getQueueStatus()
- Returns:
- the QueueStatus or null if ExecutorListenerNotifier doesn't exist
-
throttleQueues
public void throttleQueues() throws java.lang.InterruptedException
Throttle notifier queues- Throws:
java.lang.InterruptedException
- InterruptedException
-
getReceiverCleanupInterval
public java.lang.Long getReceiverCleanupInterval()
- Returns:
- receiverCleanupInterval
-
setReceiverCleanupInterval
public void setReceiverCleanupInterval(java.lang.Long receiverCleanupInterval)
- Parameters:
receiverCleanupInterval
- the receiverCleanupInterval to set
-
getConnectTimeout
public int getConnectTimeout()
- Returns:
- connectionTimeout
-
setConnectTimeout
public void setConnectTimeout(int connectTimeout)
- Parameters:
connectTimeout
- int connectionTimeout to set
-
getNotifier
public ListenerNotifier getNotifier()
- Returns:
- ListenerNotifier
-
setNotifier
public void setNotifier(ListenerNotifier notifier)
- Parameters:
notifier
- ListenerNotifier to set
-
getReadTimeout
public int getReadTimeout()
- Returns:
- readTimeout
-
setReadTimeout
public void setReadTimeout(int readTimeout)
- Parameters:
readTimeout
- int readTimeout to set
-
-