简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM