[英]Play 2.2 action not processed in parallel with web service response handler
public class Application extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result action1() {
WS.url(WS_URL).get().map(new Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
});
return ok();
}
@BodyParser.Of(BodyParser.Json.class)
public static Result action2() {
return ok();
}
}
The client first calls action1()
, then, it calls action2()
. 客户端首先调用
action1()
,然后调用action2()
。 However, it seems that Play process the second request after those 10 seconds. 但是,似乎Play在那10秒之后处理了第二个请求。 I thought that responses from web services are processed by Play in separate threads, but it seems that's not true.
我认为来自Web服务的响应是由Play在单独的线程中处理的,但事实并非如此。 In this case, what should I do if I want to sleep or to execute some code after a timeout, without interrupting the server from serving other requests?
在这种情况下,如果我想休眠或在超时后执行一些代码而不中断服务器处理其他请求,该怎么办?
EDIT: this happens when the response from the WS arrives before the client calls action2(). 编辑:当来自WS的响应在客户端调用action2()之前到达时,就会发生这种情况。
默认情况下,Play分配的线程数与您拥有的CPU内核数相同(尽管请注意,有多个线程池),因此,如果您要执行阻塞操作(例如在处理程序中进行sleep
),请将其配置为分配更多的线程。
Not really sure what you're trying to achieve, but the code below will insert a 10 second delay between when the WS call returns, and when it returns an ok result: 不太确定您要实现的目标,但是下面的代码将在WS调用返回与返回正常结果之间插入10秒的延迟:
public static F.Promise<SimpleResult> action1() {
return WS.url(WS_URL).get().flatMap(new Function<WS.Response, F.Promise<WS.Response>>() {
public F.Promise<WS.Response> apply(WS.Response response) {
return F.Promise.timeout(response, 100000);
}
}).map(new Function<WS.Response, SimpleResult>() {
public SimpleResult apply(WS.Response response) {
return ok();
}
};
}
To understand Play's thread pools, read this: 要了解Play的线程池,请阅读以下内容:
http://www.playframework.com/documentation/2.2.x/ThreadPools http://www.playframework.com/documentation/2.2.x/ThreadPools
To understand how Play works with promises, read this: 要了解Play如何实现承诺,请阅读以下内容:
http://www.playframework.com/documentation/2.2.x/JavaAsync http://www.playframework.com/documentation/2.2.x/JavaAsync
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.