To implement your own custom actor in Akka (Java binding), you extend the base class UntypedActor . This requires that you define your own onReceive(...) method:
@Override public void onReceive(Object message) {
The problem is defining a message processing strategy that allows entities to process multiple types of messages. One strategy would be to use reflections / types. The problem here is that:
- This forces us to create empty โshell classesโ that not only provide semantic meaning for the message (see below); and
- It runs the
message parameter and does not allow us to pass anything dynamic or meaningful
An example of an empty shell class:
public class EmptyShellMessage { }
Then in the onReceive method onReceive will look like this:
@Override public void onReceive(Class<?> message) { if(message.isAssignableFrom(EmptyShellMessage.class)) {
Thus, we are creating not only a useless class, but since the Object message now used to encode that class / message type, we cannot use it to contain more information, especially dynamic / that can be conveyed by another actor.
Sometimes I see a variation of this:
@Override public void onReceive(Object message) { if(message instanceof FizzEvent) {
But here we use instanceof , which is considered by many to be a huge anti-pattern (just google "instanceof antipattern").
Then we have the listings:
public enum ActorMessage { FizzEvent, BuzzEvent, FooEvent, BarEvent }
Now onReceive looks like this:
@Override public void onReceive(ActorMessage message) { if(message.equals(ActorMessage.FizzEvent)) {
The problem is that we can have a large system of actors with hundreds or even thousands of different types of events / messages. This listing becomes large and difficult to maintain. It also has the same problem as the reflection strategy above, where it prevents us from transmitting any dynamic information between the participants.
The last thing I can think of is to use the lines:
@Override public void onReceive(String message) { if(message.equals("FizzEvent")) {
But I hate that. Period. The end of the sentence.
So I ask: did I miss something obvious here, maybe a different strategy? How should Java / Akka applications process a large number of event / message types and indicate which one they process in the onReceive method?