Do the task asynchronously but reply first

扶醉桌前 提交于 2019-12-13 02:35:54

问题


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:


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



来源:https://stackoverflow.com/questions/54217289/do-the-task-asynchronously-but-reply-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!