Class AbstractBatch
- java.lang.Object
-
- com.feedzai.commons.sql.abstraction.batch.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 theonFlushFailure(BatchEntry[]).- Since:
- 2.0.0
- Author:
- Rui Vilao (rui.vilao@feedzai.com)
-
-
Field Summary
Fields Modifier and Type Field Description protected intbatchThe batch at the present moment.protected Optional<BatchListener>batchListenerThe listener for customized behavior when this batch succeeds or fails to persist data.protected intbatchSizeThe batchSize.protected longbatchTimeoutThe batch timeout.protected LinkedList<BatchEntry>bufferEntityEntry buffer.protected DatabaseEnginedeThe database engine.static longDEFAULT_RETRY_INTERVALConstant representing the default time interval (milliseconds) to wait between batch flush retries.protected static org.slf4j.MarkerdevThe dev Marker.protected longflushRetryDelayThe time interval (milliseconds) to wait between batch flush retries.protected longlastFlushTimestamp of the last flush.protected org.slf4j.LoggerloggerThe logger.protected longmaxAwaitTimeShutdownThe maximum await time to wait for the batch to shutdown.protected intmaxFlushRetriesThe number of times to retry a batch flush upon failure.protected StringnameThe name of the batch.static intNO_RETRYConstant representing that no retries should be attempted on batch flush failures.protected static intsaltSalt to avoid erroneous flushes.protected ScheduledExecutorServiceschedulerThe Timer that runs this task.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractBatch(DatabaseEngine de, int batchSize, long batchTimeout, long maxAwaitTimeShutdown)Creates a new instance ofAbstractBatch.protectedAbstractBatch(DatabaseEngine de, String name, int batchSize, long batchTimeout, long maxAwaitTimeShutdown)Creates a new instance ofAbstractBatch.protectedAbstractBatch(DatabaseEngine de, String name, int batchSize, long batchTimeout, long maxAwaitTimeShutdown, FailureListener failureListener)Deprecated.protectedAbstractBatch(DatabaseEngine de, String name, int batchSize, long batchTimeout, long maxAwaitTimeShutdown, FailureListener failureListener, int maxFlushRetries, long flushRetryDelay)Deprecated.protectedAbstractBatch(DatabaseEngine de, String name, int batchSize, long batchTimeout, long maxAwaitTimeShutdown, BatchListener batchListener)Creates a new instance ofAbstractBatchwith aBatchListener.protectedAbstractBatch(DatabaseEngine de, String name, int batchSize, long batchTimeout, long maxAwaitTimeShutdown, BatchListener batchListener, int maxFlushRetries, long flushRetryDelay)Creates a new instance ofAbstractBatchwith aBatchListener.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(BatchEntry batchEntry)Adds the fields to the batch.voidadd(String entityName, EntityEntry ee)Adds the fields to the batch.static BatchListenerconvertToBatchListener(FailureListener failureListener)Deprecated.TheFailureListeneris deprecated and this method will be removed once it is removed.voiddestroy()Destroys this batch.voidflush()Flushes the pending batches.voidflush(boolean sync)Flushes the pending messages.voidonFlushFailure(BatchEntry[] entries)Notifies about the pending entries on flush failure.voidonFlushSuccess(BatchEntry[] entries)Notifies about succeeded entries on flush success.voidrun()protected voidstart()Starts the timer task.
-
-
-
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.
-
salt
protected static final int salt
Salt to avoid erroneous flushes.- See Also:
- Constant Field Values
-
de
protected final DatabaseEngine de
The database engine.
-
maxAwaitTimeShutdown
protected final long maxAwaitTimeShutdown
The maximum await time to wait for the batch to shutdown.
-
scheduler
protected ScheduledExecutorService scheduler
The Timer that runs this task.
-
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.
-
buffer
protected LinkedList<BatchEntry> buffer
EntityEntry buffer.
-
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 ofAbstractBatchwith aBatchListener.- 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 ofAbstractBatchwith aBatchListener.- 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)
Deprecated.Creates a new instance ofAbstractBatchwith aFailureListener.- 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)
Deprecated.Creates a new instance ofAbstractBatchwith aFailureListener.- 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 ofAbstractBatch.- 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 ofAbstractBatch.- 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.
-
add
public void add(BatchEntry batchEntry) throws DatabaseEngineException
Adds the fields to the batch.- Parameters:
batchEntry- The batch entry.- Throws:
DatabaseEngineException- If an error with the database occurs.
-
add
public void add(String entityName, EntityEntry ee) throws DatabaseEngineException
Adds the fields to the batch.- Parameters:
entityName- The table name.ee- The entity entry.- Throws:
DatabaseEngineException- If an error with the database occurs.
-
flush
public void flush()
Flushes the pending batches.
-
flush
public void flush(boolean sync)
Flushes the pending messages.If
syncistrueit waits for other pending flush operations. If it isfalseit can return directly if the buffer in the batch is empty.- Parameters:
sync-trueif it should wait for other flush operations (from here, or those started by callingflush()).- 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
-
convertToBatchListener
public static BatchListener convertToBatchListener(FailureListener failureListener)
Deprecated.TheFailureListeneris deprecated and this method will be removed once it is removed.Converts aFailureListenertoBatchListener.- Parameters:
failureListener- TheFailureListenerto be converted.- Returns:
- A
BatchListenerthat calls aFailureListeneron failure. - Since:
- 2.8.1
-
-