ActorPublisher作为Play应用程序中的Comet事件源

时间:2016-06-08 10:30:35

标签: scala playframework akka comet

我正在尝试编写一个连接到Amazon Kinesis流的Actor,然后将通过Comet接收的任何消息中继到Web UI。我正在使用Source.actorPublisher,并使用Comet in Play中的json方法描述Official Docs。我使用Source.tick()使事件正常工作,但是当我尝试使用ActorPublisher时,似乎似乎没有按预期发送任何Request个消息。如何通过Akka流程发送数据请求?我正在使用Play Framework的v2.5。

我的控制器代码:

def subDeviceSeen(id: Id): Action[AnyContent] = Action {
  val deviceSeenSource: Source[DeviceSeenMessage, ActorRef] = Source.actorPublisher(DeviceSeenEventActor.props)
  Ok.chunked(deviceSeenSource
    .filter(m => m.id == id)
      .map(Json.toJson(_))
        via Comet.json("parent.deviceSeen")).as(ContentTypes.JSON)
}

我在上面做了什么明显错误的事吗?这是我的演员代码:

object DeviceSeenEventActor {
  def props: Props = Props[DeviceSeenEventActor]
}

class DeviceSeenEventActor extends ActorPublisher[DeviceSeenMessage] {

implicit val mat = ActorMaterializer()(context)

val log = Logging(context.system, this)

def receive: Receive = {
  case Request => log.debug("Received request message")
                  initKinesis()
                  context.become(run)
  case Cancel => context.stop(self)
}

def run: Receive = {
  case vsm:DeviceSeenMessage => onNext(vsm)
                                 log.debug("Received request message")
                                 onCompleteThenStop() //we are currently only interested in one message
  case _:Any                  => log.warning("Unknown message received by event Actor")
}

private def initKinesis() = {
  //init kinesis, a worker is created and given a reference to this Actor.
  //The reference is used to send messages to the actor.
}

}

永远不会显示“已接收的请求消息”日志行。我错过了一些隐含的吗?播放控制台中没有显示任何警告或任何其他明显的信息。

1 个答案:

答案 0 :(得分:0)

问题是我在case Request => ...而不是case Request() => ...上进行了模式匹配。由于我的receive()方法中没有默认大小写,因此该消息只是由Actor删除。