Class AbstractWork
- java.lang.Object
-
- org.nuxeo.ecm.core.work.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
public abstract class AbstractWork extends Object implements Work
A base implementation for aWork
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 methodcleanUp(boolean, java.lang.Exception)
is available.To deal with suspension,
work()
should periodically check forisSuspending()
and if true save its state and callsuspended()
.Specific information about the work can be returned by
getDocument()
orgetDocuments()
.- Since:
- 5.6
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.nuxeo.ecm.core.work.api.Work
Work.Progress, Work.State
-
-
Field Summary
Fields Modifier and Type Field 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
static String
GLOBAL_DLQ_COUNT_REGISTRY_NAME
protected String
id
protected boolean
isTree
Iftrue
, the docId is only the root of a set of documents on which this Work instance will act.protected NuxeoLoginContext
loginContext
protected String
originatingUsername
The originating username to use when opening theCoreSession
.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.protected byte[]
traceContext
static String
WORK_FAILED_EVENT
static String
WORK_INSTANCE
-
Constructor Summary
Constructors Constructor Description AbstractWork()
Constructs aWork
instance with a unique id.AbstractWork(String id)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
appendWorkToDeadLetterQueue()
Map<String,Serializable>
buildWorkFailureEventProps(RuntimeException exception)
Builds failure event properties.void
cleanUp(boolean ok, Exception e)
This method is called afterwork()
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 byopenSystemSession()
oropenUserSession()
.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 pathlong
getSchedulingTime()
Gets the time at which this work instance was first scheduled.protected io.opencensus.trace.Span
getSpanFromContext(byte[] traceContext)
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.CoreSession
initSession(String repositoryName)
Deprecated.since 8.1.boolean
isDocumentTree()
Returnstrue
ifWork.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 usagevoid
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 byWork
implementations to advertise that state saving is done, whenWork.isSuspending()
returnedtrue
.String
toString()
abstract void
work()
This method should implement the actual work done by theWork
instance.void
workFailed(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, wait
-
Methods inherited from interface org.nuxeo.ecm.core.work.api.Work
getTitle, isCoalescing, isGroupJoin, isIdempotent, onGroupJoinCompletion
-
-
-
-
Field Detail
-
RANDOM
protected static final Random RANDOM
-
WORK_FAILED_EVENT
public static final String WORK_FAILED_EVENT
- See Also:
- Constant Field Values
-
WORK_INSTANCE
public static final String WORK_INSTANCE
- See Also:
- Constant Field Values
-
FAILURE_MSG
public static final String FAILURE_MSG
- See Also:
- Constant Field Values
-
FAILURE_EXCEPTION
public static final String FAILURE_EXCEPTION
- See Also:
- Constant Field Values
-
id
protected String id
-
suspending
protected transient volatile boolean suspending
Suspend requested by the work manager.
-
suspended
protected transient volatile boolean suspended
Suspend acknowledged by the work instance.
-
state
protected Work.State state
-
progress
protected Work.Progress progress
-
repositoryName
protected String repositoryName
Repository name for the Work instance, if relevant.
-
docId
protected String 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
protected List<String> 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 isTree
Iftrue
, the docId is only the root of a set of documents on which this Work instance will act.
-
originatingUsername
protected String originatingUsername
The originating username to use when opening theCoreSession
.- Since:
- 8.1
-
status
protected String status
-
schedulingTime
protected long schedulingTime
-
startTime
protected long startTime
-
completionTime
protected long completionTime
-
session
protected transient CoreSession session
-
loginContext
protected transient NuxeoLoginContext loginContext
-
schedulePath
protected WorkSchedulePath schedulePath
-
callerThread
protected String callerThread
-
GLOBAL_DLQ_COUNT_REGISTRY_NAME
public static final String GLOBAL_DLQ_COUNT_REGISTRY_NAME
-
traceContext
protected byte[] traceContext
-
-
Method Detail
-
getId
public String getId()
Description copied from interface:Work
The work id.The id is used for equality comparisons, and as a key in persistent queues.
-
getSchedulePath
public WorkSchedulePath getSchedulePath()
Description copied from interface:Work
Returns the schedule path- Specified by:
getSchedulePath
in interfaceWork
-
setSchedulePath
public void setSchedulePath(WorkSchedulePath path)
Description copied from interface:Work
Set the schedule path, internal usage- Specified by:
setSchedulePath
in interfaceWork
-
setOriginatingUsername
public void setOriginatingUsername(String originatingUsername)
- Since:
- 8.1
-
setWorkInstanceSuspending
public void setWorkInstanceSuspending()
Description copied from interface:Work
CALLED BY THE WORK MANAGER (not user code) when it requests that this work instance be suspended.- Specified by:
setWorkInstanceSuspending
in interfaceWork
-
isSuspending
public boolean isSuspending()
Description copied from interface:Work
Checks 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:
isSuspending
in interfaceWork
-
suspended
public void suspended()
Description copied from interface:Work
Must be called byWork
implementations 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:Work
CALLED BY THE WORK MANAGER (not user code) to check if this work instance really suspended.- Specified by:
isWorkInstanceSuspended
in interfaceWork
-
setWorkInstanceState
public void setWorkInstanceState(Work.State state)
Description copied from interface:Work
CALLED BY THE WORK MANAGER (not user code) to set this work instance's state.- Specified by:
setWorkInstanceState
in interfaceWork
-
getWorkInstanceState
public Work.State getWorkInstanceState()
Description copied from interface:Work
CALLED 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:
getWorkInstanceState
in interfaceWork
-
setProgress
public void setProgress(Work.Progress progress)
Description copied from interface:Work
This method should be called periodically by the actual work method when it knows of its progress.- Specified by:
setProgress
in interfaceWork
- Parameters:
progress
- the progress- See Also:
Progress(float)
,Progress(long, long)
-
getProgress
public Work.Progress getProgress()
Description copied from interface:Work
Gets a progress report for this work instance.- Specified by:
getProgress
in interfaceWork
- Returns:
- a progress report, not
null
-
setStatus
public void setStatus(String status)
Sets a human-readable status for this work instance.- Parameters:
status
- the status
-
getStatus
public String getStatus()
Description copied from interface:Work
Gets a human-readable status for this work instance.
-
initSession
@Deprecated public CoreSession initSession()
Deprecated.since 8.1. UseopenSystemSession()
.May be called by implementing classes to open a session on the repository.- Returns:
- the session (also available in
session
field)
-
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
originatingUsername
to open the session.- Since:
- 8.1
-
initSession
@Deprecated public CoreSession initSession(String repositoryName)
Deprecated.since 8.1. UseopenSystemSession()
to open a session on the configured repository name, otherwise useCoreInstance.getCoreSessionSystem(String)
.May be called by implementing classes to open a session on the given repository.- Parameters:
repositoryName
- the repository name- Returns:
- the session (also available in
session
field)
-
closeSession
public void closeSession()
Closes the session that was opened byopenSystemSession()
oropenUserSession()
.- Since:
- 5.8
-
run
public void run()
Description copied from interface:Work
Runs the work instance and does all the transaction management and retry.
-
getSpanFromContext
protected io.opencensus.trace.Span getSpanFromContext(byte[] traceContext)
-
buildWorkFailureEventProps
public Map<String,Serializable> buildWorkFailureEventProps(RuntimeException exception)
Builds failure event properties. Work implementations can override this method to inject more event properties than the default.- Since:
- 10.1
-
workFailed
public void workFailed(RuntimeException exception)
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:Work
This method should implement the actual work done by theWork
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 callWork.isSuspending()
, and iftrue
callWork.suspended()
return early with saved state data.Clean up can by implemented by
Work.cleanUp(boolean, Exception)
.- Specified by:
work
in interfaceWork
- See Also:
Work.isSuspending()
,Work.suspended()
,Work.cleanUp(boolean, java.lang.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:
work()
-
cleanUp
public void cleanUp(boolean ok, Exception e)
This method is called afterwork()
is done in a finally block, whether work completed normally or was in error or was interrupted.
-
getOriginatingUsername
public String getOriginatingUsername()
Description copied from interface:Work
Gets the user on behalf of which this work is done.- Specified by:
getOriginatingUsername
in interfaceWork
- Returns:
- the originating username, or
null
-
getSchedulingTime
public long getSchedulingTime()
Description copied from interface:Work
Gets the time at which this work instance was first scheduled.- Specified by:
getSchedulingTime
in interfaceWork
- Returns:
- the scheduling time (milliseconds since epoch)
-
getStartTime
public long getStartTime()
Description copied from interface:Work
Gets the time at which this work instance was first started.- Specified by:
getStartTime
in interfaceWork
- Returns:
- the start time (milliseconds since epoch), or
0
if not stated
-
getCompletionTime
public long getCompletionTime()
Description copied from interface:Work
Gets the time at which this work instance was completed, suspended or failed.- Specified by:
getCompletionTime
in interfaceWork
- Returns:
- the completion time (milliseconds since epoch), or
0
if not completed
-
setStartTime
public void setStartTime()
Description copied from interface:Work
CALLED BY THE WORK MANAGER (not user code) to set the start time on the work instance.- Specified by:
setStartTime
in interfaceWork
-
setCompletionTime
protected void setCompletionTime()
-
getCategory
public String getCategory()
Description copied from interface:Work
Gets the category for this work.Used to choose an execution queue.
- Specified by:
getCategory
in interfaceWork
- Returns:
- the category, or
null
for the default
-
getDocument
public DocumentLocation getDocument()
Description copied from interface:Work
Gets the document impacted by the work.Returns
null
if the work isn't about a single document.- Specified by:
getDocument
in interfaceWork
- Returns:
- the document, or
null
. This is always aDocumentLocation
with anIdRef
-
getDocuments
public List<DocumentLocation> getDocuments()
Description copied from interface:Work
Gets the documents impacted by the work.Returns
null
if the work isn't about documents.- Specified by:
getDocuments
in interfaceWork
- Returns:
- the documents, or an empty list. List elements are always a
DocumentLocation
with anIdRef
-
newDocumentLocation
protected DocumentLocation newDocumentLocation(String docId)
-
isDocumentTree
public boolean isDocumentTree()
Description copied from interface:Work
Returnstrue
ifWork.getDocument()
is only the root of a set of documents on which this Work instance will act.- Specified by:
isDocumentTree
in interfaceWork
- Returns:
true
if 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
-
getPartitionKey
public String getPartitionKey()
Description copied from interface:Work
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.- Specified by:
getPartitionKey
in interfaceWork
-
-