如何在Play框架关闭期间等待演员停止播放? [英] How do I wait for an actor to stop during Play Framework shutdown?

查看:86
本文介绍了如何在Play框架关闭期间等待演员停止播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我关闭播放服务器时,以下代码将引发 java.lang.IllegalMonitorStateException 异常;这是由 onStop 方法中的 a.wait(1000)调用引起的.谁能告诉我为什么会这样,以及如何在Play框架关闭期间优雅地等待演员完成?

The below code throws a java.lang.IllegalMonitorStateException exception when I shutdown the play server; it is caused by the a.wait(1000) call in the onStop method. Could anyone tell me why this is the case and how to gracefully wait for an actor to complete within Play framework shutdown?

import play.api.GlobalSettings
import play.api.libs.concurrent.Akka
import akka.actor.{ Actor, Props }
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
import models.{ TestActor, StartMessage, StopMessage }

object Global extends GlobalSettings {

    override def onStart(application : play.api.Application) {
        val a = Akka.system.actorOf(Props[TestActor], name = "test-actor")
        a ! StartMessage("Start instruction")

    }

    override def onStop(application : play.api.Application) {
        val a = Akka.system.actorSelection("akka://application/user/test-actor")
        a ! StopMessage("Stop instruction")
        a.wait(1000)
        Akka.system.shutdown()
    }
}

更新:

这是完整的解决方案,采用以下答案:

Here is the complete solution, taking the below answer:

import play.api.GlobalSettings
import play.api.libs.concurrent.Akka
import akka.actor.{ Actor, Props }
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.Play.current
import models.{ TestActor, StartMessage, StopMessage }
import akka.pattern.gracefulStop
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.Await
import com.typesafe.config.impl.ResolveContext
import com.typesafe.config.impl.ResolveContext
import akka.actor.ActorIdentity

object Global extends GlobalSettings {

    override def onStart(application : play.api.Application) {
        val a = Akka.system.actorOf(Props[TestActor], name = "test-actor")
        a ! StartMessage("Start Instruction")
    }

    override def onStop(application : play.api.Application) {
        val a = Akka.system.actorFor("akka://application/user/test-actor")
        a ! StopMessage("Stop Instruction")

        try {
            val stopped : Future[Boolean] = gracefulStop(a, scala.concurrent.duration.Duration(5, "seconds"))
            Await.result(stopped, scala.concurrent.duration.Duration(6, "seconds"))
            // the actor has been stopped
        }
        catch {
            case e : akka.pattern.AskTimeoutException => // the actor wasn't stopped within 5 seconds
        }

        Akka.system.shutdown()
        Akka.system.awaitTermination()
    }
}

推荐答案

优雅停止 如果您需要等待多个actor的终止或编写有序的终止,则gracefulStop很有用:

"Graceful Stop gracefulStop is useful if you need to wait for termination or compose ordered termination of several actors:

import akka.pattern.gracefulStop
import akka.dispatch.Await
import akka.actor.ActorTimeoutException

try {
  val stopped: Future[Boolean] = gracefulStop(actorRef, 5 seconds)(system)
  Await.result(stopped, 6 seconds)
  // the actor has been stopped
} catch {
  case e: ActorTimeoutException ⇒ // the actor wasn't stopped within 5 seconds
}

"

PS.谷歌搜索优美的演员阿卡"会给出最高的答案.

PS. Googling for "graceful actor akka" gives the answer as the top result.

这篇关于如何在Play框架关闭期间等待演员停止播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆