Class AbstractDatasourceSink<T extends PersistentData>

java.lang.Object
com.inductiveautomation.ignition.gateway.storeforward.sinks.AbstractSink<T>
com.inductiveautomation.ignition.gateway.storeforward.sinks.AbstractDatasourceSink<T>
Type Parameters:
T - the type of PersistentData that this sink can handle
All Implemented Interfaces:
DataConsumer<T>, DataSink<T>
Direct Known Subclasses:
HistoricalRecordSink

public abstract class AbstractDatasourceSink<T extends PersistentData> extends AbstractSink<T>
An abstract base class for sinks that use a data source to store data.
  • Field Details

    • datasourceName

      protected final String datasourceName
    • queryCache

      protected QueryCache queryCache
  • Constructor Details

    • AbstractDatasourceSink

      protected AbstractDatasourceSink(GatewayContext context, StorageId storageId, PersistentFlavor<T> flavor, int batchSize)
      Constructs an instance of AbstractDatasourceSink with the specified parameters. By default, a DatasourceListener instance will be used for monitoring the status of the associated Datasource.
      Parameters:
      context - the GatewayContext providing access to runtime context and system information
      storageId - the unique identifier associated with the storage configuration
      flavor - the PersistentFlavor representing the data format or type for persistence
      batchSize - the number of records to batch during data storage operations
    • AbstractDatasourceSink

      protected AbstractDatasourceSink(GatewayContext context, StorageId storageId, PersistentFlavor<T> flavor, int batchSize, boolean useDatasourceListener)
      Constructs an AbstractDatasourceSink instance with the specified configuration.
      Parameters:
      context - the GatewayContext object providing runtime context for the sink
      storageId - the StorageId containing the storage configuration, including the engine identifier
      flavor - the PersistentFlavor representing the type of persistent storage
      batchSize - the size of data batches for storage operations
      useDatasourceListener - a flag indicating whether a DatasourceListener should be used to monitor the status of the associated datasource
  • Method Details

    • onStartup

      protected void onStartup()
      Description copied from class: AbstractSink
      This method is a synchronized lifecycle hook intended to be implemented or overridden by subclasses of AbstractSink. It is invoked during the startup phase of the data sink.

      Subclasses can implement this method to define specific behavior that should occur when the sink becomes active, such as initializing resources, registering metrics, or preparing underlying systems. This method is invoked within the startup() method.

      It is thread-safe due to the use of synchronization and ensures that only one thread can execute this method at any given time.

      Any exceptions thrown from this method will be caught and handled by the startup process, causing the sink to transition to SinkStatus.FAULTED state. The exception will be logged and stored as the fault error, ensuring the sink does not remain in an inconsistent state.

      Overrides:
      onStartup in class AbstractSink<T extends PersistentData>
    • onShutdown

      protected void onShutdown()
      Description copied from class: AbstractSink
      Synchronized lifecycle hook intended to be implemented or overridden by subclasses of AbstractSink to define custom behavior that should occur during the shutdown phase of the sink's lifecycle.

      This method is invoked within the shutdown() method and provides a safe point for performing cleanup or resource release tasks. Subclasses can use this method to deregister metrics, close connections, or perform other operations needed to gracefully shut down the sink.

      Due to synchronization, it ensures that only one thread can execute this method at any given time. Subclasses overriding this method should ensure that no unhandled exceptions are thrown, as this could disrupt the shutdown process.

      Overrides:
      onShutdown in class AbstractSink<T extends PersistentData>
    • onInitialize

      protected void onInitialize() throws Exception
      Description copied from class: AbstractSink
      Synchronized lifecycle hook intended to be implemented or overridden by subclasses to perform initialization tasks specific to the sink. This method is invoked during the initialization phase of the sink's lifecycle.

      Subclasses can use this method to define custom logic for setting up resources, configurations, or other components necessary for the sink to function properly. The method is designed to ensure that only one thread can execute it at any given time due to synchronization.

      Any exceptions thrown from this method will be handled by the initialization process and will cause the sink to fault. When an exception is thrown, the sink's status will be set to SinkStatus.FAULTED and the exception will be logged and stored as the fault error.

      Overrides:
      onInitialize in class AbstractSink<T extends PersistentData>
      Throws:
      Exception - if an unhandled exception is thrown during initialization
    • onUninitialize

      protected void onUninitialize()
      Description copied from class: AbstractSink
      Synchronized lifecycle hook intended to be implemented or overridden by subclasses of AbstractSink to define custom teardown or cleanup operations that should occur during the uninitialization phase of the sink's lifecycle.

      This method is invoked by the uninitialize() method and provides a safe point for releasing resources, deregistering metrics, or any other tasks necessary to prepare the sink for deactivation or shutdown.

      Subclasses overriding this method should ensure no unhandled exceptions are thrown, as this could disrupt the uninitialization process. Due to synchronization, it is guaranteed that only one thread will execute this method at a time.

      Overrides:
      onUninitialize in class AbstractSink<T extends PersistentData>
    • storeData

      public void storeData(List<T> data) throws DataStorageException
      Description copied from interface: DataConsumer
      Stores a list of persistent data.
      Parameters:
      data - The data to be stored
      Throws:
      DataStorageException - if an error occurs while storing data
    • getDatasource

      protected Datasource getDatasource()
      Retrieves the configured Datasource instance associated with this data sink. The datasource provides access to a database connection pool and related database operations.
      Returns:
      the configured Datasource instance
    • isDatasourceAvailable

      protected boolean isDatasourceAvailable()
      Checks whether the datasource is available and in a valid state.

      This method retrieves the current datasource using the getDatasource() method and validates that it is not null and its extended status is VALID.

      Returns:
      true if the datasource is available and its extended status is VALID; false otherwise.
    • useBatching

      protected boolean useBatching()
      Returns whether JDBC batching should be used when inserting data.
      Returns:
      the flag indicating whether batching should be used
    • getQueryCache

      protected QueryCache getQueryCache()
      Returns the QueryCache for the Datasource belonging to this datasource sink.
      Returns:
      the datasource query cache
    • storeToDatasource

      protected void storeToDatasource(Datasource datasource, List<T> data) throws DataStorageException
      Stores the list of supported T PersistentData to the database. First, batching is done, and then the operation is handed off to storeToConnection(SRConnection, List)

      A commit occurs once all batches execute. If a failure occurs, then the transaction is rolled back.

      Parameters:
      datasource - the datasource to store persistent data to
      data - the list of persistent data to store
      Throws:
      DataStorageException - if an error occurs while storing data
    • storeToConnection

      protected abstract void storeToConnection(SRConnection conn, List<T> data) throws Exception
      Stores the given data to the specified connection.
      Parameters:
      conn - the connection to store the data to
      data - the data to be stored
      Throws:
      Exception - if there is an error storing the data
    • runFirstTimeChecks

      protected void runFirstTimeChecks(Connection conn) throws SQLException
      Runs a series of checks on the connection before allowing it to be used for storing data. This includes checking whether the connection supports batch updates, and logging a warning if it does not.
      Parameters:
      conn - the connection to check
      Throws:
      SQLException - if there is an error while running the checks
    • onStorageFailure

      protected void onStorageFailure(List<T> data, Exception e) throws DataStorageException
      Handles a storage failure scenario by logging the error details and rethrowing a DataStorageException.
      Parameters:
      data - the list of data items that failed to be stored
      e - the exception that caused the storage operation to fail
      Throws:
      DataStorageException - rethrows the provided exception wrapped as a DataStorageException