I have a piece of Java code where I have the RuntimeService
(RuntimeService runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();)
a process_instance_id and a task_instance_id (e.g. from a UserTask).
Is there a way I can get/access the DelegateExecution of that Task (so I can call a class from there)?
(Having an Execution Listener doesn’t help me because I don’t want this code to be executed either at start or end of the task, but at any point).
I have a Java class that handles the receiving of websocket messages.
In that messages I have information like “process_instance_id”, “task_instance_id” and in that class I can access the RuntimeService (and subsequently the Execution).
From that class, once I receive a message I want to trigger an event on a task (e.g. UserTask). So I want to access the DelegateExecution to invoice some TaskListeners/ExecutionListeners.
Your websocket handler class detects an error condition
Your class completes the task via TaskService#completeTask providing a process variable raiseError
You have an execution listener on the activity’s end event that throws BpmnError whenever it sees the raiseError variable
The process engine triggers the error boundary event
That way, the process engine calls the execution listener triggered via task completion. There is no way to tell the process engine to invoke a listener outside of process instance execution context, so what you ask for in the original post is not possible.
All that said, I believe the proposed solution does not work due to the following missing feature: https://app.camunda.com/jira/browse/CAM-5399. You could work around that by introducing a script or service task after the user task and throwing and catching the BpmnError there as follows:
I would like to avoid your idea, which can be similarly be implemented with a X-OR gateway after the user task.
But I have to find a workaround indeed.
I’m thinking of trying to have a call activity which will call the User Task and then attach the ErrorEvent on the Call activity once the called User Task is completed. But I have some issues to pass a variable from the inner called process to the parent Call activity.
Well, I think I figured out how are the variables passed.
But the issue is the following:
In the called subprocess I update a process variable to catch the error later and I complete the User Task with the REST API. The process variable is checked in the Execution Listener (defined as “end event”) in the parent Call Activity.
However the ExecutionListener is executed before the inner called subprocess is actually completed.
How is that possible? I thought the ExecutionListener will be executed after the inner has been completed.
Or is it executed everytime I interact with the execution?