【问题标题】:In Play 2.1, What to use instead of deprecated PushEnumerator在 Play 2.1 中,使用什么来代替已弃用的 PushEnumerator
【发布时间】:2013-01-29 17:05:14
【问题描述】:

PushEnumerator 在 Play 框架 2.1-RC 中被弃用。 文档告诉我改用 Concurrent.broadcast 。但是,我推送的数据取决于用户,所以我不能向每个用户广播相同的数据。

换句话说,Concurrent.broadcast 将给我一个连接到许多迭代器的枚举器,而我需要许多连接到许多迭代器的枚举器。

【问题讨论】:

  • 我刚刚意识到我可以为每个用户调用 Concurrent.broadcast,但也许有更标准的方法?
  • 有 Concurrent.unicast[E] 但它需要一个我不明白为什么以及如何传递它的 Channel 参数。
  • @AhmedSoliman,Channel 不需要传入,其实是 Concurrent.unicast 提供给你的。请参阅下面的示例。

标签: scala playframework-2.1


【解决方案1】:

这是一个使用 Concurrent.unicast[E] 的简单示例:

// assume the following exist:
def readValueAsync(source: MySource): Future[Any]
val source: MySource = ...

// this is where the meat is:
val valueEnumerator = Concurrent.unicast[Any] {
  (channel: Concurrent.Channel[Any]) =>
    readValueAsync(source) onComplete {
      case Success(x: Any) => channel.push(x)
      case Failure(t) => channel.end(t)
    }
}

// you can then collect it using an iteratee
// since my enumerator never really ends, I only take 10 elements here
val result: List[Any] = 
  valueEnumerator through Enumeratee.take(10) run Interatee.getChunks[Any]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多