public class SQLKeeperService
extends java.lang.Object
Keeper
that uses a database
table.
The keeper uses a simple 'first to insert' a row wins methodology for deciding winner and looser. This is quite primitive and decides that any exception from the insert operation is a duplicate key exception and therefore a looser.
A LoosingOutcome
will continue to Poll the database (for as long
as it has listeners) until the work is complete. The default polling schedule
polls every 5 seconds indefinitely. The pollSchedule
property
can be used to poll for a limited time, after which it flags an exception
state. This could be used by loosing servers to flag the winner is taking
too long and has possibly crashed.
This is an example of the SQL that would create a suitable table.
CREATE TABLE oddjob_grabbable(
key VARCHAR(32),
instance VARCHAR(32),
winner VARCHAR(32),
complete boolean,
CONSTRAINT oddjob_pk PRIMARY KEY (key, instance))
This service does not tidy up the database so rows will grow indefinitely. A separate tidy job should be implemented.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
TABLE_NAME
The default table name.
|
Constructor and Description |
---|
SQLKeeperService() |
Modifier and Type | Method and Description |
---|---|
protected java.sql.PreparedStatement |
createInsertStatementFor(java.sql.Connection connection,
java.lang.String keeperKey,
java.lang.String ourIdentifier,
java.lang.Object instanceIdentifier)
Provide a PreparedStatement for the insert operation.
|
protected java.sql.PreparedStatement |
createQueryStatementFor(java.sql.Connection connection,
java.lang.String keeperKey,
java.lang.Object instanceIdentifier)
Create the PreparedStatement for the query of who won
and is work complete yet.
|
protected java.sql.PreparedStatement |
createUpdateStatementFor(java.sql.Connection connection,
java.lang.String keeperKey,
java.lang.Object instanceIdentifier)
Create the PreparedStatemenet for updating won work is
complete.
|
Keeper |
getKeeper(java.lang.String keeperKey)
Provide a
Keeper . |
java.lang.String |
getName() |
int |
getPollerCount() |
Schedule |
getPollSchedule() |
java.lang.String |
getTable() |
void |
setConnection(java.sql.Connection connection)
Set the connection.
|
void |
setName(java.lang.String name) |
void |
setPollSchedule(Schedule schedule) |
void |
setScheduleExecutorService(java.util.concurrent.ScheduledExecutorService scheduler) |
void |
setTable(java.lang.String table) |
void |
start()
Start the service.
|
void |
stop()
Stop the service.
|
java.lang.String |
toString() |
public static final java.lang.String TABLE_NAME
public void start() throws java.sql.SQLException
java.sql.SQLException
public void stop() throws java.sql.SQLException
java.sql.SQLException
public Keeper getKeeper(java.lang.String keeperKey)
Keeper
.keeperKey
- The keepers key. Must not be null.public java.lang.String getName()
public void setName(java.lang.String name)
public void setConnection(java.sql.Connection connection) throws java.sql.SQLException
connection
- The connection.java.sql.SQLException
public int getPollerCount()
Property: pollerCount
Description: The number of outstanding loosing outcome's polling of the database that are still in progress.
protected java.sql.PreparedStatement createInsertStatementFor(java.sql.Connection connection, java.lang.String keeperKey, java.lang.String ourIdentifier, java.lang.Object instanceIdentifier) throws java.sql.SQLException
connection
- keeperKey
- ourIdentifier
- instanceIdentifier
- java.sql.SQLException
protected java.sql.PreparedStatement createQueryStatementFor(java.sql.Connection connection, java.lang.String keeperKey, java.lang.Object instanceIdentifier) throws java.sql.SQLException
connection
- keeperKey
- instanceIdentifier
- java.sql.SQLException
protected java.sql.PreparedStatement createUpdateStatementFor(java.sql.Connection connection, java.lang.String keeperKey, java.lang.Object instanceIdentifier) throws java.sql.SQLException
connection
- keeperKey
- instanceIdentifier
- java.sql.SQLException
public java.lang.String getTable()
public void setTable(java.lang.String table)
public Schedule getPollSchedule()
public void setPollSchedule(Schedule schedule)
@Inject public void setScheduleExecutorService(java.util.concurrent.ScheduledExecutorService scheduler)
public java.lang.String toString()
toString
in class java.lang.Object