Reputation: 231
I'm trying to understand the tradeoffs and best practices (if there are) between the following options:
Messages
Actors
.match(POJOA.class, message -> {
...
.match(POJOB.class, message -> {
...
.match(POJOC.class, message -> {
...
if (POJO.getPhase().equals("start"))
...
else ...
What is the common practice to use? What is better in turms of performance, code maintanability etc.
Upvotes: 0
Views: 230
Reputation: 4652
A mixture of A, B and C has worked very well for me so far. You want your messages as specific as you can (so they are self-explanatory), but you still need some generality.
For example: suppose you are using your actors as a key-value store.
You can have some generic Get/Set/Delete messages
public class Get {String key;}
public class Set<T> {String key; T value;}
public class Delete {String key;}
The issue is your Set message. It's type parametric. Some serialization frameworks do not make that easy on you.
So you can change it to specific sets:
public class SetA {String key; A value;}
public class SetB {String key; B value;}
And bam, the type parameter is gone.
But the 3rd option is something you still need! Akka serialization requires you to bind a serializer to a class (the top level one), and in most cases you want to use a single serializer for all your messages, so the way to go for that is an empty interace:
public interface Message {}
public class Get implements Message {String key;}
public class SetA implements Message {String key; A value;}
public class SetB implements Message {String key; B value;}
public class Delete implements Message {String key;}
And then in your config file:
akka.actor{
serializers {
mySerializer = xxx
}
serialization-bindings {
Message = mySerializer
}
}
Upvotes: 0