Interrupt Delegate Service Task

I have a java delegate service task in my BPMN workflow that has the potential to be long running. If I want to terminate the workflow while the delegate (either by Class or DelegateExpression), the workflow fails termination since it’s still stuck in the delegate. This scenario is easily simulated putting a Thread.sleep(1000) in the delegate and calling terminate. It doesn’t seem to throw an error, it just hangs until the delegate is completed or throws an OptimisticLockingException and rolls back to the last safe state and negates the termination.

I can query the runtimeService.createActivityInstanceQuery().activityType(“serviceTask”).list(), but those don’t appear until after they’ve been completed in the history service. Is there a service that oversees running delegates and has the ability to close them prematurely?

As far as I can see this problem could be a little tricky. Basically you want Flowable to manage some arbitrary Java code running in a thread and this may not straight forward to do, even with plain old Java code, as the thread needs to be interruptible. In general, how easily and cleanly this could be done will depend on what the delegate is doing when you are trying to terminate it. If the delegate is executing some CPU bound code, such as a loop, it may be difficult or impossible to interrupt it. If the delegate is spending most of its times waiting on a interruptible call, such as acquiring a mutex, then it may be easier.

That said, it looks like your simulation case of Thread.sleep(1000) should be interruptible. Rather than terminating the workflow, you could experiment with boundary events to see if that works. If you are concerned with the delegate running for too long, maybe attaching timer boundary around the activity and exiting the workflow may be a cleaner solution. It’s also possible to set up other types of boundary events to control an activity.

it’s jvm’s business,not flowable

Thanks! It was easier to simulate with the Thread.sleep, but the likely offender is actually a kafka subscriber. It’s not necessarily a matter of timeouts, but being able to stop all parts of the workflow when a user has requested it.

You could try surrounding all or part of your workflow with a message boundary event as documented in: BPMN 2.0 Constructs · Flowable Open Source Documentation. A message can be sent programmatically from the runtime service using the messageEventReceived method to target a specific workflow.

I wrap all activities in subprocess + boundary event, but the delegate persists until it is completed. There’s no way to find a list of running delegates and stop them before terminating the workflow. They don’t appear to live on the runtimeService.