public abstract class AbstractWork extends Object implements Work
Work instance, dealing with most of the details around state change.
It also deals with transaction management, and prevents running work instances that are suspending.
Actual implementations must at a minimum implement the work() method. A method cleanUp(boolean, java.lang.Exception) is
available.
To deal with suspension, work() should periodically check for isSuspending() and if true save its
state and call suspended().
Specific information about the work can be returned by getDocument() or getDocuments().
Work.Progress, Work.State| Modifier and Type | Field and Description |
|---|---|
protected String |
callerThread |
protected long |
completionTime |
protected String |
docId
Doc id for the Work instance, if relevant.
|
protected List<String> |
docIds
Doc ids for the Work instance, if relevant.
|
static String |
FAILURE_EXCEPTION |
static String |
FAILURE_MSG |
protected String |
id |
protected boolean |
isTree
If
true, the docId is only the root of a set of documents on which this Work instance will act. |
protected LoginContext |
loginContext |
protected String |
originatingUsername
The originating username to use when opening the
CoreSession. |
protected Work.Progress |
progress |
protected static Random |
RANDOM |
protected String |
repositoryName
Repository name for the Work instance, if relevant.
|
protected WorkSchedulePath |
schedulePath |
protected long |
schedulingTime |
protected CoreSession |
session |
protected long |
startTime |
protected Work.State |
state |
protected String |
status |
protected boolean |
suspended
Suspend acknowledged by the work instance.
|
protected boolean |
suspending
Suspend requested by the work manager.
|
static String |
WORK_FAILED_EVENT |
static String |
WORK_INSTANCE |
| Constructor and Description |
|---|
AbstractWork()
Constructs a
Work instance with a unique id. |
AbstractWork(String id) |
| Modifier and Type | Method and Description |
|---|---|
Map<String,Serializable> |
buildWorkFailureEventProps(RuntimeException exception)
Builds failure event properties.
|
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. |
void |
closeSession()
Closes the session that was opened by
openSystemSession() or openUserSession(). |
void |
commitOrRollbackTransaction()
Releases the transaction resources by committing the existing transaction (if any).
|
boolean |
equals(Object other) |
String |
getCategory()
Gets the category for this work.
|
long |
getCompletionTime()
Gets the time at which this work instance was completed, suspended or failed.
|
DocumentLocation |
getDocument()
Gets the document impacted by the work.
|
List<DocumentLocation> |
getDocuments()
Gets the documents impacted by the work.
|
String |
getId()
The work id.
|
String |
getOriginatingUsername()
Gets the user on behalf of which this work is done.
|
String |
getPartitionKey()
Returns a key that can be used by the WorkManager implementation to guarantee that works with the same partition
key will be executed in the order they are submitted.
|
Work.Progress |
getProgress()
Gets a progress report for this work instance.
|
int |
getRetryCount()
Gets the number of times that this Work instance can be retried in case of concurrent update exceptions.
|
WorkSchedulePath |
getSchedulePath()
Returns the schedule path
|
long |
getSchedulingTime()
Gets the time at which this work instance was first scheduled.
|
long |
getStartTime()
Gets the time at which this work instance was first started.
|
String |
getStatus()
Gets a human-readable status for this work instance.
|
Work.State |
getWorkInstanceState()
CALLED BY THE WORK MANAGER (not user code) to get this work instance's state.
|
int |
hashCode() |
CoreSession |
initSession()
Deprecated.
since 8.1. Use
openSystemSession(). |
CoreSession |
initSession(String repositoryName)
Deprecated.
since 8.1. Use
openSystemSession() to open a session on the configured repository name,
otherwise use CoreInstance.openCoreSessionSystem(String). |
boolean |
isDocumentTree()
Returns
true if Work.getDocument() is only the root of a set of documents on which this Work instance
will act. |
boolean |
isSuspending()
Checks if a suspend has been requested for this work instance by the work manager.
|
boolean |
isWorkInstanceSuspended()
CALLED BY THE WORK MANAGER (not user code) to check if this work instance really suspended.
|
protected DocumentLocation |
newDocumentLocation(String docId) |
void |
openSystemSession()
May be called by implementing classes to open a System session on the repository.
|
void |
openUserSession()
May be called by implementing classes to open a Use session on the repository.
|
void |
run()
Runs the work instance and does all the transaction management and retry.
|
protected void |
runWorkWithTransaction()
Does work under a transaction.
|
protected void |
setCompletionTime() |
void |
setDocument(String repositoryName,
String docId) |
void |
setDocument(String repositoryName,
String docId,
boolean isTree) |
void |
setDocuments(String repositoryName,
List<String> docIds) |
void |
setOriginatingUsername(String originatingUsername) |
void |
setProgress(Work.Progress progress)
This method should be called periodically by the actual work method when it knows of its progress.
|
void |
setSchedulePath(WorkSchedulePath path)
Set the schedule path, internal usage
|
void |
setStartTime()
CALLED BY THE WORK MANAGER (not user code) to set the start time on the work instance.
|
void |
setStatus(String status)
Sets a human-readable status for this work instance.
|
void |
setWorkInstanceState(Work.State state)
CALLED BY THE WORK MANAGER (not user code) to set this work instance's state.
|
void |
setWorkInstanceSuspending()
CALLED BY THE WORK MANAGER (not user code) when it requests that this work instance be suspended.
|
boolean |
startTransaction()
Starts a new transaction.
|
void |
suspended()
Must be called by
Work implementations to advertise that state saving is done, when
Work.isSuspending() returned true. |
String |
toString() |
abstract void |
work()
This method should implement the actual work done by the
Work instance. |
void |
workFailed(RuntimeException exception)
Called when the worker failed to run successfully even after retrying.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitgetTitle, isCoalescing, isIdempotentpublic static final String WORK_FAILED_EVENT
public static final String WORK_INSTANCE
public static final String FAILURE_MSG
public static final String FAILURE_EXCEPTION
protected transient volatile boolean suspending
protected transient volatile boolean suspended
protected Work.State state
protected Work.Progress progress
protected String repositoryName
protected String docId
Either docId or docIds is set. Not both.
protected List<String> docIds
Either docId or docIds is set. Not both.
protected boolean isTree
true, the docId is only the root of a set of documents on which this Work instance will act.protected String originatingUsername
CoreSession.protected long schedulingTime
protected long startTime
protected long completionTime
protected transient CoreSession session
protected transient LoginContext loginContext
protected WorkSchedulePath schedulePath
protected String callerThread
public AbstractWork()
Work instance with a unique id.public AbstractWork(String id)
public String getId()
WorkThe id is used for equality comparisons, and as a key in persistent queues.
public WorkSchedulePath getSchedulePath()
WorkgetSchedulePath in interface Workpublic void setSchedulePath(WorkSchedulePath path)
WorksetSchedulePath in interface Workpublic void setDocument(String repositoryName, String docId, boolean isTree)
public void setDocument(String repositoryName, String docId)
public void setDocuments(String repositoryName, List<String> docIds)
public void setOriginatingUsername(String originatingUsername)
public void setWorkInstanceSuspending()
WorksetWorkInstanceSuspending in interface Workpublic boolean isSuspending()
Work
If true, then state should be saved, Work.suspended() should be called, and the Work.work()
method should return.
isSuspending in interface Workpublic void suspended()
WorkWork implementations to advertise that state saving is done, when
Work.isSuspending() returned true. After this is called, the Work.work() method should return.public boolean isWorkInstanceSuspended()
WorkisWorkInstanceSuspended in interface Workpublic void setWorkInstanceState(Work.State state)
WorksetWorkInstanceState in interface Workpublic Work.State getWorkInstanceState()
Work
Used only to get the final state of a completed instance ( State#COMPLETED, State#FAILED or
State#CANCELED).
getWorkInstanceState in interface Workpublic void setProgress(Work.Progress progress)
WorksetProgress in interface Workprogress - the progressProgress#Progress(float),
Progress#Progress(long, long)public Work.Progress getProgress()
WorkgetProgress in interface Worknullpublic void setStatus(String status)
status - the statuspublic String getStatus()
Work@Deprecated public CoreSession initSession()
openSystemSession().session field)public void openSystemSession()
public void openUserSession()
It uses the set originatingUsername to open the session.
@Deprecated public CoreSession initSession(String repositoryName)
openSystemSession() to open a session on the configured repository name,
otherwise use CoreInstance.openCoreSessionSystem(String).repositoryName - the repository namesession field)public void closeSession()
openSystemSession() or openUserSession().public void run()
Work
Usually only implemented by AbstractWork, which should be subclassed instead of implementing Work.run()
.
public Map<String,Serializable> buildWorkFailureEventProps(RuntimeException exception)
public void workFailed(RuntimeException exception)
exception - the exception that occurredprotected void runWorkWithTransaction()
public abstract void work()
WorkWork instance.
It should periodically update its progress through Work.setProgress(org.nuxeo.ecm.core.work.api.Work.Progress).
To allow for suspension by the WorkManager, it should periodically call Work.isSuspending(), and if
true call Work.suspended() return early with saved state data.
Clean up can by implemented by #cleanUp().
work in interface WorkWork.isSuspending(),
Work.suspended(),
Work.cleanUp(boolean, java.lang.Exception)public int getRetryCount()
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.public String getOriginatingUsername()
WorkgetOriginatingUsername in interface Worknullpublic long getSchedulingTime()
WorkgetSchedulingTime in interface Workpublic long getStartTime()
WorkgetStartTime in interface Work0 if not statedpublic long getCompletionTime()
WorkgetCompletionTime in interface Work0 if not completedpublic void setStartTime()
WorksetStartTime in interface Workprotected void setCompletionTime()
public String getCategory()
WorkUsed to choose an execution queue.
getCategory in interface Worknull for the defaultpublic DocumentLocation getDocument()
Work
Returns null if the work isn't about a single document.
getDocument in interface Worknull. This is always a DocumentLocation with an IdRefpublic List<DocumentLocation> getDocuments()
Work
Returns null if the work isn't about documents.
getDocuments in interface WorkDocumentLocation with an
IdRefprotected DocumentLocation newDocumentLocation(String docId)
public boolean isDocumentTree()
Worktrue if Work.getDocument() is only the root of a set of documents on which this Work instance
will act.isDocumentTree in interface Worktrue if a whole tree is impactedpublic void commitOrRollbackTransaction()
public boolean startTransaction()
Usually called after commitOrRollbackTransaction(), for instance for saving back the results of a long
process.
public String getPartitionKey()
WorkgetPartitionKey in interface WorkCopyright © 2019 Nuxeo. All rights reserved.