简体   繁体   English

akka序列化异常跨远程角色发送平滑表查询

[英]akka serialization exception sending slick table query across remote actors

Am trying to send the table query object from local actor to remote actor, but akka gives serialization exception 我试图将表查询对象从本地actor发送到远程actor,但是akka给出了序列化异常

Remote Actor looks like this and am using active slick pattern here Remote Actor看起来像这样,并且在这里使用主动滑模

object RemoteActor {
  case class Entry[M, I: BaseColumnType, T <: IdTable[M, I]](tableWithIdQuery: TableWithIdQuery[M, I, T], model: M)
}

class RemoteActor extends Actor with ActorLogging {

import RemoteActor._

lazy val db = Database.forURL(
  url = "jdbc:mysql://localhost/demo",
  driver = "com.mysql.jdbc.Driver",
  user= "root",
  password= "root")

override def receive = {
  case Entry(table, model) =>
      //I will wrap this code in a Future and use akka.pattern.pipe
      // thus avoiding deafening the actor
      //avoided wrapping in future just to avoid clutter
      db.withSession { implicit sx =>
      table.createIfNotExists
      table.save(model)
    }
    log.info("done saving to database")
  case msg => log.info(s"unknown message of type ${msg.getClass}")
}
}

Local Actor looks like this. 本地演员看起来像这样。 Local Actor builds TableWithIdQuery and passes it to the Remote Actor mentioned above. Local Actor构建TableWithIdQuery并将其传递给上述的Remote Actor。

class LocalActor extends Actor with ActorLogging {

import LocalActor._

var remoteActor: Option[ActorSelection] = None

override def preStart(): Unit = {
  remoteActor =   Some(context.actorSelection("akka.tcp://ActorSystem@127.0.0.1:2222/user/RemoteActor"))
  remoteActor.getOrElse {
    println(" unreachable, shutting down :(")
    context.stop(self)
  }
}

override def receive = {
  case Send =>
    case class User(name: String, id: Option[Long] = None)

    class Users(tag: Tag) extends IdTable[User, Long](tag, "users") {
      def name = column[String]("name")
      def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
      def * = (name, id.?) <> (User.tupled, User.unapply)
    }

    val users = new TableWithIdQuery[User, Long, Users](tag => new Users(tag)) {
      override def extractId(model: User): Option[Long] = model.id
      override def withId(model: User, id: Long): User = model.copy(id = Some(id))
    }

    import remote.RemoteActor._

    remoteActor.map(remote => remote ! Entry[User, Long, Users](users,   User("pamu nagarjuna")))
    log.info("message sent :)")

    case msg => log.info(s"unknown message of type ${msg.getClass}")
}
}

It's not safe to assume that any thing like a Query is serializable. 假设任何查询(例如查询)都是可序列化的,这是不安全的。 You really ought to run the query in a local context, map the results to a set of serializable types (eg immutable collections, case classes), and send that. 您确实应该在本地上下文中运行查询,将结果映射到一组可序列化的类型(例如,不可变的集合,案例类),然后将其发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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