Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Member II

Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

I'm very new to Activiti, What i want is not to insert my java service task in a pool but to passivate its behaviour and only complete such task when an external signal is received and/or a callback is called.

 

ex:

start->service task 1-->Receive Task-->End

 

In the Service Task i'm calling the third party service.

In the third party service once the request received it will send success ack and then it will further process the request, once after the action performed it will send us the callback with the correlation id.

 

In this example i need to wait in the service task till get the callback. 

 

Could you please help me to achieve this functionality with some example.

Thanks in advance.

4 Replies
Highlighted
Established Member

Re: Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

It just an Idea. There can be two ways to do this.

1)  You can use the timer and set the timing equal to approximate timing of your third party call.

On your call back you need to update any flag with some value. Every time when timer ends it goes to your received task and you need to check weather flag is having updated value or not. If it is not with updated value then you need send back the signal to timer again else go for received task.

2)  On your call back you need to update any flag variable and Create Event listener and on VARIABLE_UPDATE event you need to trigger the further flow.

Hope this helps.

Highlighted
Established Member II

Re: Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

Hi Ramesha,

In the Service Task i'm calling the third party service. In the third party service once the request received it will send success ack and then it will further process the request, once after the action performed it will send us the callback with the correlation id.

I think what you're really looking for is to call a service task asynchronously. I'd recommend you to have a look at Activiti user guide on asynchronous continuations here: Activiti User Guide 

Since you are calling a third party service and would like the process instance to wait until the response comes back, you would want to model your BPMN diagram such as follows

'Generate Invoice' would be your third-party call service task. It will be marked async="true". This means when the process instance reaches 'generate invoice' step, the process will wait until the 'generate invoice' completes and continues. This is asynchronous continuations. Without marking async="true", the process won't know that it should wait for 'generate invoice' task to be completed to continue. So step 1 is to define async=true for the service task

Then you would need to implement this service task in a way that it will signal the process instance to continue once 'generate invoice' third party call is done. You will need to call 

TaskActivityBehavior.leave(execution)

once the response is successfully received. I attached a sample of the service task implementation.

That's all you need.

I successfully ran my unit test for this implementation and here's a snippet of the log. You can see that sync job was executed successfully and the process was ended once async job was done.

Hope this helps,

Thanks,

Thong Huynh

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.activiti.designer.test.AsyncProcessTest

Jan 06, 2017 12:38:32 PM java.util.logging.LogManager$RootLogger log
INFO: Executing async job, execution 5

Jan 06, 2017 12:38:33 PM java.util.logging.LogManager$RootLogger log
INFO: Sending notification email for execution id 5
Jan 06, 2017 12:38:33 PM java.util.logging.LogManager$RootLogger log
INFO: Invoice id = invoiceId_5
Jan 06, 2017 12:38:43 PM java.util.logging.LogManager$RootLogger log
INFO: Running processes: 0
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 14.955 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.522s
[INFO] Finished at: Fri Jan 06 12:38:43 ICT 2017
[INFO] Final Memory: 10M/167M
[INFO] ------------------------------------------------------------------------

Highlighted
Member II

Re: Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

Hi Thong Huynh ,

If possible please share the complete code (Java classes ) of the asynchronous activiti. My requirement is once an asynchronous service task is created and it is calling another Javaapi then it should send acknowledgement as HTTP 202 , and after completing the task it should give callback. So that it the async service task continue.

Thanks in advance.

Sivaparvathi.

Highlighted
Senior Member II

Re: Activiti Java Service Task custom asynchronous behaviour: Complete task using signal or callback

Hello,

As Thong mentioned above, you can achieve the wait on a service task by extending TaskActivityBehavior and handle the response handling in the signal method.  You can also implement a TriggerableActivityBehavior (Activiti versions 6 and up)/SignallableActivityBehavior (version 5) which is there for this use case.

https://github.com/Activiti/Activiti/blob/6.x/modules/activiti-engine/src/main/java/org/activiti/eng... 
https://github.com/Activiti/Activiti/blob/5.x/modules/activiti-engine/src/main/java/org/activiti/eng... 

Ciju