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 Details

    • NOTIFICATION_INDEX_PROPERTY

      public static final String NOTIFICATION_INDEX_PROPERTY
      Property referencing a notification index config section.
      See Also:
    • INDEX_FILE_PROPERTY

      public static final String INDEX_FILE_PROPERTY
      Shortcut to create a SQLite JDBCNotificationIndex.
      See Also:
    • PRODUCT_STORAGE_PROPERTY

      public static final String PRODUCT_STORAGE_PROPERTY
      Property referencing a product storage config section.
      See Also:
    • STORAGE_DIRECTORY_PROPERTY

      public static final String STORAGE_DIRECTORY_PROPERTY
      Shortcut to create a FileProductStorage.
      See Also:
    • PRODUCT_STORAGE_MAX_AGE_PROPERTY

      public static final String PRODUCT_STORAGE_MAX_AGE_PROPERTY
      Property referencing how long to store products in milliseconds.
      See Also:
    • DEFAULT_PRODUCT_STORAGE_MAX_AGE

      public static final String DEFAULT_PRODUCT_STORAGE_MAX_AGE
      Default max age to store products, 3600000 milliseconds = 1 hour.
      See Also:
    • RECEIVER_CLEANUP_PROPERTY

      public static final String RECEIVER_CLEANUP_PROPERTY
      Property referencing how long to wait until checking for expired notifications/products.
      See Also:
    • DEFAULT_RECEIVER_CLEANUP

      public static final String DEFAULT_RECEIVER_CLEANUP
      Default time between checking for expired notifications/products, 900000 milliseconds = 15 minutes.
      See Also:
    • CONNECT_TIMEOUT_PROPERTY

      public static final String CONNECT_TIMEOUT_PROPERTY
      Property for connection Timeout
      See Also:
    • DEFAULT_CONNECT_TIMEOUT

      public static final String DEFAULT_CONNECT_TIMEOUT
      Default connection timeout. 15 seconds
      See Also:
    • READ_TIMEOUT_PROPERTY

      public static final String READ_TIMEOUT_PROPERTY
      Property for read timeout
      See Also:
    • DEFAULT_READ_TIMEOUT

      public static final String DEFAULT_READ_TIMEOUT
      default read timeout. 15 seconds
      See Also:
    • LISTENER_NOTIFIER_PROPERTY

      public static final String LISTENER_NOTIFIER_PROPERTY
      Property for listener notifier
      See Also:
    • EXECUTOR_LISTENER_NOTIFIER

      public static final String EXECUTOR_LISTENER_NOTIFIER
      Property for listener notifier to set to executor
      See Also:
    • FUTURE_LISTENER_NOTIFIER

      public static final String FUTURE_LISTENER_NOTIFIER
      Property to listener notifier to set to future
      See Also:
    • ROUNDROBIN_LISTENER_NOTIFIER

      public static final String ROUNDROBIN_LISTENER_NOTIFIER
      Property to listener notifier to set to roundrobin
      See Also:
  • Constructor Details

    • DefaultNotificationReceiver

      public DefaultNotificationReceiver()
      Creates new ExecutorListenerNotifier to var notifier
  • Method Details

    • addNotificationListener

      public void addNotificationListener(NotificationListener listener) throws Exception
      Add a new notification listener.
      Specified by:
      addNotificationListener in interface NotificationReceiver
      Parameters:
      listener - the listener to add. When notifications are received, this listener will be notified.
      Throws:
      Exception - exception
    • removeNotificationListener

      public void removeNotificationListener(NotificationListener listener) throws Exception
      Remove an existing notification listener. Any currently queued notifications are processed before shutting down.
      Specified by:
      removeNotificationListener in interface NotificationReceiver
      Parameters:
      listener - the listener to remove. When notifications are receive, this listener will no longer be notified.
      Throws:
      Exception - exception
    • receiveNotification

      public void receiveNotification(Notification notification) throws Exception
      Store a notification and notify listeners. Updates the notification index before notifying listeners of the newly available product.
      Specified by:
      receiveNotification in interface NotificationReceiver
      Parameters:
      notification - the notification being received.
      Throws:
      Exception - if the notificationIndex throws an Exception.
    • notifyListeners

      protected void notifyListeners(Notification notification) throws 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:
      Exception - exception
    • getListenerQueueStatus

      public String getListenerQueueStatus()
      Returns:
      "Using notifier"
    • removeExpiredNotifications

      public void removeExpiredNotifications() throws 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 interface NotificationReceiver
      Throws:
      Exception - if NotificationIndexCleanup throws an Exception.
    • onExpiredNotification

      public void onExpiredNotification(Notification notification) throws Exception
      Callback from the NotificationIndexCleanup thread. Checks if Notification refers to a product in storage, which should also be removed.
      Specified by:
      onExpiredNotification in interface NotificationIndexCleanup.Listener
      Parameters:
      notification - expired notification about to be removed.
      Throws:
      Exception - if error occurs sttempting to removing product
      See Also:
    • retrieveProduct

      public Product retrieveProduct(ProductId id) throws 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 interface NotificationReceiver
      Parameters:
      id - the product to retrieve
      Returns:
      the retrieved product, or null if not available.
      Throws:
      Exception - exception
    • storeProductSource

      protected Notification storeProductSource(ProductSource source) throws Exception
      Calls the current ProductStorage.storeProductSource method.
      Parameters:
      source - The ProductSource to store.
      Returns:
      The ProductId of the product referenced by the given ProductSource.
      Throws:
      Exception
      See Also:
    • sendNotifications

      public void sendNotifications(NotificationListener listener, List<String> sources, List<String> types, List<String> codes) throws Exception
      Send matching notifications to listener. Searches the NotificationIndex for matching notifications, and sends a NotificationEvent for each notification found.
      Specified by:
      sendNotifications in interface NotificationReceiver
      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:
      Exception - if the notification index or notification listener throw an exception.
    • configure

      public void configure(Config config) throws Exception
      Description copied from class: DefaultConfigurable
      Process configuration settings. Called before startup().
      Specified by:
      configure in interface Configurable
      Overrides:
      configure in class DefaultConfigurable
      Parameters:
      config - the Config object with settings.
      Throws:
      Exception - if configuration exceptions occur.
    • shutdown

      public void shutdown() throws Exception
      Description copied from class: DefaultConfigurable
      Stop any processing/background threads.
      Specified by:
      shutdown in interface Configurable
      Overrides:
      shutdown in class DefaultConfigurable
      Throws:
      Exception - if exceptions occur while starting.
    • startup

      public void startup() throws Exception
      Description copied from class: DefaultConfigurable
      Start any processing/background threads.
      Specified by:
      startup in interface Configurable
      Overrides:
      startup in class DefaultConfigurable
      Throws:
      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 Long getProductStorageMaxAge()
      Returns:
      the productStorageMaxAge
    • setProductStorageMaxAge

      public void setProductStorageMaxAge(Long productStorageMaxAge)
      Parameters:
      productStorageMaxAge - the productStorageMaxAge to set
    • getQueueStatus

      public Map<String,Integer> getQueueStatus()
      Returns:
      the QueueStatus or null if ExecutorListenerNotifier doesn't exist
    • throttleQueues

      public void throttleQueues() throws InterruptedException
      Throttle notifier queues
      Throws:
      InterruptedException - InterruptedException
    • getReceiverCleanupInterval

      public Long getReceiverCleanupInterval()
      Returns:
      receiverCleanupInterval
    • setReceiverCleanupInterval

      public void setReceiverCleanupInterval(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