我已经使用下拉向导创建了一个后端点。下拉列表中的异步技术
@POST
@Timed
public String runPageSpeed(@RequestParam String request) {
try {
JSONObject requestJSON=new JSONObject(request);
JSONArray urls=requestJSON.getJSONArray("urls");
process(urls); // this takes around 10 minutes to complete
return "done";
} catch (Exception e) {
throw new WebApplicationException("failed", Response.Status.INTERNAL_SERVER_ERROR);
}
}
喜 process(urls);
大约需要10分钟即可完成。所以如果我们称之为终点,则需要超过10分钟才能得到答复。 我想要process(urls);
收到request
收到urls
后在后台运行。所以当用户点击这个URL时,他会在几秒钟内得到响应。
我尝试了下面的代码和线程。
@POST
@Timed
public String runPageSpeed(@RequestParam String request) {
try {
JSONObject requestJSON=new JSONObject(request);
JSONArray urls=requestJSON.getJSONArray("urls");
Thread thread = new Thread() {
public void run() {
process(urls); // this takes around 10 minutes to complete
}
};
thread.start();
return "done";
} catch (Exception e) {
throw new WebApplicationException("failed", Response.Status.INTERNAL_SERVER_ERROR);
}
}
这里我用了一个线程。所以它异步运行,用户在几秒钟内得到响应,并且process(urls);
在后台运行。 这解决了我的问题。但这是否正确?如果我使用这种方法,是否有任何问题,因为每分钟可以有很多请求。 dropwizard
有什么技巧可以处理这种异步性质?
请查看https://jersey.github.io/documentation/latest/async.html –