public abstract class AbstractWork extends Object implements Work
Work
instance, dealing with most of the
details around state change.
Actual implementations must at a minimum implement the work()
method. A method cleanUp(boolean, java.lang.Exception)
is available.
To deal with suspension, the method suspendFromQueue()
should be
implemented, and work()
should periodically check for
isSuspending()
.
Specific information about the work can be returned by
getPrincipal()
and getDocuments()
.
Work.Progress, Work.State
Constructor and Description |
---|
AbstractWork() |
Modifier and Type | Method and Description |
---|---|
void |
afterRun(boolean ok)
Called by the thread pool executor after the work is run.
|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Wait for the work to be completed or suspension finished.
|
void |
beforeRun()
Called by the thread pool executor before the work is run.
|
void |
cleanUp(boolean ok,
Exception e)
This method is called after
work() is done in a finally block,
whether work completed normally or was in error or was interrupted. |
String |
getCategory()
Gets the category for this work.
|
long |
getCompletionTime()
Gets the time at which this work instance was completed, suspended or
failed.
|
Map<String,Serializable> |
getData()
Gets the state data for this suspended work instance.
|
Collection<DocumentLocation> |
getDocuments()
Gets the documents impacted by the work.
|
Principal |
getPrincipal()
Gets the principal on behalf of which this work is done.
|
Work.Progress |
getProgress()
Gets a progress report for this work instance.
|
long |
getSchedulingTime()
Gets the time at which this work instance was scheduled.
|
long |
getStartTime()
Gets the time at which this work instance was started.
|
Work.State |
getState()
Gets the running state for this work instance.
|
String |
getStatus()
Gets a human-readable status for this work instance.
|
void |
run()
The actual work.
|
void |
setCanceled()
Sets the state of this queued work instance to
CANCELED . |
void |
setData(Map<String,Serializable> data)
Restores a saved state data for this work instance.
|
boolean |
suspend()
Requests that this work instance suspend its state in memory.
|
String |
toString() |
abstract void |
work()
This method should implement the actual work done by the
Work
instance. |
public void setData(Map<String,Serializable> data)
Work
public Work.Progress getProgress()
Work
getProgress
in interface Work
null
public String getStatus()
Work
public void beforeRun()
Work
public void run()
Work
public abstract void work() throws Exception
Work
instance.
It should periodically call #setProgress()
to report its
progress.
To allow for suspension by the WorkManager
, it should
periodically call isSuspending()
, and if true call
#suspended()
with saved state data and return early.
Exception
public void afterRun(boolean ok)
Work
public void cleanUp(boolean ok, Exception e)
work()
is done in a finally block,
whether work completed normally or was in error or was interrupted.ok
- true
if the work completed normallye
- the exception, if availablepublic boolean suspend()
Work
Does not block. Use Work.awaitTermination(long, java.util.concurrent.TimeUnit)
to wait for actual
suspension and then Work.getData()
to get the data.
A QUEUED work instance must immediately suspend.
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
Work
awaitTermination
in interface Work
timeout
- how long to waitunit
- the timeout unittrue
if the work is completed or suspended, or
false
for a timeoutInterruptedException
public void setCanceled()
Work
CANCELED
. Called by the work manager implementation.setCanceled
in interface Work
public Map<String,Serializable> getData()
Work
public Work.State getState()
Work
public long getSchedulingTime()
Work
getSchedulingTime
in interface Work
public long getStartTime()
Work
getStartTime
in interface Work
0
if not
statedpublic long getCompletionTime()
Work
getCompletionTime
in interface Work
0
if
not completedpublic String getCategory()
Work
Used to choose a thread pool queue.
getCategory
in interface Work
null
for the defaultpublic Principal getPrincipal()
Work
This is informative only.
getPrincipal
in interface Work
null
public Collection<DocumentLocation> getDocuments()
Work
This is informative only.
getDocuments
in interface Work
Copyright © 2012 Nuxeo SA. All Rights Reserved.