Class AbstractWork
- All Implemented Interfaces:
Serializable,Work
- Direct Known Subclasses:
AsyncEventExecutor.ListenerWork,BaseIndexingWorker,BatchFinderWork,BatchProcessorWork,BinaryMetadataUpdateWork,BinaryMetadataWork,BlobProviderDocumentsUpdateWork,ComputeDigestWork,DocumentRoutingEscalationServiceImpl.EscalationRuleWork,DuplicateCollectionMemberWork,ESAuditMigrationWork,FulltextExtractorWork,ImagingRecomputeWork,PictureViewsGenerationWork,PreviewWork,QuotaMaxSizeSetterWork,QuotaStatsInitialWork,RemovedAbstractWork,RemoveFromCollectionWork,RenditionWork,SleepWork,ThreeDBatchUpdateWork,ThumbnailRecomputeWork,TransiantStorageGCWork,TransientStoreWork,UpdateACEStatusWork,UserProfileImporterWork,VideoConversionWork,VideoInfoWork,VideoStoryboardWork
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().
- Since:
- 5.6
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.nuxeo.ecm.core.work.api.Work
Work.Progress, Work.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringprotected longprotected StringDoc id for the Work instance, if relevant.Doc ids for the Work instance, if relevant.static final Stringstatic final Stringstatic final Stringprotected Stringprotected booleanIftrue, the docId is only the root of a set of documents on which this Work instance will act.protected NuxeoLoginContextprotected StringThe originating username to use when opening theCoreSession.protected Work.Progressprotected static final Randomprotected StringRepository name for the Work instance, if relevant.protected WorkSchedulePathprotected longprotected CoreSessionprotected longprotected Work.Stateprotected Stringprotected booleanSuspend acknowledged by the work instance.protected booleanSuspend requested by the work manager.protected byte[]static final Stringstatic final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbuildWorkFailureEventProps(RuntimeException exception) Builds failure event properties.voidThis method is called afterwork()is done in a finally block, whether work completed normally or was in error or was interrupted.voidCloses the session that was opened byopenSystemSession()oropenUserSession().voidReleases the transaction resources by committing the existing transaction (if any).booleanGets the category for this work.longGets the time at which this work instance was completed, suspended or failed.Gets the document impacted by the work.Gets the documents impacted by the work.getId()The work id.Gets the user on behalf of which this work is done.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.Gets a progress report for this work instance.intGets the number of times that this Work instance can be retried in case of concurrent update exceptions.Returns the schedule pathlongGets the time at which this work instance was first scheduled.protected io.opencensus.trace.SpangetSpanFromContext(byte[] traceContext) longGets the time at which this work instance was first started.Gets a human-readable status for this work instance.CALLED BY THE WORK MANAGER (not user code) to get this work instance's state.inthashCode()booleanReturnstrueifWork.getDocument()is only the root of a set of documents on which this Work instance will act.booleanChecks if a suspend has been requested for this work instance by the work manager.booleanCALLED BY THE WORK MANAGER (not user code) to check if this work instance really suspended.protected DocumentLocationnewDocumentLocation(String docId) voidMay be called by implementing classes to open a System session on the repository.voidMay be called by implementing classes to open a Use session on the repository.voidrun()Runs the work instance and does all the transaction management and retry.protected voidDoes work under a transaction.protected voidvoidsetDocument(String repositoryName, String docId) voidsetDocument(String repositoryName, String docId, boolean isTree) voidsetDocuments(String repositoryName, List<String> docIds) voidsetOriginatingUsername(String originatingUsername) voidsetProgress(Work.Progress progress) This method should be called periodically by the actual work method when it knows of its progress.voidSet the schedule path, internal usagevoidCALLED BY THE WORK MANAGER (not user code) to set the start time on the work instance.voidSets a human-readable status for this work instance.voidsetWorkInstanceState(Work.State state) CALLED BY THE WORK MANAGER (not user code) to set this work instance's state.voidCALLED BY THE WORK MANAGER (not user code) when it requests that this work instance be suspended.booleanStarts a new transaction.voidMust be called byWorkimplementations to advertise that state saving is done, whenWork.isSuspending()returnedtrue.toString()abstract voidwork()This method should implement the actual work done by theWorkinstance.voidworkFailed(RuntimeException exception) Called when the worker failed to run successfully even after retrying.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.nuxeo.ecm.core.work.api.Work
getTitle, isCoalescing, isGroupJoin, isIdempotent, onGroupJoinCompletion
-
Field Details
-
RANDOM
-
WORK_FAILED_EVENT
- See Also:
-
WORK_INSTANCE
- See Also:
-
FAILURE_MSG
- See Also:
-
FAILURE_EXCEPTION
- See Also:
-
id
-
suspending
protected transient volatile boolean suspendingSuspend requested by the work manager. -
suspended
protected transient volatile boolean suspendedSuspend acknowledged by the work instance. -
state
-
progress
-
repositoryName
Repository name for the Work instance, if relevant. -
docId
Doc id for the Work instance, if relevant. This describes for the WorkManager a document on which this Work instance will act.Either docId or docIds is set. Not both.
-
docIds
Doc ids for the Work instance, if relevant. This describes for the WorkManager the documents on which this Work instance will act.Either docId or docIds is set. Not both.
-
isTree
protected boolean isTreeIftrue, the docId is only the root of a set of documents on which this Work instance will act. -
originatingUsername
The originating username to use when opening theCoreSession.- Since:
- 8.1
-
status
-
schedulingTime
protected long schedulingTime -
startTime
protected long startTime -
completionTime
protected long completionTime -
session
-
loginContext
-
schedulePath
-
callerThread
-
GLOBAL_DLQ_COUNT_REGISTRY_NAME
-
traceContext
protected byte[] traceContext
-
-
Constructor Details
-
AbstractWork
public AbstractWork()Constructs aWorkinstance with a unique id. -
AbstractWork
-
-
Method Details
-
getId
Description copied from interface:WorkThe work id.The id is used for equality comparisons, and as a key in persistent queues.
-
getSchedulePath
Description copied from interface:WorkReturns the schedule path- Specified by:
getSchedulePathin interfaceWork
-
setSchedulePath
Description copied from interface:WorkSet the schedule path, internal usage- Specified by:
setSchedulePathin interfaceWork
-
setDocument
-
setDocument
-
setDocuments
-
setOriginatingUsername
- Since:
- 8.1
-
setWorkInstanceSuspending
public void setWorkInstanceSuspending()Description copied from interface:WorkCALLED BY THE WORK MANAGER (not user code) when it requests that this work instance be suspended.- Specified by:
setWorkInstanceSuspendingin interfaceWork
-
isSuspending
public boolean isSuspending()Description copied from interface:WorkChecks if a suspend has been requested for this work instance by the work manager.If
true, then state should be saved,Work.suspended()should be called, and theWork.work()method should return.- Specified by:
isSuspendingin interfaceWork
-
suspended
public void suspended()Description copied from interface:WorkMust be called byWorkimplementations to advertise that state saving is done, whenWork.isSuspending()returnedtrue. After this is called, theWork.work()method should return. -
isWorkInstanceSuspended
public boolean isWorkInstanceSuspended()Description copied from interface:WorkCALLED BY THE WORK MANAGER (not user code) to check if this work instance really suspended.- Specified by:
isWorkInstanceSuspendedin interfaceWork
-
setWorkInstanceState
Description copied from interface:WorkCALLED BY THE WORK MANAGER (not user code) to set this work instance's state.- Specified by:
setWorkInstanceStatein interfaceWork
-
getWorkInstanceState
Description copied from interface:WorkCALLED BY THE WORK MANAGER (not user code) to get this work instance's state.Used only to get the final state of a completed instance.
- Specified by:
getWorkInstanceStatein interfaceWork
-
setProgress
Description copied from interface:WorkThis method should be called periodically by the actual work method when it knows of its progress.- Specified by:
setProgressin interfaceWork- Parameters:
progress- the progress- See Also:
-
getProgress
Description copied from interface:WorkGets a progress report for this work instance.- Specified by:
getProgressin interfaceWork- Returns:
- a progress report, not
null
-
setStatus
Sets a human-readable status for this work instance.- Parameters:
status- the status
-
getStatus
Description copied from interface:WorkGets a human-readable status for this work instance. -
openSystemSession
public void openSystemSession()May be called by implementing classes to open a System session on the repository.- Since:
- 8.1
-
openUserSession
public void openUserSession()May be called by implementing classes to open a Use session on the repository.It uses the set
originatingUsernameto open the session.- Since:
- 8.1
-
closeSession
public void closeSession()Closes the session that was opened byopenSystemSession()oropenUserSession().- Since:
- 5.8
-
run
public void run()Description copied from interface:WorkRuns the work instance and does all the transaction management and retry. -
getSpanFromContext
protected io.opencensus.trace.Span getSpanFromContext(byte[] traceContext) -
buildWorkFailureEventProps
Builds failure event properties. Work implementations can override this method to inject more event properties than the default.- Since:
- 10.1
-
workFailed
Called when the worker failed to run successfully even after retrying.- Parameters:
exception- the exception that occurred- Since:
- 10.1
-
appendWorkToDeadLetterQueue
protected void appendWorkToDeadLetterQueue() -
runWorkWithTransaction
protected void runWorkWithTransaction()Does work under a transaction.- Since:
- 5.9.4
-
work
public abstract void work()Description copied from interface:WorkThis method should implement the actual work done by theWorkinstance.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 callWork.isSuspending(), and iftruecallWork.suspended()return early with saved state data.Clean up can by implemented by
Work.cleanUp(boolean, Exception). -
getRetryCount
public int getRetryCount()Gets the number of times that this Work instance can be retried in case of concurrent update exceptions.- Returns:
- 0 for no retry, or more if some retries are possible
- Since:
- 5.8
- See Also:
-
cleanUp
This method is called afterwork()is done in a finally block, whether work completed normally or was in error or was interrupted. -
getOriginatingUsername
Description copied from interface:WorkGets the user on behalf of which this work is done.- Specified by:
getOriginatingUsernamein interfaceWork- Returns:
- the originating username, or
null
-
getSchedulingTime
public long getSchedulingTime()Description copied from interface:WorkGets the time at which this work instance was first scheduled.- Specified by:
getSchedulingTimein interfaceWork- Returns:
- the scheduling time (milliseconds since epoch)
-
getStartTime
public long getStartTime()Description copied from interface:WorkGets the time at which this work instance was first started.- Specified by:
getStartTimein interfaceWork- Returns:
- the start time (milliseconds since epoch), or
0if not stated
-
getCompletionTime
public long getCompletionTime()Description copied from interface:WorkGets the time at which this work instance was completed, suspended or failed.- Specified by:
getCompletionTimein interfaceWork- Returns:
- the completion time (milliseconds since epoch), or
0if not completed
-
setStartTime
public void setStartTime()Description copied from interface:WorkCALLED BY THE WORK MANAGER (not user code) to set the start time on the work instance.- Specified by:
setStartTimein interfaceWork
-
setCompletionTime
protected void setCompletionTime() -
getCategory
Description copied from interface:WorkGets the category for this work.Used to choose an execution queue.
- Specified by:
getCategoryin interfaceWork- Returns:
- the category, or
nullfor the default
-
toString
-
getDocument
Description copied from interface:WorkGets the document impacted by the work.Returns
nullif the work isn't about a single document.- Specified by:
getDocumentin interfaceWork- Returns:
- the document, or
null. This is always aDocumentLocationwith anIdRef
-
getDocuments
Description copied from interface:WorkGets the documents impacted by the work.Returns
nullif the work isn't about documents.- Specified by:
getDocumentsin interfaceWork- Returns:
- the documents, or an empty list. List elements are always a
DocumentLocationwith anIdRef
-
newDocumentLocation
-
isDocumentTree
public boolean isDocumentTree()Description copied from interface:WorkReturnstrueifWork.getDocument()is only the root of a set of documents on which this Work instance will act.- Specified by:
isDocumentTreein interfaceWork- Returns:
trueif a whole tree is impacted
-
commitOrRollbackTransaction
public void commitOrRollbackTransaction()Releases the transaction resources by committing the existing transaction (if any). This is recommended before running a long process. -
startTransaction
public boolean startTransaction()Starts a new transaction.Usually called after
commitOrRollbackTransaction(), for instance for saving back the results of a long process.- Returns:
- true if a new transaction was started
-
equals
-
hashCode
public int hashCode() -
getPartitionKey
Description copied from interface:WorkReturns 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.- Specified by:
getPartitionKeyin interfaceWork
-