Do the task asynchronously but reply first

2019-08-04 05:25发布

This is my first try to do a asynchronous program with java.

My requirement is that whenever I receive a request I have to start a task which may take an hour. Therefore I have immediately reply to the user that “ the task has started” before the task finishes. I amusing apache timcat version 8.5.23.

I assumed that AsyncContext.start() will help me but what I found that AsyncContext won’t return the response to the client until it calls the onComplete(AsyncEvent event).

Is there a way I can respond to the user before onComplete(AsyncEvent event) happens?

I did see a similar question How to execute code after response in JavaEE .

I heard that https://vertx.io/docs/guide-for-java-devs/ may help me but before diving into deep; I am wondering, is there any other better way or technology to do this?

Thanks in advance.

1条回答
乱世女痞
2楼-- · 2019-08-04 05:58

Instead of using AsyncContext.start(Runnable), I used CompletableFuture.runAsync(Runnable) and it worked as expected.

查看更多
登录 后发表回答