【问题标题】:PlayFramework 2.0.x -> 2.1-RC migrationPlayFramework 2.0.x -> 2.1-RC 迁移
【发布时间】:2013-01-03 15:01:15
【问题描述】:

在我的 Play 2.0.4 程序中,我有这段代码:

val channel = Enumerator.imperative[JsValue](onStart = self ! NotifyJoin(username))

现在它说imperative 已弃用,API 说我应该改用unicastbroadcast。我倾向于使用unicast,因为在我的代码中channel 是单播的。所以我喜欢

val channel = Concurrent.unicast[JsValue](onStart = self ! NotifyJoin(username))

但它不起作用.. 看起来unicast 想要别的东西。我无法弄清楚 - API 中没有更多信息......有人知道在这里做什么吗?

更新:

在 Play Framework 用户组中发起讨论。事实证明,对于熟悉范式的开发人员来说,这是一个非常普遍的问题。希望文档能够得到改进。

【问题讨论】:

  • 当我们将它与 1.x.x 版本进行比较时,我认为 play framework 2.*.* 不擅长文档。

标签: playframework playframework-2.0 playframework-2.1


【解决方案1】:

Concurrent.unicast 的 API 是:

unicast[E](onStart: (Channel[E]) ⇒ Unit, onComplete: ⇒ Unit, onError: (String, Input[E]) ⇒ Unit): Enumerator[E]

Concurrent.broadcast 的 API 是:

broadcast[E]: (Enumerator[E], Channel[E])

您可以在您的应用中访问 API:

http://localhost:9000/@documentation/api/scala/index.html#play.api.libs.iteratee.Concurrent$

【讨论】:

  • 是的,我在网站上看到了这个。太糟糕了,它没有说明我在哪里得到Channel[E]Enumerator[E],也没有真正解释它是如何工作的。实际上,当我在 API 页面上时,只有这两个签名。但这并不比 Eclipse 在其自动完成建议中默认提供的帮助多。我只是想说明一些解释是值得的。
  • broadcast 返回一个带有 Enumerator[E]Channel[E] 的元组。这不是你需要的吗?
  • 哈哈,随便叫我,但我对这种 API 完全陌生,所以我只想要一个好的文字解释,而不是签名。无论如何,Play Framework 用户组中已经有一个关于这个的线程......原来很多人都是这个领域的新手。
  • 我知道你的意思。这些东西非常缺乏代码示例。 :(
【解决方案2】:

单播使用示例:

// here is an enumerator that returns a chunk to the channel
val outEnumerator = Concurrent.unicast[JsValue] { channel =>
    val data = Json.obj("data" -> 12345)
    channel.push(data)
}

使用旧 Enumerator.imperative 的替代方法是使用 generateM:

val out = Enumerator.generateM[JsValue] {
    Promise.timeout( {
        Some(Json.obj("data" -> 12345))
    }, 100, TimeUnit.MILLISECONDS )
}

在这里,我们使用超时生成一个重复值。这个枚举器永远重复,尽管 generateM 允许您返回 None 以指示何时完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 2019-11-12
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    相关资源
    最近更新 更多