EmbeddedPDLClient.java

  1. package gov.usgs.earthquake.distribution;

  2. import gov.usgs.earthquake.cube.CubeMessage;
  3. import gov.usgs.earthquake.eids.LegacyConverter;
  4. import gov.usgs.earthquake.eidsutil.EIDSClient;
  5. import gov.usgs.earthquake.product.Product;

  6. import java.io.File;

  7. /**
  8.  * An example of an embedded PDL client.
  9.  *
  10.  * Creates a notification receiver, which store it's information in a specified
  11.  * directory. Listeners can be added to this receiver before its startup()
  12.  * method is called, which starts the distribution process.
  13.  */
  14. public class EmbeddedPDLClient {

  15.     /** name for embedded receiver, appears in log files. */
  16.     public static final String EMBEDDED_NAME = "embeddedPDL";
  17.     /** name for eids tracking file, in data directory. */
  18.     public static final String EMBEDDED_TRACKING_FILE = "receiver_tracking.dat";
  19.     /** name for notification index file, in data directory. */
  20.     public static final String EMBEDDED_INDEX_FILE = "receiver_index.db";
  21.     /** name for receiver storage directory, in data directory. */
  22.     public static final String EMBEDDED_STORAGE_DIRECTORY = "receiver_storage";

  23.     /** The notification receiver. */
  24.     private EIDSNotificationReceiver eidsReceiver;

  25.     /**
  26.      * Construct an embedded PDL client.
  27.      *
  28.      * @param dataDirectory
  29.      *            directory where receiver files are stored.
  30.      * @param serverHost
  31.      *            PDL hub hostname.
  32.      * @param serverPort
  33.      *            PDL hub port.
  34.      * @param alternateServersList
  35.      *            comma separated list of "hostname:port" alternate pdl hubs.
  36.      * @throws Exception if error occurs
  37.      */
  38.     public EmbeddedPDLClient(final File dataDirectory, final String serverHost,
  39.             final Integer serverPort, final String alternateServersList)
  40.             throws Exception {
  41.         EIDSClient client = new EIDSClient();
  42.         client.setServerHost(serverHost);
  43.         client.setServerPort(serverPort);
  44.         client.setAlternateServersList(alternateServersList);
  45.         client.setTrackingFileName(new File(dataDirectory,
  46.                 EMBEDDED_TRACKING_FILE).getCanonicalPath());


  47.         eidsReceiver = new EIDSNotificationReceiver();
  48.         eidsReceiver.setName(EMBEDDED_NAME);
  49.         eidsReceiver.setNotificationIndex(new JDBCNotificationIndex(new File(
  50.                 dataDirectory, EMBEDDED_INDEX_FILE).getCanonicalPath()));
  51.         eidsReceiver.setProductStorage(new FileProductStorage(new File(
  52.                 dataDirectory, EMBEDDED_STORAGE_DIRECTORY)));
  53.         // default these to 15 minutes
  54.         eidsReceiver.setProductStorageMaxAge(900000L);
  55.         eidsReceiver.setReceiverCleanupInterval(900000L);
  56.         eidsReceiver.setClient(client);
  57.         client.addListener(eidsReceiver);
  58.     }

  59.     /**
  60.      * Get the embedded EIDSNotificationReceiver object for further
  61.      * configuration, adding/removing listeners, and starting/stopping
  62.      * distribution.
  63.      *
  64.      * @return the embedded EIDSNotificationReceiver object.
  65.      */
  66.     public EIDSNotificationReceiver getReceiver() {
  67.         return eidsReceiver;
  68.     }

  69.     /**
  70.      * Example main method that uses the EmbeddedPDLClient.
  71.      *
  72.      * @param args
  73.      *            not used.
  74.      * @throws Exception if error occurs
  75.      */
  76.     public static void main(final String[] args) throws Exception {
  77.         // disable product tracker messages
  78.         ProductTracker.setTrackerEnabled(false);

  79.         // client for production hub
  80.         File dataDirectory = new File("embeddedStorage");
  81.         String hostname = "prod01-pdl01.cr.usgs.gov";
  82.         Integer port = 39977;
  83.         String alternateServers = "prod02-pdl01.wr.usgs.gov:39977";

  84.         // create embedded client
  85.         final EmbeddedPDLClient client = new EmbeddedPDLClient(dataDirectory,
  86.                 hostname, port, alternateServers);

  87.         // create a listener that tries to convert messages to cube
  88.         final DefaultNotificationListener listener = new DefaultNotificationListener() {
  89.             // convert a product to a cube message, if possible
  90.             private final LegacyConverter converter = LegacyConverter
  91.                     .cubeConverter();

  92.             @Override
  93.             public void onProduct(final Product product) {
  94.                 System.err.println("Processing product "
  95.                         + product.getId().toString());
  96.                 try {
  97.                     byte[] cubeBytes = converter.convert(product);
  98.                     if (cubeBytes != null) {
  99.                         CubeMessage cubeMessage = CubeMessage.parse(new String(
  100.                                 cubeBytes));
  101.                         // CubeMessage instanceof CubeEvent
  102.                         // or CubeMessage instanceof CubeDelete
  103.                         System.err.println(cubeMessage.toCUBE());
  104.                     }
  105.                 } catch (Exception e) {
  106.                     // ignore
  107.                 }
  108.             }
  109.         };
  110.         // only listen for origin messages
  111.         listener.getIncludeTypes().add("origin");
  112.         // name appears in log messages
  113.         listener.setName("embeddedListener");
  114.         // add listener index for more reliable notification across restarts
  115.         listener.setNotificationIndex(new JDBCNotificationIndex(new File(
  116.                 dataDirectory, "embedded_listener_index.db").getCanonicalPath()));

  117.         // add listener to receiver
  118.         client.getReceiver().addNotificationListener(listener);

  119.         // start
  120.         listener.startup();
  121.         client.getReceiver().startup();

  122.         Runtime.getRuntime().addShutdownHook(new Thread() {
  123.             public void run() {
  124.                 try {
  125.                     client.getReceiver().shutdown();
  126.                     listener.shutdown();
  127.                 } catch (Exception e) {
  128.                     e.printStackTrace();
  129.                 }
  130.             }
  131.         });
  132.     }
  133. }