0

I have two applications communicating by Akka remote.

I have one class in my first application in a pachage a:

@SerialVersionUID(42L)
case class A()

and the same one in my second application but in a different package b:

@SerialVersionUID(42L)
case class A() 

But when I get by an actor message with an instance of the class a I get a java.lang.ClassNotFoundException: a.A because of the different package names.

Is there a way to easily avoid this?

Simon
  • 6,025
  • 7
  • 46
  • 98

1 Answers1

1

What you want would require a full classpath scan (what if there is a third mypackage3.A which has also the same SerialVersionUID? How does the JVM know that it has to deserialize mypackage1.A into another type?) and obviously SerialVersionUID is not meant for this. It is only meant to track serialization-compatibility across multiple version of the same class.

Have a read What is a serialVersionUID and why should I use it?

So you have two options:

  • You use manually deserialize the byte array from mypackage1.A into mypackage2.A
  • You share the data model and you avoid having two identical classes in two different packages in two different apps
Community
  • 1
  • 1
Edmondo
  • 19,559
  • 13
  • 62
  • 115
  • 1
    For the first option: if I use Protobuf, would it work? For the second one: is there an easy way to share the data model? The only way I can think of is to create a packaged library but it requires me to repackage it every time I want to amend this model. – Simon Oct 21 '16 at 10:40
  • If you want two apps to speak with each other, you should agree on some sort of data model. Using protobuf is great if you need fast and portable serialization, so you can read/write from multiple languages, but doesn't overcome that you need to share your .proto definitions – Edmondo Oct 21 '16 at 12:57
  • So there is no other way than creating a packaged library and repackage it every time I want to amend this model? – Simon Oct 21 '16 at 15:33
  • 1
    There are always two approaches: format agreed upfront and free format. If you want a typesafe api, you need to agree upfront. If not, you can use json and leave the interpretation of the message to the receiver – Edmondo Oct 22 '16 at 12:32