# Cancel an async task and move on the next task

**URL:** <https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598>\
**Category:** Flowable Engine\
**Created:** [June 24, 2024, 4:56pm UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598 "2024-06-24T16:56:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DGuha](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DGuha](https://forum.flowable.org/u/DGuha)\
**Post date:** [June 24, 2024, 4:56pm UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/1 "2024-06-24T16:56:53Z")

</div>

Hello All,

We have a flowable model with an async task containing an external api call. When we receive a success response from the external API, the async task will move on to the next task. However, if there is a network failure while calling the API / 500 error from the API, the async task gets retried many times before ending up being a deadletter job.  
Now, the problem is, we need to have a functionality where we can manually (from the java application) close that async task and move on to the next task. Is it possible using any flowable component / rest api call?  
My understanding is that, for sync task - we can do it with taskService.complete(taskID). But for async task, it is not possible. Is there any way to do this?

The solution will help us a lot. 🙂 Thanks in advance.

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [June 25, 2024, 4:08am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/2 "2024-06-25T04:08:33Z")

</div>

Hey @DGuha,

You can use a boundary error event to handle unsuccessful REST API calls like that. I suppose you are using an async HTTP Task. If not, can you perhaps be a bit more specific of how your async task looks like?

Cheers,  
Filip

---

<div class="post-metadata">

**Author:** ![DGuha](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DGuha](https://forum.flowable.org/u/DGuha)\
**Post date:** [June 25, 2024, 5:47am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/3 "2024-06-25T05:47:31Z")

</div>

Hello @filiphr, Thanks for the quick response. Our async task is a service task where java code handles the external api call (throwing all errors so that the async task will fail and retry). We want all network errors to be retried as usual except one specific error (gateway-timeout-error). When this error happens, we want to task to both retry and also wait for a manual continuation (whichever happens early). My understanding is if we use a boundary error event, the process would be terminated without going to the next task.  
We have made a change like this for handling such scenario. Could you please validate if there is any better way of doing it?

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/7/790e56ac0af48e52fe77cf9e52882c543910763e.png)

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [June 25, 2024, 6:42am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/4 "2024-06-25T06:42:27Z")

</div>

Thanks for the explanation @DGuha.

What you can do is to throw a `BpmnError` with the code `gateway-timeout-error` when your specific error happens. This would make it possible for you to use a boundary error.

> [@DGuha](#):
>
> My understanding is if we use a boundary error event, the process would be terminated without going to the next task.

It all depends on what you do after your boundary error. e.g. you can do something like:

![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/6/6935ead4a9c9bc9734722610ea03f00f32308740.png)

Alternatively, you can also just catch that gateway timeout and do not throw an exception for it. This way the execution will just continue through the regular path. It all depends on what you want to do business wise.

Cheers,  
Filip

---

<div class="post-metadata">

**Author:** ![DGuha](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DGuha](https://forum.flowable.org/u/DGuha)\
**Post date:** [June 25, 2024, 7:33am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/5 "2024-06-25T07:33:15Z")

</div>

Thanks again. I genuinely appreciate the help.  
I have implemented it as you specified.  
Is it possible to stop the timer job execution when manual continuation message is received and the process continues to the next task?  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/b/bc46fffda16d196d2a2a4e4d68a63b83675ad32a.png)

Please check this realtime diagram where the process is moved on to the next task, but the timer job is still pending.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/7/7a88b1c98ad56139e9342f3efe6111cc94722b07.png)

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [June 25, 2024, 7:51am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/6 "2024-06-25T07:51:27Z")

</div>

> [@DGuha](#):
>
> Is it possible to stop the timer job execution when manual continuation message is received and the process continues to the next task?

I believe what you are looking for is [Event based gateway](https://www.flowable.com/open-source/docs/bpmn/ch07b-BPMN-Constructs#event-based-gateway).

![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/f/f179c965fa70e269f14dd64e45ae4d8ca9eb52b4.png)

Using this it will cancel either the timer or the message (depending on which one gets triggered).

Cheers,  
Filip

---

<div class="post-metadata">

**Author:** ![DGuha](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@DGuha](https://forum.flowable.org/u/DGuha)\
**Post date:** [June 25, 2024, 8:41am UTC](https://forum.flowable.org/t/cancel-an-async-task-and-move-on-the-next-task/11598/7 "2024-06-25T08:41:25Z")

</div>

You are a lifesaver. 🙂  
My final business process:  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/2/2504f60ff7f30a7d90a46725eb6d97ddc2299973.png)
