Akka Design Principals

后端 未结 1 681
醉酒成梦
醉酒成梦 2021-02-10 17:47

Whilst working on a fairly large Akka application, I have come across a very simple structure when working with normal methods and non-Akka classes but which are actually quite

相关标签:
1条回答
  • 2021-02-10 18:03

    It seems to me like you don't necessarily need N stages to collect the responses. If you know how many responses you're waiting for you can collect them all under a single behavior.

    Also, in any scenario where you are using ask, you can easily replace it with an intermediary Actor to hold this context and pass all messages through tell. That's actually what ask does anyway, but the main differences are that you wouldn't have to deal with specifying timeouts for every ask (well, you should still have a timeout for the overall request) and you can bundle all outstanding stages in a single Actor instead of an extra Actor for every ask.

    Jamie Allen has really good description of this scenario as the Extra and Cameo Patterns in Effective Akka.

    So with all this in mind, you might be able to follow something along the lines of:

    • When Consumer send the message to Connector, Connector can create a new Actor (Cameo) for this request context. You have to capture the sender Consumer in this Actor.
    • The Cameo Actor can kick off the subsequent requests through tells. At this point you can make either the Cameo or the Connector as the Supervisor so your Supervision Strategies still work as you want.
    • The Cameo in it's Receive block can wait for responses from Connections. This doesn't have to be an await on the ask. Just accept the message in the receive and then update your internal state.
    • When all the Connections are complete, you can respond to the original Consumer through tell.
    0 讨论(0)
提交回复
热议问题