public abstract class Batcher extends Object implements Runnable
Batcher uses a BlockingQueue
to provide a thread-safe means of submitting rows to be
inserted. Once a row is submitted to the Batcher, it will submit the queued rows to the
database after a certain duration of time or once a certain number of candidate rows are
available for insertion, whichever comes first. These max delay and batch size values can
be overridden in the Builder before the Batcher is instantiated.
Modifier and Type | Class and Description |
---|---|
static class |
Batcher.Builder<T extends Batcher.Builder<?>>
Utility class for building
Batcher objects with custom behavioral tweaks. |
Modifier and Type | Field and Description |
---|---|
protected String[] |
columnNames |
protected String |
columnString |
protected Integer[] |
columnTypes |
protected DataSource |
dataSource |
static int |
DEADLOCK_RETRIES
The number of attempts to make in case a SQL operation returns a deadlock error.
|
static int |
DEADLOCK_SLEEP
The number of milliseconds to wait between retries in case a SQL operation returns a
deadlock error.
|
static Duration |
DEFAULT_MAX_BATCH_DELAY
The default duration of time to wait once the
Batcher has become non-empty (i.e. |
static int |
DEFAULT_MAX_BATCH_SIZE
The default maximum number of rows that will be inserted/updated/deleted in the database
during a single SQL operation.
|
static int |
DEFAULT_QUEUE_SIZE
By default,
Batcher will store up to this many candidate rows. |
protected Duration |
maxBatchDelay |
protected int |
maxBatchSize |
protected BlockingQueue<Object[]> |
queue |
protected String |
tableName |
Modifier | Constructor and Description |
---|---|
protected |
Batcher(Batcher.Builder<?> builder)
Constructs a new
Batcher using a Batcher.Builder as a template |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object... values)
Inserts the row into the processing queue using
BlockingQueue.add(Object) . |
protected int |
execute(Collection<? extends Object> values)
Prepares and executes a single batch insert/update/delete statement as defined by
the implementation-specific
prepareSql method. |
protected int |
execute(Object... params)
Prepares and executes a single batch insert/update/delete statement as defined by
the implementation-specific
prepareSql(java.util.Collection<? extends java.lang.Object>) method. |
BlockingQueue<Object[]> |
getQueue() |
boolean |
offer(Duration timeout,
Object... values)
Inserts the row into the processing queue using
BlockingQueue.offer(Object, long, TimeUnit) . |
boolean |
offer(Object... values)
Inserts the row into the processing queue using
BlockingQueue.offer(Object) . |
protected abstract String |
prepareSql(Collection<? extends Object> values)
Defines the SQL statement to be executed during each batch operation.
|
void |
put(Object... values)
Inserts the row into the processing queue using
BlockingQueue.put(Object) . |
void |
run() |
void |
shutdown()
Instructs the
Batcher to begin its shutdown procedure. |
public static final int DEFAULT_QUEUE_SIZE
Batcher
will store up to this many candidate rows. Additional attempts to
insert candidate rows will block or fail as specified in the underlying BlockingQueue
until subsequent SQL operations have resulted in space becoming available in the Batcher
.
This value may be overridden at runtime using the queueSize method in the Builder
.
public static final int DEFAULT_MAX_BATCH_SIZE
This value may be overridden at runtime using the maxBatchSize method in the Builder
.
public static final Duration DEFAULT_MAX_BATCH_DELAY
Batcher
has become non-empty (i.e. there
are candidate rows awaiting insertion/updates/deletions in the database) until the
SQL operations are executed.
Note that even with the default duration of zero, natural computational speed limitations may result in additional rows being submitted before the SQL operation is actually performed. Batcher offers no guarantee as to whether those rows will be included in the imminent SQL transaction or delayed until the next one.
This value may be overridden at runtime using the Batcher.Builder.maxBatchDelay
method in
the Builder
.
public static final int DEADLOCK_RETRIES
SQLException
.public static final int DEADLOCK_SLEEP
protected final DataSource dataSource
protected final BlockingQueue<Object[]> queue
protected final String tableName
protected final String[] columnNames
protected final Integer[] columnTypes
protected final String columnString
protected final int maxBatchSize
protected final Duration maxBatchDelay
protected Batcher(Batcher.Builder<?> builder)
Batcher
using a Batcher.Builder
as a templatebuilder
- the Builder
to use as a templateprotected abstract String prepareSql(Collection<? extends Object> values)
However, implementations should note that their resulting SQL statement must be
representative of a PreparedStatement
(and will in fact be used to create a
PreparedStatement
). As a result, implementations should use parameterized
placeholders ('?') to represent each element of the collection in the resulting SQL
statement, and avoid injecting the raw values directly.
values
- the values to be eventually set in the prepared SQL statementprotected int execute(Object... params) throws SQLException
prepareSql(java.util.Collection<? extends java.lang.Object>)
method.params
- the values used to populate the statement. May be null/empty.SQLException
- if an error occurs while preparing or executing the SQL queryprotected int execute(Collection<? extends Object> values) throws SQLException
prepareSql
method.values
- the values used to populate the statement. May be empty, but cannot
be null.SQLException
- if an error occurs while preparing or executing the SQL querypublic void shutdown()
Batcher
to begin its shutdown procedure. The Batcher
will submit any remaining candidate rows to the database before releasing its thread.
Note that Batcher
and its underlying BlockingQueue
will continue to
successfully handle calls to the #add
, #put
, or #offer
methods
after shutdown is requested, and even after shutdown is complete (at least until the
queue reaches its maximum capacity). However, any candidate rows added to the queue
after shutdown is request will be ignored and will NOT be submitted to the database.
public boolean add(Object... values)
BlockingQueue.add(Object)
. Adding rows directly to the queue is
also acceptable.values
- the column values of the row to addCollection.add(E)
)public boolean offer(Object... values)
BlockingQueue.offer(Object)
. Adding rows directly to the queue is
also acceptable.values
- the column values of the row to addpublic boolean offer(Duration timeout, Object... values) throws InterruptedException
BlockingQueue.offer(Object, long, TimeUnit)
. Adding rows directly
to the queue is also acceptable.timeout
- how long to wait before giving upvalues
- the column values of the row to addInterruptedException
- if interrupted while waitingpublic void put(Object... values) throws InterruptedException
BlockingQueue.put(Object)
. Adding rows directly to the queue is
also acceptable.values
- the column values of the row to addInterruptedException
- if interrupted while waitingpublic final BlockingQueue<Object[]> getQueue()
Batcher
Copyright © 2016. All rights reserved.