我想知道是否有可能(以及如何)让 akka actor 接收来自 stdin 的消息。本质上,这个想法是将每一行输入作为消息发送给参与者,例如
> myprogram
DO X
DO Y
...
,然后让参与者接收消息“DO X”、“DO Y”等。
是否有标准解决方案可以做到这一点?
我想一种方法是这样做:
spawn {
while(in.available) {
actor ! in.readLine
}
}
但是我有两个演员(或者一个基于演员的任务和一个演员),并且我会使用阻塞 IO(顺便问一下,这对于演员来说安全吗?)。 ..此外,它使得控制生成块变得更加困难(例如杀死任务)。
添加了来自 OP 的进一步跟进
如果您允许的话,我有几个跟进...
-
使用此解决方案是否会影响性能(即 CamelServiceManager
启动很多东西吗?HTTP 服务器等)?
-
有适合初学者的良好教程吗?我开始从官方 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...
-
Is there a performance hit using this solution (i.e. does CamelServiceManager
start a lot of things? HTTP server, etc.)?
-
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
.
发布评论
评论(1)
您可以将 akka-camel 与 camel-stream 组件让参与者从标准输入接收消息。这是一个工作示例:
更新: 后续问题的解答
CamelServiceManager.startCamelService
方法启动一个CamelContext
和两个新注册的 Akka Actor在CamelContext
处启动了Consumer
actor 端点。没有启动 HTTP 服务器。InputStream
。You could use akka-camel together with the camel-stream component to let actors receive messages from stdin. Here's a working example:
Update: Answers to the follow-up questions
CamelServiceManager.startCamelService
method starts aCamelContext
and two Akka actors that register newly startedConsumer
actor endpoints at theCamelContext
. No HTTP server is started.InputStream
at the endpoint URI is currently not possible with the camel-stream component.