Play framework async processing and blocking I/O in Java

前端 未结 1 1505
甜味超标
甜味超标 2020-12-29 10:45

My application uses Play framework to process REST requests. I need to perform some potentially long lasting blocking I/O operation in http request handler. In parallel I\'d

相关标签:
1条回答
  • 2020-12-29 11:06

    I would recommend that you set up your own context and run your blocking/cpu-intensive operations there using Plays F.Promise<A>. As always with threads, the optimal solution depends on numerous of things like number of cores etc.

    First set up your context in applications.conf:

    play {
      akka {
        akka.loggers = ["akka.event.Logging$DefaultLogger", "akka.event.slf4j.Slf4jLogger"]
        loglevel = WARNING
        actor {
          default-dispatcher = {
            fork-join-executor {
              parallelism-min = 1
              parallelism-factor = 2
              parallelism-max = 6
            }
          }
          my-context {
            fork-join-executor {
              parallelism-min = 1
              parallelism-factor = 4
              parallelism-max = 16
            }
          }
        }
      }
    }
    

    Then in your controller, make use of your context using Plays Promises (I'm using Java 8):

    public static F.Promise<Result> love() {
        ExecutionContext myExecutionContext = Akka.system().dispatchers().lookup("play.akka.actor.my-context");
    
        F.Promise<Integer> integerPromise = F.Promise.promise(() ->
                LongRunningProcess.run(10000L)
        , myExecutionContext);
    
        F.Promise<Integer> integerPromise2 = F.Promise.promise(() ->
                LongRunningProcess.run(10000L)
        , myExecutionContext);
    
        return integerPromise.flatMap(i -> integerPromise2.map(x -> ok()));
    }
    

    This way your Play app will still handle short lasting requests on the default-dispatcher execution context and the blocking/cpu-intensive will run in my-context.

    I made a very short example for you demonstrating this, check it out on github.

    0 讨论(0)
提交回复
热议问题