-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistent actor does not restore state from Journal #102
Comments
Thanks to issue 72 I found that actor receives JObject not ChatMessage.Event:
The complete sample is posted to https://github.com/OlegZee/akkling-persist-sample. And got a great advising from @OObject to look into EventAdapter. |
@OlegZee So you've got Json.net configured for serialization of The quick fix would be to use your own dedicated serializer. In the meantime I'll try to make some better solution. |
@Horusiath thank you. For now I made the following hack:
and now looking into custom event adapter solution. |
@Horusiath, I've just pushed the sample project with another workaround - provide custom event adapter which properly encodes type of the event: type EventAdapter(__ : Akka.Actor.ExtendedActorSystem) =
interface Akka.Persistence.Journal.IEventAdapter with
member __.Manifest(_ : obj) =
let manifestType = typeof<Newtonsoft.Json.Linq.JObject>
sprintf "%s,%s" manifestType.FullName <| manifestType.Assembly.GetName().Name
member __.ToJournal(evt : obj) : obj =
new JObject(
new JProperty("evtype", evt.GetType().FullName),
new JProperty("value", JObject.FromObject(evt))
)
:> obj
member __.FromJournal(evt : obj, _ : string) : Akka.Persistence.Journal.IEventSequence =
match evt with
| :? JObject as jobj ->
match jobj.TryGetValue("evtype") with
| false, _ -> box jobj
| _, typ ->
let t = Type.GetType(typ.ToString())
jobj.["value"].ToObject(t)
|> Akka.Persistence.Journal.EventSequence.Single
| _ ->
Akka.Persistence.Journal.EventSequence.Empty Looking back to payload, which Akka fails to deserialize: {{
"Case": "Event",
"Fields": [
{
"$id": "1",
"$type": "Program+ChatEvent, akka-persist",
"Message": "New session started 20.05.2018 12:31:02"
}
]
}} it seems that |
@Horusiath, @object now I could state the issue sits inside Akka.NET serializer. As I noticed above How is this connected with Akkling... What if we have two separated type parameters for |
Final step is checking NewtonSoft behavior which Akka NewtonsoftJsonSerializer relies on: open Newtonsoft.Json
type ChatEvent = { Message : string }
type ChatCommand = | Message of string | GetMessages
type ChatMessage = | Command of ChatCommand | Event of ChatEvent
[<EntryPoint>]
let main argv =
let settings = new JsonSerializerSettings()
settings.TypeNameHandling <- TypeNameHandling.All
let serialized = JsonConvert.SerializeObject({Message = "hhh"}, settings)
let deser = JsonConvert.DeserializeObject<_>(serialized, settings)
printfn "%s\n%A\n" serialized deser
let serialized = JsonConvert.SerializeObject(Event {Message = "Hello world"}, settings)
let deser = JsonConvert.DeserializeObject<_>(serialized, settings)
printfn "%s\n\n%A" serialized deser
0 results in
The first case, record type is deserialized into |
Here's the minimalistic sample which is based on sample.
On every run the new message is posted to actor and it should print more messages on every subsequent run.
Actual behavior:
The sample project is available at https://github.com/OlegZee/akkling-persist-sample/tree/d116eef4bd55a0eb91a8c33e05a9bdc5df679435
The text was updated successfully, but these errors were encountered: