Expand my Community achievements bar.

SOLVED

How to make workflow sleep

Avatar

Former Community Member

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

1 Accepted Solution

Avatar

Correct answer by
Employee

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/WorkflowE...

View solution in original post

9 Replies

Avatar

Level 10

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...

Avatar

Former Community Member

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

Avatar

Level 10

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

Avatar

Level 10

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

Avatar

Former Community Member

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

Avatar

Employee Advisor

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

Avatar

Correct answer by
Employee

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/WorkflowE...

Avatar

Level 4

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!