|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.almworks.sqlite4java.SQLiteJob<T>
T
- type of the resultpublic abstract class SQLiteJob<T>
SQLiteJob is a unit of work accepted by SQLiteQueue
. You can
implement job(com.almworks.sqlite4java.SQLiteConnection)
method and add the job to the queue with SQLiteQueue.execute(J)
method.
<T>
defines the type of the result, and the
value of the result is returned by the job()
method. If job finishes unsuccessfully or is cancelled,
the result is always null. If you don't need the job to have a result, define it as
SQLiteJob<Object>
or SQLiteJob<Void>
and return null from the job()
method.
Job implements Future
interface and can be used along with different types of future results.
Also, you can override methods jobStarted(com.almworks.sqlite4java.SQLiteConnection)
, jobFinished(T)
, jobCancelled()
and
jobError(java.lang.Throwable)
to implement callbacks during the job's lifecycle.
SQLiteJob is a one-time object. Once the job is finished, it cannot be executed again.
Public methods of SQLiteJob are thread-safe unless specified otherwise. Protected methods are mostly called
from the database thread and must be overridden carefully.
When programming a job, it's a good practice to keep transaction boundaries within single job. That is, if you
BEGIN TRANSACTION in the job, make sure you COMMIT or ROLLBACK in the end. Otherwise, your transaction will
remain unfinished, locks held, and you possible wouldn't know which job will execute next in the context of
this unfinished transaction.
SQLiteQueue
Constructor Summary | |
---|---|
SQLiteJob()
|
Method Summary | |
---|---|
void |
cancel()
Cancels this job. |
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this job. |
T |
complete()
Wait if necessary for the job to complete and return the result. |
T |
get()
Waits if necessary for the job to complete, and then retrieves its result. |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Waits if necessary for at most the given time for the job to complete, and then retrieves its result, if available. |
java.lang.Throwable |
getError()
Returns the error thrown by the job. |
protected SQLiteQueue |
getQueue()
Returns the instance of the queue that is currently running the job. |
boolean |
isCancelled()
Returns true if this job was cancelled before it completed normally. |
boolean |
isDone()
Returns true if this job completed. |
protected abstract T |
job(SQLiteConnection connection)
Performs work on the SQLite database. |
protected void |
jobCancelled()
This method is called after job has been cancelled, either due to call to the cancel(boolean) method,
or because queue has stopped, or for any other reason. |
protected void |
jobError(java.lang.Throwable error)
This method is called after job(com.almworks.sqlite4java.SQLiteConnection) method has thrown an exception. |
protected void |
jobFinished(T result)
This method is called when the job is no longer in the queue. |
protected void |
jobStarted(SQLiteConnection connection)
This method is called when the job is about to be executed, before call to job(com.almworks.sqlite4java.SQLiteConnection) method. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public SQLiteJob()
Method Detail |
---|
protected abstract T job(SQLiteConnection connection) throws java.lang.Throwable
connection
- an open connection to the database, not null
java.lang.Throwable
- on any problemSQLiteQueue.execute(J)
protected void jobStarted(SQLiteConnection connection) throws java.lang.Throwable
job(com.almworks.sqlite4java.SQLiteConnection)
method.
This method may not be called at all if a job is cancelled before execution starts.
connection
- an open connection to the database, not null
java.lang.Throwable
- on any problemprotected void jobFinished(T result) throws java.lang.Throwable
SQLiteQueue.execute(J)
is called and the time when this job is no longer in the queue
nor being executed.
The result of the job is passed as a parameter.
result
- the result of the job, or null if the job was cancelled or has thrown exception
java.lang.Throwable
- on any problemprotected void jobError(java.lang.Throwable error) throws java.lang.Throwable
job(com.almworks.sqlite4java.SQLiteConnection)
method has thrown an exception. The exception is passed
as a parameter.
error
- exception thrown by the job
java.lang.Throwable
- on any problem, or the rethrown exceptionprotected void jobCancelled() throws java.lang.Throwable
cancel(boolean)
method,
or because queue has stopped, or for any other reason.
java.lang.Throwable
- on any problemprotected final SQLiteQueue getQueue()
public java.lang.Throwable getError()
job(com.almworks.sqlite4java.SQLiteConnection)
method, or null.public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public boolean cancel(boolean mayInterruptIfRunning)
SQLiteConnection.interrupt()
method is called to cancel a potentially long-running SQL. If there's
no SQL running, it will have no effect. The running job may check isCancelled()
method and finish
prematurely. There are no other means to cancel a running job.
If the job is still pending, then jobCancelled()
and jobFinished(T)
callbacks are called during
the execution of this method.
cancel
in interface java.util.concurrent.Future<T>
mayInterruptIfRunning
- true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to complete
public void cancel()
cancel(true)
.
cancel(boolean)
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
complete()
, is a way to block the current thread
and wait for the result.
get
in interface java.util.concurrent.Future<T>
java.util.concurrent.CancellationException
- if the job was cancelled
java.util.concurrent.ExecutionException
- if the job threw an exception
java.lang.InterruptedException
- if the current thread was interrupted while waitingpublic T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get
in interface java.util.concurrent.Future<T>
timeout
- the maximum time to waitunit
- the time unit of the timeout argument
java.util.concurrent.CancellationException
- if the job was cancelled
java.util.concurrent.ExecutionException
- if the job threw an exception
java.lang.InterruptedException
- if the current thread was interrupted while waiting
java.util.concurrent.TimeoutException
- if the wait timed outpublic T complete()
get()
without having to catch exceptions.
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |