亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我們如何將 play.libs.concurrent.HttpExecutionContext

我們如何將 play.libs.concurrent.HttpExecutionContext

揚帆大魚 2023-02-23 09:57:24
在一個播放框架項目控制器中,我正在使用 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 嗎?
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

使用HttpExecutionContext.execute


public class HomeController extends Controller {

    @Inject HttpExecutionContext ec;


    public Result index() {

        // The data to parallel processing

        List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");


        // Make a Stream. The `parallelStream` is not used because 

        // `current.execute` will make it run in parallel.

        Stream<String> listInParralel = list.stream(); 


        // The current executor with the HTTP context. 

        Executor current = ec.current();


        System.out.println("START");

        listInParralel.forEach(item -> {

          current.execute(()-> {

            // request().uri() internally uses HttpExecutionContext

            System.out.println("item: " + item + " in "  +  request().uri()  + "(" + Thread.currentThread().getName() + ")");

          });

        });


        // Results

        /*

        START

        item: Item 7 in /(application-akka.actor.default-dispatcher-9)

        item: Item 5 in /(application-akka.actor.default-dispatcher-7)

        item: Item 3 in /(application-akka.actor.default-dispatcher-5)

        item: Item 1 in /(application-akka.actor.default-dispatcher-6)

        item: Item 6 in /(application-akka.actor.default-dispatcher-8)

        item: Item 4 in /(application-akka.actor.default-dispatcher-2)

        item: Item 2 in /(application-akka.actor.default-dispatcher-4)

        item: Item 8 in /(application-akka.actor.default-dispatcher-9)

        */


        return ok("Done");

    }


}

不過,我更喜歡緩存 HTTP 數據,然后在并行處理中使用它們。不喜歡打擾HttpExecutionContext:


public class HomeController extends Controller {

    @Inject HttpExecutionContext ec;


    public Result index() {

        // The data to parallel processing

        List<String> list = List.of("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6", "Item 7", "Item 8");

        Stream<String> listInParralel = list.parallelStream(); 


        // Take all that you need from the HttpExecutionContext.  

        String uri = request().uri();


        System.out.println("START");

        listInParralel.forEach(item -> {

            // use pre cached HTTP context data, liek `uri`

            System.out.println("item: " + item + " in "  +  uri  + "(" + Thread.currentThread().getName() + ")");

        });


        // Results

        /*

        START

        item: Item 1 in /(ForkJoinPool.commonPool-worker-7)

        item: Item 8 in /(ForkJoinPool.commonPool-worker-3)

        item: Item 7 in /(ForkJoinPool.commonPool-worker-15)

        item: Item 4 in /(ForkJoinPool.commonPool-worker-9)

        item: Item 3 in /(ForkJoinPool.commonPool-worker-13)

        item: Item 2 in /(ForkJoinPool.commonPool-worker-5)

        item: Item 5 in /(ForkJoinPool.commonPool-worker-11)

        item: Item 6 in /(application-akka.actor.default-dispatcher-4)

        */


        return ok("Done");

    }



查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號