Scala 中的发布者订阅

发布于 2024-12-12 00:35:21 字数 112 浏览 0 评论 0原文

我是 scala 新手,我正在研究发布者订阅的用法。我的 Google 技能可能有问题,但我似乎找不到这种用法的示例,可以指定订阅者应该观察哪些事件。

有人有一些例子吗?

谢谢!

I'm new to scala, and I'm looking into the Publisher-Subscribe usage. There might be a problem with my Google skills, but I can't seem to find an example of this usage where I can specify which events should be observed by the subscriber.

Anyone got some examples?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-12-19 00:35:21

如果你需要一些非常简单的东西,那么我建议你自己做,它应该不会那么难。但如果您想要更大的东西,那么您可以查看 Eventbus。它是 Java 库,但我也在 Scala 中使用它。

Scala 中的示例实现在 http://jim 中描述-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html

If you need something really simple, then I suggest do it your self, it should not be that hard. But if you want something bigger then you can look at Eventbus. It is Java lib, but I am using it with Scala too.

An example implementation in Scala is described in http://jim-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html

你又不是我 2024-12-19 00:35:21

可以在 http://comments.gmane.org 上找到一个简单的参考示例/gmane.comp.lang.scala.user/63002

case class MyEvent(number: Int)

class PrintIt extends Subscriber[MyEvent, Publisher[MyEvent]] {
  override def notify(pub: Publisher[MyEvent], event: MyEvent): Unit = {
    println("got an event: " + event)
  }
}

class RunIt extends Publisher[MyEvent] {
  def pub() = publish(MyEvent(12))
}

object Main extends App {
  val runIt = new RunIt()
  runIt.subscribe(new PrintIt)
  runIt.pub()
}

An simple reference example can be found on http://comments.gmane.org/gmane.comp.lang.scala.user/63002 :

case class MyEvent(number: Int)

class PrintIt extends Subscriber[MyEvent, Publisher[MyEvent]] {
  override def notify(pub: Publisher[MyEvent], event: MyEvent): Unit = {
    println("got an event: " + event)
  }
}

class RunIt extends Publisher[MyEvent] {
  def pub() = publish(MyEvent(12))
}

object Main extends App {
  val runIt = new RunIt()
  runIt.subscribe(new PrintIt)
  runIt.pub()
}
说谎友 2024-12-19 00:35:21

您可以在这里找到一个简单的 EventBus 实现。它还执行异步事件处理和计划事件发布。

https://github.com/hipjim/scala-event-bus

case class Msg(content: String)
val eb = EventBus()
eb.subscribe[Msg] { t =>
  println(t.content)
}

for (i <- 1 to 100)
 eb.post(Msg(i.toString))

You can find a simple EventBus implementation here. It also does async event handling and scheduled event publishing.

https://github.com/hipjim/scala-event-bus

case class Msg(content: String)
val eb = EventBus()
eb.subscribe[Msg] { t =>
  println(t.content)
}

for (i <- 1 to 100)
 eb.post(Msg(i.toString))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文