Skip to main content
Dec 8, 2016 5:51 PM UTC
Solved

How to make workflow sleep

Wondering if there is a way to make logic within a workflow implementation sleep.

My use case is I have a workflow which executes logic only if the payload is not in use by other workflows, if it is in use the program

will sleep then re-check if it's in use and continue on with logic.

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
WillMc1Accepted solution
Adobe Employee
Dec 9, 2016 2:09 PM UTC

Hello -

As Jorg mentioned it is bad practice to have any Thread.sleep() calls in your workflow processes - this will cause workflow threads to be stalled, and if many of these thread.sleep() calls occur simultaneously you could be effectively stopping workflow processing completely.

The correct approach here is to use an ExternalProcessStep [0] to wait until the correct time to execute your process step.  You can either execute your code in the execute method if you do not need to wait (return null) or execute your code in the  "hasFinished" or "handleResult" methods if you must wait for other workflows to finish.

Also, I wouldn't worry about having multiple workflows running for the same payload - this is typically fine - what you want to avoid (and we have hooks for in the system as Jorg mentioned) is to have the same workflow model running for the same payload.  In this case it seems it's different workflow models which are running.

Hope this helps,

Will

[0] https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/com/adobe/granite/workflow/exec/WorkflowExternalProcess.html

9 replies

smacdonald2008
Level 10
Dec 8, 2016 6:42 PM UTC

That could easily be done using a custom step and Java logic. You could even write a dialog for the step that would allow an author to set the time. Here is an article that shows how to write a custom step with a dialog.

http://scottsdigitalcommunity.blogspot.ca/2015/04/creating-aem-custom-worflow-step-with.html

Hope this helps...

Translate
Dec 8, 2016 7:10 PM UTC

Hi thanks for an answer but I'd like to keep it within the same step and java logic if possible. Rather than adding in an extra step. My model contains one step anyways.

I tried the following but didn't work:

Thread.currentThread(); Thread.sleep(1000l);

Also note this is within an Workflow process step

Translate
smacdonald2008
Level 10
Dec 8, 2016 8:10 PM UTC

I am not clear - you want part of the workflow to have a delay - but not between steps?

Translate
Dec 8, 2016 8:29 PM UTC

Yes exactly that

Translate
smacdonald2008
Level 10
Dec 8, 2016 8:32 PM UTC

Is your current 1 step model a custom step that uses Java? 

Translate
Dec 8, 2016 8:42 PM UTC

Yes it is, I tried Thread.sleep(1000l) and did not work.

Translate
joerghoh
Adobe Employee
Adobe Employee
Dec 8, 2016 9:47 PM UTC

Hi,

If you use a programatic sleep in a workflow step, you block the thread of the workflow engine for this time. So if this situation happens often, you will limit your overall workflow performance.

On the other hand, a payload (a node) should never be part of multiple concurrently running workflows. While it is not really enforced on lowlevel, there are highlevel mechanics which should prevent that. So is this just a proactive activity or do you have real problems with a node being the payload of concurrent workflow instances?

Jörg

Translate
WillMc1Adobe EmployeeAccepted solution
Adobe Employee
Dec 9, 2016 2:09 PM UTC

Hello -

As Jorg mentioned it is bad practice to have any Thread.sleep() calls in your workflow processes - this will cause workflow threads to be stalled, and if many of these thread.sleep() calls occur simultaneously you could be effectively stopping workflow processing completely.

The correct approach here is to use an ExternalProcessStep [0] to wait until the correct time to execute your process step.  You can either execute your code in the execute method if you do not need to wait (return null) or execute your code in the  "hasFinished" or "handleResult" methods if you must wait for other workflows to finish.

Also, I wouldn't worry about having multiple workflows running for the same payload - this is typically fine - what you want to avoid (and we have hooks for in the system as Jorg mentioned) is to have the same workflow model running for the same payload.  In this case it seems it's different workflow models which are running.

Hope this helps,

Will

[0] https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/com/adobe/granite/workflow/exec/WorkflowExternalProcess.html

Translate
Level 3
Jan 3, 2017 10:31 AM UTC

One simple way is to assign the workflow step to admin so that the user won't see it in his / her inbox with an appropriate step name such as "Waiting for something" and programmatically completing the step using Workflow API once the dependent process is completed.

Please revert if my response didn't help or mark answered if it helped!

Translate