Class AbstractBatch

  • All Implemented Interfaces:
    Runnable
    Direct Known Subclasses:
    DefaultBatch

    public abstract class AbstractBatch
    extends Object
    implements Runnable
    A Batch that periodically flushes pending insertions to the database.

    Extending classes that want to be notified when a flush could not be performed after the timeout has been reached, must override the onFlushFailure(BatchEntry[]).

    Since:
    2.0.0
    Author:
    Rui Vilao (rui.vilao@feedzai.com)
    • Field Detail

      • logger

        protected final org.slf4j.Logger logger
        The logger.
      • NO_RETRY

        public static final int NO_RETRY
        Constant representing that no retries should be attempted on batch flush failures.
        See Also:
        Constant Field Values
      • DEFAULT_RETRY_INTERVAL

        public static final long DEFAULT_RETRY_INTERVAL
        Constant representing the default time interval (milliseconds) to wait between batch flush retries.
        See Also:
        Constant Field Values
      • dev

        protected static final org.slf4j.Marker dev
        The dev Marker.
      • maxAwaitTimeShutdown

        protected final long maxAwaitTimeShutdown
        The maximum await time to wait for the batch to shutdown.
      • batchSize

        protected final int batchSize
        The batchSize.
      • batchTimeout

        protected final long batchTimeout
        The batch timeout.
      • batch

        protected int batch
        The batch at the present moment.
      • lastFlush

        protected volatile long lastFlush
        Timestamp of the last flush.
      • name

        protected String name
        The name of the batch.
      • batchListener

        protected Optional<BatchListener> batchListener
        The listener for customized behavior when this batch succeeds or fails to persist data.
        Since:
        2.8.1
      • maxFlushRetries

        protected final int maxFlushRetries
        The number of times to retry a batch flush upon failure.

        Defaults to 0.

      • flushRetryDelay

        protected final long flushRetryDelay
        The time interval (milliseconds) to wait between batch flush retries.

        Defaults to 300L.

    • Constructor Detail

      • AbstractBatch

        protected AbstractBatch​(DatabaseEngine de,
                                String name,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown,
                                @Nullable
                                BatchListener batchListener,
                                int maxFlushRetries,
                                long flushRetryDelay)
        Creates a new instance of AbstractBatch with a BatchListener.
        Parameters:
        de - The database engine.
        name - The batch name (null or empty names are allowed, falling back to "Anonymous Batch").
        batchSize - The batch size.
        batchTimeout - The batch timeout.
        maxAwaitTimeShutdown - The maximum await time for the batch to shutdown.
        batchListener - The listener that will be invoked whenever some batch operation fail or succeeds to persist.
        maxFlushRetries - The number of times to retry a batch flush upon failure. When set to 0, no retries will be attempted.
        flushRetryDelay - The time interval (milliseconds) to wait between batch flush retries.
        Since:
        2.8.1
      • AbstractBatch

        protected AbstractBatch​(DatabaseEngine de,
                                String name,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown,
                                @Nullable
                                BatchListener batchListener)
        Creates a new instance of AbstractBatch with a BatchListener.
        Parameters:
        de - The database engine.
        name - The batch name (null or empty names are allowed, falling back to "Anonymous Batch").
        batchSize - The batch size.
        batchTimeout - The batch timeout.
        maxAwaitTimeShutdown - The maximum await time for the batch to shutdown.
        batchListener - The listener that will be invoked whenever some batch operation fail or succeeds to persist.
        Since:
        2.8.1
      • AbstractBatch

        @Deprecated
        protected AbstractBatch​(DatabaseEngine de,
                                String name,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown,
                                FailureListener failureListener,
                                int maxFlushRetries,
                                long flushRetryDelay)
        Creates a new instance of AbstractBatch with a FailureListener.
        Parameters:
        de - The database engine.
        name - The batch name (null or empty names are allowed, falling back to "Anonymous Batch").
        batchSize - The batch size.
        batchTimeout - The batch timeout.
        maxAwaitTimeShutdown - The maximum await time for the batch to shutdown.
        failureListener - The listener that will be invoked whenever some batch operation fail to persist.
        maxFlushRetries - The number of times to retry a batch flush upon failure. When set to 0, no retries will be attempted.
        flushRetryDelay - The time interval (milliseconds) to wait between batch flush retries.
        Since:
        2.1.12
      • AbstractBatch

        @Deprecated
        protected AbstractBatch​(DatabaseEngine de,
                                String name,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown,
                                FailureListener failureListener)
        Creates a new instance of AbstractBatch with a FailureListener.
        Parameters:
        de - The database engine.
        name - The batch name (null or empty names are allowed, falling back to "Anonymous Batch").
        batchSize - The batch size.
        batchTimeout - The batch timeout.
        maxAwaitTimeShutdown - The maximum await time for the batch to shutdown.
        failureListener - The listener that will be invoked whenever some batch operation fail to persist.
        Since:
        2.1.11
      • AbstractBatch

        protected AbstractBatch​(DatabaseEngine de,
                                String name,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown)
        Creates a new instance of AbstractBatch.
        Parameters:
        de - The database engine.
        name - The batch name (null or empty names are allowed, falling back to "Anonymous Batch").
        batchSize - The batch size.
        batchTimeout - The batch timeout.
        maxAwaitTimeShutdown - The maximum await time for the batch to shutdown.
      • AbstractBatch

        protected AbstractBatch​(DatabaseEngine de,
                                int batchSize,
                                long batchTimeout,
                                long maxAwaitTimeShutdown)
        Creates a new instance of AbstractBatch.
        Parameters:
        de - The database engine.
        batchSize - The batch size.
        batchTimeout - The batch timeout.
    • Method Detail

      • start

        protected void start()
        Starts the timer task.
      • destroy

        public void destroy()
        Destroys this batch.
      • flush

        public void flush()
        Flushes the pending batches.
      • flush

        public void flush​(boolean sync)
        Flushes the pending messages.

        If sync is true it waits for other pending flush operations. If it is false it can return directly if the buffer in the batch is empty.

        Parameters:
        sync - true if it should wait for other flush operations (from here, or those started by calling flush()).
        Since:
        2.1.6
      • onFlushFailure

        public void onFlushFailure​(BatchEntry[] entries)
        Notifies about the pending entries on flush failure.
        Parameters:
        entries - The entries that are pending to be persisted.
      • onFlushSuccess

        public void onFlushSuccess​(BatchEntry[] entries)
        Notifies about succeeded entries on flush success.
        Parameters:
        entries - The entries that were persisted.
        Since:
        2.8.1
      • run

        public void run()
        Specified by:
        run in interface Runnable