cancel
Showing results for 
Search instead for 
Did you mean: 

Handling Exceptions in TaskListeners

uprooter
Champ in-the-making
Champ in-the-making
Hello guys,

I have been working on a problem for hours now and cannot find a clean way to solve it. Maybe someone here has a good advice for me?

What I want to do is the following:

I have a simple process that looks like this:  start -> UserTask1 -> UserTask2 -> UserTask3 -> end

UserTask2 is activiti:async="true" and has a TaskListener assigned that is triggered by the "create" event. The TaskListener does some EJB logic which could generate exceptions that let the TaskListener fail in its execution.

What I want to do is define a stable exception handling for this TaskListener which should eg "revert" the process back to UserTask1.

Unfortunately I do not know how to do it. Right now the execution fails and the process instance is suspended.
Is there a way to take another sequenceFlow in case of an exception in the TaskListener execution?

Why do I need a TaskListener anyway? Well, I want to do the logic at the very moment of the UserTask2 creation and I need the name of UserTask2 for the TaskListener execution.
4 REPLIES 4

frederikherema1
Star Contributor
Star Contributor
You can always replace the task-create-listener by a service-task right after the previous user-task. This has the same effect, once the previous task is completed, some code is run. If you throw an exception from the service-task (due to EJB-magic you do), the UserTask2 won't be started yet, and the process is rolled back to UserTask1. Also, it doesn't make sense marking user-tasks as being async, as the only thing a user-task does, is create a Task-entry in the DB and stop the process/execution from executing.

Service-task is better suited for executing 'logic'. If you insist on having a task-listener, you can always try-catch the exception and throw a BPMNException instead. This way, you can use a message (boundary) event to go back to user-task1. The advantage of this approach is that the diagram clearly shows that there is a possibility of an functional error.

uprooter
Champ in-the-making
Champ in-the-making
thanks for the quick reply!

The reason I have configured the user task to be async is to let the transaction commit on UserTask1 completion without triggerring the TaskListener of UserTask2 because the TaskListener expects a certain DB state that is updated in the same transaction as UserTask1 completion Smiley Happy

I have thought about the option of using a service task as well but it has 2 disantvantages for my use case:

1. Since it is a generic code in the TaskListener I need the name of the task that the listener is attached to. The service task would have a different name then
2. I need the candidate groups defined in the UserTask2 which I can get from the DelegateTask without problems

uprooter
Champ in-the-making
Champ in-the-making
Does activiti support a message boundary event on a user task? I cant figure out how to get it working. It always says

org.activiti.engine.ActivitiException: Execution with id '3305' does not have a subscription to a message event with name 'newInvoice'

jbarrez
Star Contributor
Star Contributor
Sounds strange, cause a task has an execution pointing to that specific task. Is is simply a receive message on the bondary of a user task?