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.StringCONNECT_TIMEOUT_PROPERTYProperty for connection Timeoutstatic java.lang.StringDEFAULT_CONNECT_TIMEOUTDefault connection timeout.static java.lang.StringDEFAULT_PRODUCT_STORAGE_MAX_AGEDefault max age to store products, 3600000 milliseconds = 1 hour.static java.lang.StringDEFAULT_READ_TIMEOUTdefault read timeout.static java.lang.StringDEFAULT_RECEIVER_CLEANUPDefault time between checking for expired notifications/products, 900000 milliseconds = 15 minutes.static java.lang.StringEXECUTOR_LISTENER_NOTIFIERProperty for listener notifier to set to executorstatic java.lang.StringFUTURE_LISTENER_NOTIFIERProperty to listener notifier to set to futurestatic java.lang.StringINDEX_FILE_PROPERTYShortcut to create a SQLite JDBCNotificationIndex.static java.lang.StringLISTENER_NOTIFIER_PROPERTYProperty for listener notifierstatic java.lang.StringNOTIFICATION_INDEX_PROPERTYProperty referencing a notification index config section.static java.lang.StringPRODUCT_STORAGE_MAX_AGE_PROPERTYProperty referencing how long to store products in milliseconds.static java.lang.StringPRODUCT_STORAGE_PROPERTYProperty referencing a product storage config section.static java.lang.StringREAD_TIMEOUT_PROPERTYProperty for read timeoutstatic java.lang.StringRECEIVER_CLEANUP_PROPERTYProperty referencing how long to wait until checking for expired notifications/products.static java.lang.StringROUNDROBIN_LISTENER_NOTIFIERProperty to listener notifier to set to roundrobinstatic java.lang.StringSTORAGE_DIRECTORY_PROPERTYShortcut 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 voidaddNotificationListener(NotificationListener listener)Add a new notification listener.voidconfigure(Config config)Process configuration settings.intgetConnectTimeout()java.lang.StringgetListenerQueueStatus()NotificationIndexgetNotificationIndex()ListenerNotifiergetNotifier()ProductStoragegetProductStorage()java.lang.LonggetProductStorageMaxAge()java.util.Map<java.lang.String,java.lang.Integer>getQueueStatus()intgetReadTimeout()java.lang.LonggetReceiverCleanupInterval()protected voidnotifyListeners(Notification notification)Send a notification to all registered NotificationListeners.voidonExpiredNotification(Notification notification)Callback from the NotificationIndexCleanup thread.voidreceiveNotification(Notification notification)Store a notification and notify listeners.voidremoveExpiredNotifications()Search the notification index for expired notifications, removing any that are found.voidremoveNotificationListener(NotificationListener listener)Remove an existing notification listener.ProductretrieveProduct(ProductId id)Retrieve a product by id.voidsendNotifications(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.voidsetConnectTimeout(int connectTimeout)voidsetNotificationIndex(NotificationIndex notificationIndex)voidsetNotifier(ListenerNotifier notifier)voidsetProductStorage(ProductStorage productStorage)voidsetProductStorageMaxAge(java.lang.Long productStorageMaxAge)voidsetReadTimeout(int readTimeout)voidsetReceiverCleanupInterval(java.lang.Long receiverCleanupInterval)voidshutdown()Stop any processing/background threads.voidstartup()Start any processing/background threads.protected NotificationstoreProductSource(ProductSource source)Calls the currentProductStorage.storeProductSourcemethod.voidthrottleQueues()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:
addNotificationListenerin 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:
removeNotificationListenerin 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:
receiveNotificationin 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.ExceptionSearch 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:
removeExpiredNotificationsin 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:
onExpiredNotificationin 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:
retrieveProductin 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.storeProductSourcemethod.- Parameters:
source- TheProductSourceto store.- Returns:
- The
ProductIdof 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:
sendNotificationsin 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:DefaultConfigurableProcess configuration settings. Called before startup().- Specified by:
configurein interfaceConfigurable- Overrides:
configurein classDefaultConfigurable- Parameters:
config- the Config object with settings.- Throws:
java.lang.Exception- if configuration exceptions occur.
-
shutdown
public void shutdown() throws java.lang.ExceptionDescription copied from class:DefaultConfigurableStop any processing/background threads.- Specified by:
shutdownin interfaceConfigurable- Overrides:
shutdownin classDefaultConfigurable- Throws:
java.lang.Exception- if exceptions occur while starting.
-
startup
public void startup() throws java.lang.ExceptionDescription copied from class:DefaultConfigurableStart any processing/background threads.- Specified by:
startupin interfaceConfigurable- Overrides:
startupin 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.InterruptedExceptionThrottle 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
-
-