org.oddjob.sql
Class SQLKeeperService

java.lang.Object
  extended by org.oddjob.sql.SQLKeeperService

public class SQLKeeperService
extends Object

Description

Provides a 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.

Example

See the User Guide.
Author:
Rob Gordon.

Field Summary
static String TABLE_NAME
          The default table name.
 
Constructor Summary
SQLKeeperService()
           
 
Method Summary
protected  PreparedStatement createInsertStatementFor(Connection connection, String keeperKey, String ourIdentifier, Object instanceIdentifier)
          Provide a PreparedStatement for the insert operation.
protected  PreparedStatement createQueryStatementFor(Connection connection, String keeperKey, Object instanceIdentifier)
          Create the PreparedStatement for the query of who won and is work complete yet.
protected  PreparedStatement createUpdateStatementFor(Connection connection, String keeperKey, Object instanceIdentifier)
          Create the PreparedStatemenet for updating won work is complete.
 Keeper getKeeper(String keeperKey)
          Provide a Keeper.
 String getName()
           
 int getPollerCount()
           
 Schedule getPollSchedule()
           
 String getTable()
           
 void setConnection(Connection connection)
          Set the connection.
 void setName(String name)
           
 void setPollSchedule(Schedule schedule)
           
 void setScheduleExecutorService(ScheduledExecutorService scheduler)
           
 void setTable(String table)
           
 void start()
          Start the service.
 void stop()
          Stop the service.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TABLE_NAME

public static final String TABLE_NAME
The default table name.

See Also:
Constant Field Values
Constructor Detail

SQLKeeperService

public SQLKeeperService()
Method Detail

start

public void start()
           throws SQLException
Start the service.

Throws:
SQLException

stop

public void stop()
          throws SQLException
Stop the service.

Throws:
SQLException

getKeeper

public Keeper getKeeper(String keeperKey)
Provide a Keeper.

Parameters:
keeperKey - The keepers key. Must not be null.
Returns:
A keeper. Never null.

getName

public String getName()

setName

public void setName(String name)

setConnection

public void setConnection(Connection connection)
                   throws SQLException
Set the connection.

Parameters:
connection - The connection.
Throws:
SQLException

getPollerCount

public int getPollerCount()

Property: pollerCount

Description: The number of outstanding loosing outcome's polling of the database that are still in progress.

Returns:
The number.

createInsertStatementFor

protected PreparedStatement createInsertStatementFor(Connection connection,
                                                     String keeperKey,
                                                     String ourIdentifier,
                                                     Object instanceIdentifier)
                                              throws SQLException
Provide a PreparedStatement for the insert operation.

Parameters:
connection -
keeperKey -
ourIdentifier -
instanceIdentifier -
Returns:
Throws:
SQLException

createQueryStatementFor

protected PreparedStatement createQueryStatementFor(Connection connection,
                                                    String keeperKey,
                                                    Object instanceIdentifier)
                                             throws SQLException
Create the PreparedStatement for the query of who won and is work complete yet.

Parameters:
connection -
keeperKey -
instanceIdentifier -
Returns:
Throws:
SQLException

createUpdateStatementFor

protected PreparedStatement createUpdateStatementFor(Connection connection,
                                                     String keeperKey,
                                                     Object instanceIdentifier)
                                              throws SQLException
Create the PreparedStatemenet for updating won work is complete.

Parameters:
connection -
keeperKey -
instanceIdentifier -
Returns:
Throws:
SQLException

getTable

public String getTable()

setTable

public void setTable(String table)

getPollSchedule

public Schedule getPollSchedule()

setPollSchedule

public void setPollSchedule(Schedule schedule)

setScheduleExecutorService

@Inject
public void setScheduleExecutorService(ScheduledExecutorService scheduler)

toString

public String toString()
Overrides:
toString in class Object