ste*_*vew 13 scala long-polling akka playframework playframework-2.0
我正在分布式环境中在Play 2.0中实现长轮询.我理解的方式是,当Play获取请求时,它应该暂停更新的待处理通知,然后转到数据库以获取新数据并重复.我开始查看Play 2.0提供的聊天示例,但它是在websocket中.此外,它看起来不像是能够分发.所以我想我会用Akka的赛车.我采用了事件流实现并使用LookupClassification复制了我自己的实现.但是我很难知道我将如何收到消息(或者就此而言,应该是订户而不是ActorRef)?
EventStream实现:https: //github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/event/EventStream.scala
我不确定这是你在寻找什么,但是在彗星时钟样本中有一个非常简单的解决方案,你可以适应使用AKKA演员.它使用无限iframe而不是长轮询.我使用了一个改编版本,用于更复杂的应用程序,在AKKA actor中进行多个DB调用和长时间计算,并且工作正常.
  def enum = Action {
    //get your actor
    val myActorRef = Akka.system.actorOf(Props[TestActor]) 
   //do some query to your DB here. Promise.timeout is to simulate a blocking call
   def getDatabaseItem(id: Int): Promise[String] = { Promise.timeout("test", 10 milliseconds) } 
    //test iterator, you will want something smarter here
    val items1 = 1 to 10 toIterator
    // this is a very simple enumerator that takes ints from an existing iterator (for an http request parameters for instance) and do some computations
    def myEnum(it: Iterator[Int]): Enumerator[String] = Enumerator.fromCallback[String] { () =>
      if (!items1.hasNext)
        Promise.pure[Option[String]](None) //we are done with our computations
      else {
        // get the next int, query the database and compose the promise with a further query to the AKKA actor
        getDatabaseItem(items1.next).flatMap { dbValue =>
          implicit val timeout = new Timeout(10 milliseconds)
          val future = (myActorRef ? dbValue) mapTo manifest[String]
          // here we convert the AKKA actor to the right Promise[Option] output
          future.map(v => Some(v)).asPromise
        }
      }
    }
    // finally we stream the result to the infinite iframe. 
    // console.log is the javascript callback, you will want something more interesting.
    Ok.stream(myEnum(items1) &> Comet(callback = "console.log"))
  }
请注意,这个fromCallback不允许您将枚举器与"andThen"组合在一起,在play2的trunk版本中有一个generateM方法,如果你想使用组合,它可能更合适.
这不是长时间的民意调查,但它运作良好.