0

While trying to persist an Event and update the state of my actor, I get this exception:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `module.MyActor$State`: non-static inner classes like this can only by instantiated using default, no-argument constructor

Here is how my actor case class looks like:

object MyActor {

  final case class Settings(
      name: String = "",
      accessToken: AccessToken = AccessToken()      


     ) extends CborSerializable
     .
     .
     .
}

class MyActor()(implicit system: ActorSystem[_]) {
  import MyActor._

  private implicit val executionContext: ExecutionContextExecutor = system.executionContext
  private val sharding = ClusterSharding(system)

  // State
  final case class State(
      settings: Settings = Settings(),
      pollTime: Long = 0L
  ) extends CborSerializable
  . 
  .
  private def onEvent(state: State, event: Event): State = {
event match {
  case _ => {            
       state.copy(pollTime = state.pollTime.updated(timestampToStringDate(System.currentTimeMillis())))
   }
  }
 .
 .     
}

This is a typed EventSourced Actor, The Event/Command handlers are omitted here since the issue is wrt to serialization.

I am using jackson serializer and have updated that in my application.conf

akka.actor.serialization-bindings {
  "module.CborSerializable" = jackson-cbor
}

I defined a trait CborSerializable in my module.

I wanted to understand what am I doing wrong here.

  • Your inner class: State, has no default no-args constructor. That's the basic issue. You'll have to move the params to inner fields and use them with getters (and setters). That's my best hunch. – Jurgen Rutten Oct 20 '22 at 08:23
  • Alternatively, move the `State` class to `object MyActor`... it's generally a really bad idea to have the state type not be static. It also looks like there's a weird mixture of typed and classic APIs for defining an event-sourced actor. – Levi Ramsey Oct 20 '22 at 23:07

0 Answers0