Reputation: 13471
Is it possible serialize a Scala Future into byte[]
and then deserialize it again?.
And if is possible, does someone know the best approach?
Upvotes: 0
Views: 439
Reputation: 582
That shouldn't be possible with the standard-implementation of Futures in scala. Future clearly doesn't implement the interface Serializable and isn't supposed to be serialized.
The question is, what would happen when the Future isn't finished yet. The Future gets executed because it's task is scheduled in the ExecutionContext. If one would serialize this Future and deserialize it on another JVM, then the task would only be scheduled on the first JVM. On the second JVM, the future would never get its result.
It probably makes more sense to serialize the content of a Future, as soon as it is ready.
Upvotes: 1
Reputation: 44937
import concurrent.ExecutionContext.Implicits._
println(if ((concurrent.Future{}).isInstanceOf[Serializable]) "Yes" else "No")
prints
No
Upvotes: 1