Play Framework 2.5 JavaAsync引发CompletionException [英] Play Framework 2.5 JavaAsync throwing CompletionException

查看:78
本文介绍了Play Framework 2.5 JavaAsync引发CompletionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play 2.5来构建一个简单的应用程序.为了获得更好的性能,我在Java 8 CompletionStage策略中使用了Akka分块响应.下面是生成分块响应的代码(不使用ComperableFuture时工作正常):

I'm using Play 2.5 to build a simple app. For better performance I'm using Akka chunked response with Java 8 CompletionStage strategy. Below is the code by which chunked response is getting generated(it's working fine when not using 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;
                });

    }

}

这是我的控制器:

And here is my controller:

@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));

    }

}

现在,当我运行应用程序时,它会引发以下异常:

Now when I'm running the app it's throwing following exception:

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上下文,所以为什么我没有得到为什么.当返回带有分块响应的正常Result时,相同的代码正在工作.请为此提供帮助

I'm not using HTTP context anywhere, so why this is not working I'm not getting. Same code is working when returning normal Result with chunked response. Please help with this

推荐答案

在处理CompletableFuture/CompletionStage时,您必须提供HTTP执行上下文.在Scala中,上下文信息是通过隐式传递的,这些隐式在Java中不可用-这就是Play使用ThreadLocal的原因.

You have to supply the HTTP execution context when dealing with CompletableFuture / CompletionStage. In Scala the context information is passed via implicits, these are not available in Java - this is why Play uses ThreadLocal.

但是,在切换线程时,您可能会丢失此信息,这就是您遇到问题的原因.您可能认为您没有访问HTTP上下文,但实际上您在访问-使用的是request().

However you can lose this information when switching threads and that is why you have the problem. You may think that you don't access the HTTP context but actually you do - you are using request().

因此,您必须更改代码以将supplyAsync与执行程序一起使用:

So you have to change your code to use supplyAsync with an Executor:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#supplyAsync-java.util.function.Supplier-java .util.concurrent.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;

这篇关于Play Framework 2.5 JavaAsync引发CompletionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆