我正在使用Play 2.5构建一个简单的应用程序。为了获得更好的性能,我使用了Akka chunked响应和Java 8 CompletionStage策略。下面是生成分块响应的代码(不使用ComperableFuture时工作正常):
@Singleton
public class AbstractSource {
public Source<ByteString, ?> getChunked(String html) {
return Source.<ByteString>actorRef(256, OverflowStrategy.dropNew())
.mapMaterializedValue(sourceActor -> {
sourceActor.tell(ByteString.fromString(html), null);
sourceActor.tell(new Status.Success(NotUsed.getInstance()), null);
return null;
});
}
}
这是我的控制器:
@Singleton
@AddCSRFToken
public class Application extends Controller {
@Inject
private AbstractSource abstractSource;
public CompletionStage<Result> index() {
CompletionStage<Source<ByteString, ?>> source = CompletableFuture.supplyAsync(() ->
abstractSource.getChunked(index.render(CSRF.getToken(request()).map(t ->
t.value()).orElse("no token")).body()
)
);
return source.thenApply( chunks -> ok().chunked(chunks));
}
}
现在,当我正在运行应用程序时,它会抛出以下异常:
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.RuntimeException: There is no HTTP Context available from here.]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:269)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:195)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:98)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: There is no HTTP Context available from here.
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
at play.mvc.Http$Context.current(Http.java:57)
at play.mvc.Controller.request(Controller.java:36)
at com.mabsisa.ui.web.controllers.Application.lambda$index$1(Application.java:31)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
... 5 common frames omitted
我没有在任何地方使用HTTP上下文,所以为什么这不起作用我没有得到。使用分块响应返回正常结果时,相同的代码正在运行。请帮助解决这个问题
答案 0 :(得分:13)
在处理CompletableFuture
/ CompletionStage
时,您必须提供HTTP执行上下文。在Scala中,上下文信息通过implicits传递,这些在Java中不可用 - 这就是Play使用ThreadLocal
的原因。
但是,切换线程时可能会丢失此信息,这就是您遇到问题的原因。您可能认为您无法访问HTTP上下文,但实际上您正在使用request()
。
因此,您必须更改代码以将supplyAsync
与Executor一起使用:
由此:
CompletableFuture.supplyAsync(() -> abstractSource.getChunked(index.render(CSRF.getToken(request()).map(t ->
t.value()).orElse("no token")).body()
)
);
到此:
CompletableFuture.supplyAsync(() -> abstractSource.getChunked(index.render(CSRF.getToken(request()).map(t ->
t.value()).orElse("no token")).body()
)
, ec.current());
ec
是您的上下文:@Inject HttpExecutionContext ec;
答案 1 :(得分:1)
我除了 Anton 的答案。
如果您使用Play Java API构建非阻止应用程序,每次需要调用HttpExecutionContext
上的方法时,注入ec.current())
并传递CompletionStage
可能会非常麻烦。
为了让生活更轻松,您可以使用装饰器,它将保留调用之间的上下文。
public class ContextPreservingCompletionStage<T> implements CompletionStage<T> {
private HttpExecutionContext context;
private CompletionStage<T> delegate;
public ContextPreservingCompletionStage(CompletionStage<T> delegate,
HttpExecutionContext context) {
this.delegate = delegate;
this.context = context;
}
...
}
所以你只需要传递一次上下文:
return new ContextPreservingCompletionStage<>(someCompletableFuture, context)
.thenCompose(something -> {...});
.thenApply(something -> {...});
而不是
return someCompletableFuture.thenComposeAsync(something -> {...}, context.current())
.thenApplyAsync(something -> {...}, context.current());
如果您正在构建多层应用程序,并在不同类之间传递CompletionStage
,那么这将非常有用。
完整的装饰器实现示例is here。