从Play控制器调用Akka演员

时间:2014-06-27 10:55:58

标签: playframework akka akka-cluster

我一直在玩distributed worker pattern,我遇到了一个从网页请求推送工作的问题。

示例项目有一个前端:

val mediator = DistributedPubSubExtension(context.system).mediator

  def receive = {
    case work =>
      log.info("Frontend received: " + work.toString())
      implicit val timeout = Timeout(5.seconds)
      (mediator ? Send("/user/master/active", work, localAffinity = false)) map {
        case Master.Ack(_) => Ok
      } recover { case _ => NotOk } pipeTo sender
  }

和WorkProducer:

override def preStart(): Unit =
    scheduler.scheduleOnce(5.seconds, self, Tick)

def receive = {
    case Tick =>
      n += 1
      log.info("Produced work: {}", n)
      val work = Work(nextWorkId(), n)
      frontend ! work
      context.become(waitAccepted(work), discardOld = false)

  }

当我从Play Framework控制器直接发送到前端时,这一切都运行正常:

def multiply(num: Long) = Action {
    implicit request =>
        implicit val timeout = Timeout(5.seconds)
        val frontend = core.Main.frontend
        frontend ! num
        Ok
  }

这条消息似乎迷失了。前端接收到消息,但似乎下游的演员不会。

我修改了播放配置以使用ClusterActorRefProvider

play {
  akka {
    extensions = ["akka.contrib.pattern.ClusterReceptionistExtension"]    
    actor.provider = "akka.cluster.ClusterActorRefProvider"
    remote.netty.tcp.port=0
        }
}

但无济于事。

1 个答案:

答案 0 :(得分:0)

我一直是一个布偶,并且没有发送正确的消息,控制器应该是这样的:

def multiply(num: Long) = Action {
    implicit request =>
        implicit val timeout = Timeout(5.seconds)
        val frontend = core.Main.frontend
        val work = Work(nextWorkId(), num)
        frontend ! work
        Ok
  }