Akka Akka implementation: extension or injection?

I'm new to Akka (I'm using Java lib v2.3.9) and I'm wondering what the tradeoffs are between the two competing actor implementation strategies. On the one hand, I could implement my actors in a standard way:

// Groovy pseudo-code
class StormTrooper extends UntypedActor {
    @Override
    void onReceive(Object message) {
        if(message instanceof ExecuteOrder66) {
            // Betray all Jedi, serve the Emperor!
        }
    }
}

class DarthVader extends UntypedActor {
    @Override
    void onReceive(Object message) {
        if(message instanceof Feels) {
            // Betray the Emperor, save your son!
        }
    }
}

class Emperor extends UntypedActor {
    @Override
    void onReceive(Object message) {
        if(message instanceof Mace) {
            // Transform into your true self!
        }
    }
}

      

... instead, I could add an abstraction layer via injection:

class StarWarsCharacter extends UntypedActor {
    @Inject // Injected behavior; implementation doesn't matter
    BehaviorHandler behaviorHadler

    @Override
    void onReceive(Object message) {
        behaviorHandler.handle(message)
    }
}

interface BehaviorHandler {
    void handle(Object message)
}

class StormTrooperBehaviorHandler implements BehaviorHandler {
    @Override
    void handle(Object message) {
        // Betray all Jedi, serve the Emperor!
    }
}

class DarthVaderBehaviorHandler implements BehaviorHandler {
    @Override
    void handle(Object message) {
        // Betray the Emperor, save your son!
    }
}

class EmperorBehaviorHandler implements BehaviorHandler {
    @Override
    void handle(Object message) {
        // Transform into your true self!!
    }
}

      

Are there performance / class benefits of loading only one type of actor and injecting it with different implementations BehavioHandler

? Is there a reason I wouldn't want to do this and use the "standard" actor implementation?

+3


source to share


1 answer


If you just want to use your Actor as a way to send messages to handlers, then the second (non-standard) way is fine and can give you some advantages in terms of code structure - for example, the fact that it BehaviorHandler

is the only interface of a method will allow you to implement it. using a lambda.

However , Actors are not just method dispatchers. They provide a variety of methods that you will need as your system grows more complex.

See http://doc.akka.io/docs/akka/2.0/java/untyped-actors.html#untypedactor-api



A typical moderate-sized Akka system will need references to self

, sender

and context

that are defined on UntypedActor

.

In short: if you don't use inheritance, you are discarding everything you understand.

+1


source







All Articles