NotificationListenerCallable.java

  1. package gov.usgs.earthquake.distribution;

  2. import java.util.concurrent.Callable;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;

  5. /**
  6.  * A Callable object for deferred listener notification.
  7.  */
  8. public class NotificationListenerCallable implements Callable<Void> {

  9.     private static final Logger LOGGER = Logger
  10.             .getLogger(NotificationListenerCallable.class.getName());

  11.     private final NotificationListener listener;
  12.     private final NotificationEvent event;

  13.     /**
  14.      * Create an ExecutorListenerNotifierCallable.
  15.      *
  16.      * @param listener
  17.      *            the listener to notify
  18.      * @param event
  19.      *            the notification to send
  20.      */
  21.     public NotificationListenerCallable(
  22.             final NotificationListener listener, final NotificationEvent event) {
  23.         this.listener = listener;
  24.         this.event = event;
  25.     }

  26.     public Void call() throws Exception {
  27.         try {
  28.             listener.onNotification(event);
  29.             return null;
  30.         } catch (Exception e) {
  31.             LOGGER.log(Level.WARNING, "["
  32.                     + event.getNotificationReceiver().getName()
  33.                     + "] listener (" + listener.getName()
  34.                     + ") threw exception, for product id = "
  35.                     + event.getNotification().getProductId(), e);

  36.             // track exception
  37.             Notification notification = event.getNotification();
  38.             new ProductTracker(notification.getTrackerURL()).exception(listener
  39.                     .getClass().getCanonicalName(),
  40.                     notification.getProductId(), e);

  41.             // but rethrow for outside handling
  42.             throw e;
  43.         }
  44.     }

  45. }