Class JsonNotificationIndex

All Implemented Interfaces:
NotificationIndex, Configurable, AutoCloseable

public class JsonNotificationIndex extends JDBCConnection implements NotificationIndex
Store Notifications in a database. Only SQLITE or local development should rely on createSchema. Products (data column) have exceeded 64kb, plan accordingly. Mysql Schema Example:
 CREATE TABLE IF NOT EXISTS indexer_receiver_index
 (id INTEGER PRIMARY KEY AUTO_INCREMENT
 , created VARCHAR(255)
 , expires VARCHAR(255)
 , source VARCHAR(255)
 , type VARCHAR(255)
 , code VARCHAR(255)
 , updatetime BIGINT
 , url TEXT
 , data LONGTEXT
 , KEY source_index (source)
 , KEY type_index (type)
 , KEY code_index (code)
 , KEY expires_index (expires)
 ) ENGINE=innodb CHARSET=utf8;
 
  • Field Details

  • Constructor Details

    • JsonNotificationIndex

      public JsonNotificationIndex()
      Construct a JsonNotification using defaults.
    • JsonNotificationIndex

      public JsonNotificationIndex(String driver, String url)
      Construct a JsonNotificationIndex with the default table.
      Parameters:
      driver - Driver to use
      url - URL to use
    • JsonNotificationIndex

      public JsonNotificationIndex(String driver, String url, String table)
      Construct a JsonNotificationIndex with custom driver, url, and table.
      Parameters:
      driver - Driver to use
      url - URL to use
      table - Table to use
  • Method Details

    • getTable

      public String getTable()
      Returns:
      table
    • setTable

      public void setTable(String table)
      Parameters:
      table - Table to set
    • configure

      public void configure(Config config) throws Exception
      Description copied from class: JDBCConnection
      Implement Configurable
      Specified by:
      configure in interface Configurable
      Overrides:
      configure in class JDBCConnection
      Parameters:
      config - Config to set driver and URL in
      Throws:
      Exception - Exception
    • startup

      public void startup() throws Exception
      After normal startup, check whether schema exists and attempt to create.
      Specified by:
      startup in interface Configurable
      Overrides:
      startup in class JDBCConnection
      Throws:
      Exception - if error occurs
    • schemaExists

      public boolean schemaExists() throws Exception
      Check whether schema exists.
      Returns:
      boolean
      Throws:
      Exception - if error occurs
    • createSchema

      public void createSchema() throws Exception
      Attempt to create schema. Only supports sqlite or mysql. When not using sqlite, relying on this method is only recommended for local development.
      Throws:
      Exception - if error occurs
    • addNotification

      public void addNotification(Notification notification) throws Exception
      Add a notification to the index.
      Specified by:
      addNotification in interface NotificationIndex
      Parameters:
      notification - To be added to index
      Throws:
      Exception - if error occurs
    • removeNotification

      public void removeNotification(Notification notification) throws Exception
      Remove notification from index.
      Specified by:
      removeNotification in interface NotificationIndex
      Parameters:
      notification - to be removed from index
      Throws:
      Exception - if error occurs
    • removeNotifications

      public void removeNotifications(List<Notification> notifications) throws Exception
      Remove notifications from index.
      Specified by:
      removeNotifications in interface NotificationIndex
      Parameters:
      notifications - notifications to be removed from index
      Throws:
      Exception - if error occurs
    • findNotifications

      public List<Notification> findNotifications(String source, String type, String code) throws Exception
      Search index for notifications.
      Specified by:
      findNotifications in interface NotificationIndex
      Parameters:
      source - source, or null for all sources.
      type - type, or null for all types.
      code - code, or null for all codes.
      Returns:
      list with matching notifications, empty if not found.
      Throws:
      Exception - if error occurs
    • findNotifications

      public List<Notification> findNotifications(List<String> sources, List<String> types, List<String> codes) throws Exception
      Search index for notifications.
      Specified by:
      findNotifications in interface NotificationIndex
      Parameters:
      sources - sources, or null for all sources.
      types - types, or null for all types.
      codes - codes, or null for all codes.
      Returns:
      list with matching notifications, empty if not found.
      Throws:
      Exception - if error occurs
    • findExpiredNotifications

      public List<Notification> findExpiredNotifications() throws Exception
      Find notifications with expires time before or equal to current time.
      Specified by:
      findExpiredNotifications in interface NotificationIndex
      Returns:
      list with matching notifications, empty if not found.
      Throws:
      Exception - if error occurs
    • findNotifications

      public List<Notification> findNotifications(ProductId id) throws Exception
      Search index for notifications for a specific product.
      Specified by:
      findNotifications in interface NotificationIndex
      Parameters:
      id - the product id to search.
      Returns:
      list with matching notifications, empty if not found.
      Throws:
      Exception - if error occurs
    • getMissingNotifications

      public List<Notification> getMissingNotifications(String otherTable) throws Exception
      This method is used to find notifications present in this index but not present in another JsonNotificationIndex table in the same database. This is used to optimize the queuing process at startup and returns DefaultNotifications. The receiver process will look up the actual notification object during processing.
      Parameters:
      otherTable - name of table in same database.
      Returns:
      list of notifications found in this indexes table, but not found in the other table.
      Throws:
      Exception - if error occurs
    • getNotifications

      protected List<Notification> getNotifications(PreparedStatement ps) throws Exception
      Parse notifications from a statement ready to be executed.
      Parameters:
      ps - PreparedStatement to be parsed
      Returns:
      List of notifications
      Throws:
      Exception - if error occurs
    • parseNotification

      protected Notification parseNotification(String created, String expires, String source, String type, String code, Long updateTime, String url, String data) throws Exception
      Creates and returns a Notification based on the provided data.
      • Return a JSONNotification if created and data are set
      • Return a URLNotification if url is set
      • Otherwise, return a DefaultNotification
      Parameters:
      created - When created
      expires - When notification expires
      source - sources
      type - types
      code - codes
      updateTime - updateTime
      url - URL
      data - data
      Returns:
      Notification, JSONNotification, URLNotification, or DefaultNotification
      Throws:
      Exception - if error occurs