如何从标准输入获取演员消息?

发布于 2024-12-04 02:48:30 字数 815 浏览 1 评论 0 原文

我想知道是否有可能(以及如何)让 akka actor 接收来自 stdin 的消息。本质上,这个想法是将每一行输入作为消息发送给参与者,例如

> myprogram
DO X
DO Y
...

,然后让参与者接收消息“DO X”、“DO Y”等。

是否有标准解决方案可以做到这一点?

我想一种方法是这样做:

spawn {
    while(in.available) {
        actor ! in.readLine
    }
}

但是我有两个演员(或者一个基于演员的任务和一个演员),并且我会使用阻塞 IO(顺便问一下,这对于演员来说安全吗?)。 ..此外,它使得控制生成块变得更加困难(例如杀死任务)。

添加了来自 OP 的进一步跟进

如果您允许的话,我有几个跟进...

  1. 使用此解决方案是否会影响性能(即 CamelServiceManager启动很多东西吗?HTTP 服务器等)?

  2. 有适合​​初学者的良好教程吗?我开始从官方 Akka 文档中阅读 Camel ,但似乎假设对 Camel 的了解比我目前拥有的更多。例如,我不知道如何使用自定义 java.io.InputStream 作为 endpointUri

I would like to know if it's possible (and how) to get an akka actor to receive messages from stdin. Essentially, the idea would be for every line of input to be sent as a message to the actor, e.g.

> myprogram
DO X
DO Y
...

and then to have the actor receive messages "DO X", "DO Y", etc.

Is there a standard solution to do this?

I guess one way would be to do this:

spawn {
    while(in.available) {
        actor ! in.readLine
    }
}

But then I'd have two actors (or one actor-based task and one actor) and I'd be using blocking IO (is that safe with actors, by the way?)... Also, it makes it harder to control the spawn block (e.g. to kill the task).

Added further follow ups from OP

I have a couple follow ups, if you will allow me...

  1. Is there a performance hit using this solution (i.e. does CamelServiceManager start a lot of things? HTTP server, etc.)?

  2. Got a good tutorial for beginners? I started reading Camel from the official Akka documentation, but it seems to assume more knowledge of Camel than I currently possess. For instance, I couldn't figure out how to use a custom java.io.InputStream as endpointUri.

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

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

发布评论

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

评论(1

×眷恋的温暖 2024-12-11 02:48:30

您可以将 akka-camelcamel-stream 组件让参与者从标准输入接收消息。这是一个工作示例:

import akka.actor.Actor
import akka.camel.{Message, CamelServiceManager, Consumer}

object Example extends App {
  CamelServiceManager.startCamelService
  Actor.actorOf[ExampleConsumer].start
}

class ExampleConsumer extends Actor with Consumer {
  def endpointUri = "stream:in"
  def receive = {
    case msg: Message => println("received %s" format msg.bodyAs[String])
  }
}

更新: 后续问题的解答

  • CamelServiceManager.startCamelService 方法启动一个 CamelContext 和两个新注册的 Akka Actor在 CamelContext 处启动了 Consumer actor 端点。没有启动 HTTP 服务器。
  • Apache Camel:集成涅槃 文章和 rel="nofollow">骆驼在行动一书。 Camel in Action 的附录 E 是对 akka-camel 的介绍。
  • 目前,camel-stream 组件无法在端点 URI 处设置自定义 InputStream

You could use akka-camel together with the camel-stream component to let actors receive messages from stdin. Here's a working example:

import akka.actor.Actor
import akka.camel.{Message, CamelServiceManager, Consumer}

object Example extends App {
  CamelServiceManager.startCamelService
  Actor.actorOf[ExampleConsumer].start
}

class ExampleConsumer extends Actor with Consumer {
  def endpointUri = "stream:in"
  def receive = {
    case msg: Message => println("received %s" format msg.bodyAs[String])
  }
}

Update: Answers to the follow-up questions

  • The CamelServiceManager.startCamelService method starts a CamelContext and two Akka actors that register newly started Consumer actor endpoints at the CamelContext. No HTTP server is started.
  • Good introductions to Apache Camel are Apache Camel: Integration Nirvana article and chapter 1 of the Camel in Action book. The Appendix E of Camel in Action is an introduction to akka-camel.
  • Setting a custom InputStream at the endpoint URI is currently not possible with the camel-stream component.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文