简体   繁体   English

Akka消息和actor的命名约定

[英]Naming conventions for Akka messages and actors

I'm currently in the process of making a pretty large Akka based Java application and I'm running into a couple issues that bug me to no end. 我目前正在制作一个非常大的基于Akka的Java应用程序,而且我遇到了一些让我感到困惑的问题。

My current package layout looks kinda like this: 我目前的包布局看起来像这样:

项目结构

My Mobile class serving as the supervisor of the actors inside the actors package. 我的Mobile类充当actors包内actors的主管。

Since I don't want to create a new set of Actors for every HttpClient and Account , I pass those around in message objects, which are stored in the messages package, together with the endpoint ActorRef that receives the final result. 因为我不想为每个HttpClientAccount创建一组新的Actors,所以我将它们传递给消息包中存储的消息对象,以及接收最终结果的端点ActorRef This does however create a very cluttered messages package with a different message for each actor. 但是,这会创建一个非常混乱的messages包,每个actor都有不同的消息。 Eg. 例如。 MobileForActor1 , Actor1ForMobile , MobileForActor2 etc. Now my question is, is there a Convention to use for this sort of stuff that deals with this problem and is my structure ( Mobile -> Actor1 -> Mobile -> Actor2 ->etc.) the way Akka wants it to be or do I have to just sort of waterfall the messages ( Mobile -> Actor1 -> Actor2 ->etc.)? MobileForActor1Actor1ForMobileMobileForActor2等现在我的问题是,是否有一个公约用于处理这个问题的这类东西,并且是我的结构( Mobile - > Actor1 - > Mobile - > Actor2 - >等)的方式Akka希望它是或者我只需要对消息进行排序( Mobile - > Actor1 - > Actor2 - >等)?

Right now I'm sending a ConnectMessage to my Mobile actor which then sends it to Actor1 , Actor1 processes it and sends a new message back to Mobile , Mobile sends that response then to Actor2 and the cycle continues with a new message being created based on the old message. 现在我正在向我的Mobile actor发送一个ConnectMessage ,然后将它发送给Actor1Actor1处理它并将新消息发送回MobileMobile然后将该响应发送给Actor2 ,并且循环继续,基于创建新消息旧消息。 Eg. 例如。 new Message2(message1.foo, message1.bar, message1.baz, newComputatedResult, newComputatedResult2, etc);

Is this good practice or should I include the old instance (which may contain info that isn't useful anymore) and include the new stuff? 这是一个好习惯,还是应该包含旧实例(可能包含不再有用的信息)并包含新内容? Eg. 例如。 new Message2(message1, newComputatedResult, newComputatedResult2, etc);

Or should I do something completely different? 或者我应该做一些完全不同的事情?

I thought about using TypedActors but those require the use of a waterfall pattern and I don't know how I would pass on the ActorRef of the listener that wants to receive the final result. 我想过使用TypedActors但是那些需要使用瀑布模式,我不知道如何传递想要接收最终结果的侦听器的ActorRef。

I hope I made myself understandable enough because English is not my maiden languages and that the question is clear to everyone. 我希望自己能够理解,因为英语不是我的母语,而且每个人都清楚这个问题。

I'm a beginning Akka developer and love the idea but since the documentation doesn't cover this very well, I figured this would be the best place to ask. 我是Akka开发人员的开始并且喜欢这个想法,但由于文档没有很好地涵盖这一点,我认为这将是最好的问题。 Thanks for reading! 谢谢阅读!

I will venture a few comments in response to this because I've dealt with the same issues in my learning curve of Akka. 为了回应这个问题,我会冒一些评论,因为我在Akka的学习曲线中处理了同样的问题。 I think you're asking for some rules of thumb so mine are contained herein. 我认为你要求一些经验法则,因此我的内容包含在内。

First, creating actors is incredibly cheap; 首先,创造演员非常便宜; they are very lightweight. 它们非常轻巧。 So, why not create one for each HttpClient and Account and give them suitable names derived from their identity? 那么,为什么不为每个HttpClient和Account创建一个,并为它们提供从其身份派生的合适名称? This also avoids you having to pass them around as much, probably, decluttering your code. 这也避免了你必须尽可能多地传递它们,可能会使你的代码整齐。

Second, keep your message names short, focused and starting with a verb. 其次,保持消息名称简短,重点突出并以动词开头。 Each message should tell the actor to do something so you want the name to reflect that by using a verb. 每条消息都应告诉演员做某事,以便你希望名字通过动词反映出来。

Third, sets of messages go with the actor. 第三,消息集与演员一起。 I usually declare them in the actor class's companion object so that using them is like ActorClass.MessageName unless it is within ActorClass and then it is just MessageName . 我通常在actor类的伴随对象中声明它们,因此使用它们就像ActorClass.MessageName除非它在ActorClass ,然后它只是MessageName

Fourth, append a counter to the name of an actor. 第四,在演员的名字附加一个计数器。 I often just combine a counter (use AtomicInteger ) with the name of the type ( Car-1 , Car-2 , etc.). 我经常只将一个计数器(使用AtomicInteger )与该类型的名称( Car-1Car-2等)组合在一起。

If the hierarchy is important to you, I would recommend only appending the parent actor to the name. 如果层次结构对您很重要,我建议只将父actor附加到名称。 Something like Phone-1-in-Car-7 meaning Phone-1 is contained within Car-7 . Phone-1-in-Car-7意味着Phone-1包含在Car-7 You can then assemble the hierarchy both programmatically and manually by following the parent links. 然后,您可以通过遵循父链接以编程方式和手动方式组合层次结构。

I think "Message" in ConnectMessage is redundant. 我认为ConnectMessage “消息”是多余的。 Just make the message name be "Connect" or even better "ConnectToThing" (whatever Thing is, if that's relevant). 只需将消息名称设置为“Connect”或甚至更好的“ConnectToThing”(无论是什么,如果相关的话)。

I wouldn't compound your message names too much like you're suggesting with Message2 . 我不会像你在Message2建议的那样复杂化你的消息名称。 Use the minimal amount of information to be useful to whomever is going to read those names. 使用最少量的信息对于读取这些名称的任何人都有用。 I think the lack of response to this may have resulted from this part of your question. 我认为这部分问题可能导致对此缺乏回应。 I found it confusing as a lot of detail is missing. 我发现它很混乱,因为很多细节都没有了。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM