Package gov.usgs.earthquake.aws
Class JsonNotificationIndex
java.lang.Object
gov.usgs.util.DefaultConfigurable
gov.usgs.earthquake.util.JDBCConnection
gov.usgs.earthquake.aws.JsonNotificationIndex
- All Implemented Interfaces:
NotificationIndex
,Configurable
,AutoCloseable
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Variable for the default driverstatic final String
Variable for the default tablestatic final String
Variable for the default URLFields inherited from class gov.usgs.earthquake.util.JDBCConnection
DATABASE_SECRET_ARN_PROPERTY
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a JsonNotification using defaults.JsonNotificationIndex
(String driver, String url) Construct a JsonNotificationIndex with the default table.JsonNotificationIndex
(String driver, String url, String table) Construct a JsonNotificationIndex with custom driver, url, and table. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addNotification
(Notification notification) Add a notification to the index.void
Implement Configurablevoid
Attempt to create schema.Find notifications with expires time before or equal to current time.Search index for notifications for a specific product.findNotifications
(String source, String type, String code) Search index for notifications.Search index for notifications.getMissingNotifications
(String otherTable) This method is used to find notifications present in this index but not present in another JsonNotificationIndex table in the same database.protected List<Notification>
Parse notifications from a statement ready to be executed.getTable()
protected Notification
parseNotification
(String created, String expires, String source, String type, String code, Long updateTime, String url, String data) Creates and returns aNotification
based on the provided data.void
removeNotification
(Notification notification) Remove notification from index.void
removeNotifications
(List<Notification> notifications) Remove notifications from index.boolean
Check whether schema exists.void
void
startup()
After normal startup, check whether schema exists and attempt to create.Methods inherited from class gov.usgs.earthquake.util.JDBCConnection
beginTransaction, buildUrl, close, commitTransaction, connect, getConnection, getDriver, getUrl, rollbackTransaction, setDriver, setUrl, shutdown, verifyConnection
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, shutdown
-
Field Details
-
DEFAULT_DRIVER
Variable for the default driver- See Also:
-
DEFAULT_TABLE
Variable for the default table- See Also:
-
DEFAULT_URL
Variable for the default URL- See Also:
-
-
Constructor Details
-
JsonNotificationIndex
public JsonNotificationIndex()Construct a JsonNotification using defaults. -
JsonNotificationIndex
Construct a JsonNotificationIndex with the default table.- Parameters:
driver
- Driver to useurl
- URL to use
-
JsonNotificationIndex
Construct a JsonNotificationIndex with custom driver, url, and table.- Parameters:
driver
- Driver to useurl
- URL to usetable
- Table to use
-
-
Method Details
-
getTable
- Returns:
- table
-
setTable
- Parameters:
table
- Table to set
-
configure
Description copied from class:JDBCConnection
Implement Configurable- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classJDBCConnection
- Parameters:
config
- Config to set driver and URL in- Throws:
Exception
- Exception
-
startup
After normal startup, check whether schema exists and attempt to create.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classJDBCConnection
- Throws:
Exception
- if error occurs
-
schemaExists
Check whether schema exists.- Returns:
- boolean
- Throws:
Exception
- if error occurs
-
createSchema
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
Add a notification to the index.- Specified by:
addNotification
in interfaceNotificationIndex
- Parameters:
notification
- To be added to index- Throws:
Exception
- if error occurs
-
removeNotification
Remove notification from index.- Specified by:
removeNotification
in interfaceNotificationIndex
- Parameters:
notification
- to be removed from index- Throws:
Exception
- if error occurs
-
removeNotifications
Remove notifications from index.- Specified by:
removeNotifications
in interfaceNotificationIndex
- 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 interfaceNotificationIndex
- 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 interfaceNotificationIndex
- 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
Find notifications with expires time before or equal to current time.- Specified by:
findExpiredNotifications
in interfaceNotificationIndex
- Returns:
- list with matching notifications, empty if not found.
- Throws:
Exception
- if error occurs
-
findNotifications
Search index for notifications for a specific product.- Specified by:
findNotifications
in interfaceNotificationIndex
- Parameters:
id
- the product id to search.- Returns:
- list with matching notifications, empty if not found.
- Throws:
Exception
- if error occurs
-
getMissingNotifications
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
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 aNotification
based on the provided data.- Return a JSONNotification if
created
anddata
are set - Return a URLNotification if
url
is set - Otherwise, return a DefaultNotification
- Parameters:
created
- When createdexpires
- When notification expiressource
- sourcestype
- typescode
- codesupdateTime
- updateTimeurl
- URLdata
- data- Returns:
- Notification, JSONNotification, URLNotification, or DefaultNotification
- Throws:
Exception
- if error occurs
- Return a JSONNotification if
-