我们如何将 play.libs.concurrent.HttpExecutionContext

在一个播放框架项目控制器中,我正在使用 forEach() 处理一个对象列表,它工作正常。


List<Post> posts = repository.getPosts();

posts.forEach(post -> {

    //...some processing


    anyFunc(); //<-- internally uses HttpExecutionContext


    //...further processing

});

但是当我尝试使用 parallelStream() 并行处理这些对象列表以提高性能时,我在并行流中丢失了 HttpExecutionContext 实例。


List<Post> posts = repository.getPosts();

posts.parallelStream().forEach(post -> {

    //...some processing


    anyFunc(); //<-- not able to use HttpExecutionContext now


    //...further processing

});

我无法将 HttpExecutionContext 作为参数传递给anyFunc. 有什么方法可以在 parallelStream() 中传递/设置 HttpExecutionContext 吗?


扬帆大鱼
浏览 95回答 1
1回答

白板的微信

使用HttpExecutionContext.executepublic class HomeController extends Controller {&nbsp; &nbsp; @Inject HttpExecutionContext ec;&nbsp; &nbsp; public Result index() {&nbsp; &nbsp; &nbsp; &nbsp; // The data to parallel processing&nbsp; &nbsp; &nbsp; &nbsp; List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");&nbsp; &nbsp; &nbsp; &nbsp; // Make a Stream. The `parallelStream` is not used because&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // `current.execute` will make it run in parallel.&nbsp; &nbsp; &nbsp; &nbsp; Stream<String> listInParralel = list.stream();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // The current executor with the HTTP context.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Executor current = ec.current();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("START");&nbsp; &nbsp; &nbsp; &nbsp; listInParralel.forEach(item -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current.execute(()-> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // request().uri() internally uses HttpExecutionContext&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("item: " + item + " in "&nbsp; +&nbsp; request().uri()&nbsp; + "(" + Thread.currentThread().getName() + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // Results&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; START&nbsp; &nbsp; &nbsp; &nbsp; item: Item 7 in /(application-akka.actor.default-dispatcher-9)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 5 in /(application-akka.actor.default-dispatcher-7)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 3 in /(application-akka.actor.default-dispatcher-5)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 1 in /(application-akka.actor.default-dispatcher-6)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 6 in /(application-akka.actor.default-dispatcher-8)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 4 in /(application-akka.actor.default-dispatcher-2)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 2 in /(application-akka.actor.default-dispatcher-4)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 8 in /(application-akka.actor.default-dispatcher-9)&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; return ok("Done");&nbsp; &nbsp; }}不过,我更喜欢缓存 HTTP 数据,然后在并行处理中使用它们。不喜欢打扰HttpExecutionContext:public class HomeController extends Controller {&nbsp; &nbsp; @Inject HttpExecutionContext ec;&nbsp; &nbsp; public Result index() {&nbsp; &nbsp; &nbsp; &nbsp; // The data to parallel processing&nbsp; &nbsp; &nbsp; &nbsp; List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");&nbsp; &nbsp; &nbsp; &nbsp; Stream<String> listInParralel = list.parallelStream();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Take all that you need from the HttpExecutionContext.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; String uri = request().uri();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("START");&nbsp; &nbsp; &nbsp; &nbsp; listInParralel.forEach(item -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // use pre cached HTTP context data, liek `uri`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("item: " + item + " in "&nbsp; +&nbsp; uri&nbsp; + "(" + Thread.currentThread().getName() + ")");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // Results&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; START&nbsp; &nbsp; &nbsp; &nbsp; item: Item 1 in /(ForkJoinPool.commonPool-worker-7)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 8 in /(ForkJoinPool.commonPool-worker-3)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 7 in /(ForkJoinPool.commonPool-worker-15)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 4 in /(ForkJoinPool.commonPool-worker-9)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 3 in /(ForkJoinPool.commonPool-worker-13)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 2 in /(ForkJoinPool.commonPool-worker-5)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 5 in /(ForkJoinPool.commonPool-worker-11)&nbsp; &nbsp; &nbsp; &nbsp; item: Item 6 in /(application-akka.actor.default-dispatcher-4)&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; return ok("Done");&nbsp; &nbsp; }}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java