Search code examples
scalaplayframeworkhookapplication-lifecycle

Wrong type of callable in ApplicationLifecycle hooks in play scala


I am trying to register a stop hook on service in our application (Play Scala).

Based on these docs it's pretty straightforward. https://www.playframework.com/documentation/2.8.x/PluginsToModules#Create-a-Module-class And should be something like this:

@Singleton
class AblyService @Inject() (ablyRealtime: AblyRealtime, lifecycle: ApplicationLifecycle) {

  def getChannel(name: String): Channel = ablyRealtime.channels.get(name)

  lifecycle.addStopHook { () =>
    Future.successful(ablyRealtime.close())
  }
}

However, I am getting a type missmatch error:

[error] /Users/shurikag/ProcessStreet/dev/process-street/app/features/ably/AblyService.scala:15:22: type mismatch;
[error]  found   : scala.concurrent.Future[Unit]
[error]  required: java.util.concurrent.CompletionStage[_]
[error]     Future.successful(ablyRealtime.close())

What is the correct way to do it?


Solution

  • Guess you use the Java API play.inject.ApplicationLifecycle (which expects CompletionStage) and not the Scala one (which accepts a Future).