PlayFramework 2.0.x -> 2.1-RC 迁移

5
在我的Play 2.0.4程序中,我有这段代码:
val channel = Enumerator.imperative[JsValue](onStart = self ! NotifyJoin(username))

现在API已经弃用了imperative,建议使用unicast或者broadcast。我倾向于使用unicast,因为在我的代码中channel是单播的。所以我会这样做:

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

但它不起作用...看起来单播需要其他东西。我无法弄清楚 - API中没有更多信息...有人知道在这里该怎么做吗?

更新:

在Play Framework用户组中开始了一次讨论。结果发现这是开发人员中相当常见的问题,他们对这种范例还不熟悉。希望文档能得到改进。


我认为与1.x.x版本相比,Play框架2.*.*在文档方面做得不够好。 - Ömer Faruk AK
2个回答

3
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$

2
是的,我在网站上看到了这个。太糟糕了,它没有说明我如何获取Channel[E]Enumerator[E],也没有真正解释这个东西是如何工作的。实际上,在我访问API页面时,只有这两个签名。但这并没有比Eclipse默认提供的自动完成建议更有帮助。我只是想表达一下,一些解释是值得的。 - noncom
“broadcast” 返回一个包含 “Enumerator[E]” 和 “Channel[E]” 的元组。这不是您所需要的吗? - James Ward
1
哈哈,你可以随便叫我什么,但我对这种API完全是新手,所以我只想要一个好的文字解释,而不是签名。无论如何,Play框架用户组中已经有一个关于此问题的帖子了...结果发现很多人都是新手。 - noncom
1
我知道你的意思。这方面的代码示例相当缺乏。 :( - James Ward

0

使用Unicast的示例:

// 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)
}

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接