Julien D
Julien D

Reputation: 1267

Play framework scala POST and Future

I have this action in my controller

def doRegister = Action { implicit request =>
    Future {
      Thread.sleep(5000)
    }
    Ok("")
  }

This is the route

POST /api/checkout/register controllers.shop.checkout.CheckoutAuthController.doRegister

I want to return the Ok result, without waiting for the Future.

It works with a GET requests (returns instantly), but not with POST. The timeout applies and the javascript vuejs project that make the call, has to wait.

Upvotes: 0

Views: 85

Answers (1)

Mario Galic
Mario Galic

Reputation: 48430

As per Mateusz's advice, offload blocking calls to a separate thread pool, for example

val ecForBlockingTasks = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(5))

def doRegister = Action { implicit request =>
  Future {
    Thread.sleep(5000)
  }(ecForBlockingTasks)

  Ok("")
}

Make sure you create the thread pool only once at app launch, otherwise you might end up with a resource leak.

Upvotes: 2

Related Questions