blue-sky
blue-sky

Reputation: 53826

Should akka message implement Serializable?

I've encountered an custom Akka message type that implements Serializable :

public Interface MyMessage
{
    public String getId();
}

public class MyMessageImpl implements MyMessage, Serializable {
     private String id;

     public MyMessageImpl (String id){
       this.id = id;
     }

     public String getId(){
       return this.id;
     }
    }

I'm unaware why Serializable is being implemented as part of a message being sent to an actor. Is this required ? Only reason I can think of is that Akka sends serialized messages over wire from supervisor to actor ? In my testing it does not appear to make any difference.

Developer that wrote this class has since moved on.

Upvotes: 0

Views: 1452

Answers (1)

George Simms
George Simms

Reputation: 4060

All messages sent over the wire must be serializable.

(They often are implicitly by being case classes or case objects.) Hello Akka Scala works if I change Greet to an ordinary object and thus make it not Serializable, which suggests that running in the same JVM they don't have to be, although it is a good habit.

By contrast they should also be immutable, in case the message received is reference-equal to the message sent, which they probably will be in the same JVM.

EDIT So if you modified a message in one actor it would quite likely effect its state in all the other actors in the same JVM. Of course, were it in a different JVM that wouldn't be possible unless you were doing something like remoting which we are not.

Upvotes: 1

Related Questions