本文整理汇总了Scala中akka.serialization.Serialization类的典型用法代码示例。如果您正苦于以下问题:Scala Serialization类的具体用法?Scala Serialization怎么用?Scala Serialization使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Serialization类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ServerActor
//设置package包名称以及导入依赖的类
package com.naoh.beef.internal.server
import akka.actor.Actor
import akka.actor.Props
import akka.serialization.Serialization
import com.naoh.beef.Beef
import com.naoh.beef.Server
import com.naoh.beef.internal.Network.ServerLocation
import com.naoh.beef.internal.ServerSelectorActor
import com.naoh.beef.proto.network.RequestChannel
import com.naoh.beef.proto.network.ServerChannel
object ServerActor {
def props(server: Server) = Props(new ServerActor(server))
}
class ServerActor(
server: Server
) extends Actor {
val location = ServerLocation(server.region, self)
val locationProto = location.toProto
Beef(context.system).network ! ServerSelectorActor.Add(location)
println("Server Actor Start")
var created = 0
override def receive: Receive = {
case req: RequestChannel =>
sender() ! create()
}
def create(): ServerChannel = {
val ref = context.actorOf(ServerChannelActor.props(server), s"channel-$created")
created += 1
ServerChannel(Some(location.toProto), Serialization.serializedActorPath(ref))
}
}
示例2: AssemblyApp
//设置package包名称以及导入依赖的类
package csw.services.integtration.apps
import akka.actor.{Actor, ActorPath, Props}
import akka.serialization.Serialization
import csw.services.integtration.common.TestFutureExtension.RichFuture
import csw.services.location.commons.{ClusterSettings, CswCluster}
import csw.services.location.models.Connection.AkkaConnection
import csw.services.location.models.{AkkaRegistration, ComponentId, ComponentType}
import csw.services.location.scaladsl.{ActorSystemFactory, LocationServiceFactory}
object AssemblyApp {
private val cswCluster = CswCluster.withSettings(ClusterSettings().withInterface("eth1"))
val assemblyActorRef = ActorSystemFactory.remote.actorOf(Props[AssemblyApp], "assembly")
val componentId = ComponentId("assembly", ComponentType.Assembly)
val connection = AkkaConnection(componentId)
val actorPath = ActorPath.fromString(Serialization.serializedActorPath(assemblyActorRef))
val registration = AkkaRegistration(connection, assemblyActorRef)
val registrationResult = LocationServiceFactory.withCluster(cswCluster).register(registration).await
def main(args: Array[String]): Unit = {}
}
class AssemblyApp extends Actor {
override def receive: Receive = {
case "Unregister" => {
AssemblyApp.registrationResult.unregister()
}
}
}
示例3: JBasicLoggerActor
//设置package包名称以及导入依赖的类
package csw.services.logging.javadsl
import java.util.Optional
import akka.actor.{AbstractActor, ActorPath}
import akka.serialization.Serialization
import akka.typed.javadsl.ActorContext
import csw.services.logging.internal.JLogger
import csw.services.logging.scaladsl.LoggerImpl
import scala.compat.java8.OptionConverters.RichOptionalGeneric
abstract class JBasicLoggerActor extends AbstractActor {
protected def maybeComponentName: Optional[String]
protected def getLogger: ILogger = {
val actorName = ActorPath.fromString(Serialization.serializedActorPath(getSelf)).toString
val log = new LoggerImpl(maybeComponentName.asScala, Some(actorName))
new JLogger(log, getClass)
}
}
import akka.typed.scaladsl.adapter._
abstract class JBasicLoggerTypedActor[T](actorContext: ActorContext[T])
extends akka.typed.javadsl.Actor.MutableBehavior[T] {
protected def maybeComponentName: Optional[String]
protected def getLogger: ILogger = {
val actorName = ActorPath.fromString(Serialization.serializedActorPath(actorContext.getSelf.toUntyped)).toString
val log = new LoggerImpl(maybeComponentName.asScala, Some(actorName))
new JLogger(log, getClass)
}
}
示例4: PgAsyncSnapshotStore
//设置package包名称以及导入依赖的类
package akka.persistence.pg.snapshot
import akka.actor.{ActorLogging, ActorSystem}
import akka.persistence.pg.{PgConfig, PgExtension}
import akka.persistence.serialization.Snapshot
import akka.persistence.{SelectedSnapshot, SnapshotMetadata, SnapshotSelectionCriteria}
import akka.serialization.{Serialization, SerializationExtension}
import scala.concurrent.Future
class PgAsyncSnapshotStore extends akka.persistence.snapshot.SnapshotStore
with PgSnapshotStore
with ActorLogging
with PgConfig {
implicit val system: ActorSystem = context.system
implicit val executionContext = context.system.dispatcher
override val serialization: Serialization = SerializationExtension(context.system)
override lazy val pluginConfig = PgExtension(context.system).pluginConfig
import driver.api._
override def loadAsync(persistenceId: String, criteria: SnapshotSelectionCriteria): Future[Option[SelectedSnapshot]] = {
log.debug(s"loading snapshot for persistenceId: {}, criteria: {}", persistenceId, criteria)
selectMostRecentSnapshotFor(persistenceId, criteria)
}
override def saveAsync(metadata: SnapshotMetadata, snapshot: Any): Future[Unit] = {
log.debug(s"saving snapshot for metadata {}",metadata)
val serialized: Array[Byte] = serialization.serialize(Snapshot(snapshot)).get
database.run(snapshotsQuery(metadata).length
.result.flatMap { result: Int =>
val v = (metadata.persistenceId, metadata.sequenceNr, metadata.timestamp, serialized)
if (result > 0) {
snapshotsQuery(metadata).update(v)
} else {
snapshots += v
}
}).map { _ => () }
}
override def deleteAsync(metadata: SnapshotMetadata): Future[Unit] = {
log.debug(s"deleting: {}",metadata)
deleteSnapshot(metadata).map { _ =>
log.debug(s"deleted snapshot {}",metadata)
}
}
override def deleteAsync(persistenceId: String, criteria: SnapshotSelectionCriteria): Future[Unit] = {
log.debug(s"deleting for persistenceId: {} and criteria: {}",persistenceId, criteria)
database.run(selectSnapshotsQuery(persistenceId, criteria).delete).map { deleted =>
log.debug(s"deleted {} snapshots", deleted); ()
}
}
}
示例5: serialization
//设置package包名称以及导入依赖的类
package akka.persistence.pg.snapshot
import akka.persistence.pg.PgConfig
import akka.persistence.serialization.Snapshot
import akka.persistence.{SelectedSnapshot, SnapshotMetadata, SnapshotSelectionCriteria}
import akka.serialization.Serialization
import scala.concurrent.{ExecutionContext, Future}
import scala.language.postfixOps
trait PgSnapshotStore extends SnapshotTable {
self: PgConfig =>
def serialization: Serialization
import driver.api._
def snapshotsQuery(metadata: SnapshotMetadata) = {
snapshots
.filter(_.persistenceId === metadata.persistenceId)
.filter(_.sequenceNr === metadata.sequenceNr)
}
def deleteSnapshot(metadata: SnapshotMetadata): Future[Int] = {
database.run {
snapshotsQuery(metadata).delete
}
}
def selectMostRecentSnapshotFor(persistenceId: String, criteria: SnapshotSelectionCriteria)
(implicit executionContext: ExecutionContext): Future[Option[SelectedSnapshot]] = {
database.run {
selectSnapshotsQuery(persistenceId, criteria)
.sortBy(_.sequenceNr.desc)
.take(1)
.result.headOption
} map {
_ map { r =>
SelectedSnapshot(SnapshotMetadata(r._1, r._2, r._3), serialization.deserialize(r._4, classOf[Snapshot]).get.data)
}
}
}
def selectSnapshotsQuery(persistenceId: String, criteria: SnapshotSelectionCriteria): Query[SnapshotTable, (String, Long, Long, Array[Byte]), Seq] = {
snapshots
.filter(_.persistenceId === persistenceId)
.filter(_.sequenceNr <= criteria.maxSequenceNr)
.filter(_.timestamp <= criteria.maxTimestamp)
}
}
示例6: PgSnapshotStoreSpec
//设置package包名称以及导入依赖的类
package akka.persistence.pg.testkit
import akka.persistence.pg.snapshot.PgSnapshotStore
import akka.persistence.pg.{PgExtension, PgConfig}
import akka.persistence.pg.util.RecreateSchema
import akka.persistence.snapshot.SnapshotStoreSpec
import akka.serialization.{Serialization, SerializationExtension}
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Milliseconds, Second, Span}
import scala.language.postfixOps
class PgSnapshotStoreSpec extends SnapshotStoreSpec(ConfigFactory.load("pg-application.conf"))
with PgSnapshotStore
with RecreateSchema
with ScalaFutures
with PgConfig {
override implicit val patienceConfig = PatienceConfig(timeout = Span(1, Second), interval = Span(100, Milliseconds))
override lazy val pluginConfig = PgExtension(system).pluginConfig
override val serialization: Serialization = SerializationExtension(system)
import driver.api._
override def beforeAll() {
database.run(recreateSchema.andThen(snapshots.schema.create)).futureValue
super.beforeAll()
}
override protected def afterAll(): Unit = {
system.terminate()
system.whenTerminated.futureValue
()
}
}
示例7: jsonToAny
//设置package包名称以及导入依赖的类
package com.seancheatham.akka
import akka.serialization.Serialization
import com.google.common.io.BaseEncoding
import play.api.libs.json._
package object persistence {
def jsonToAny(v: JsValue)(implicit serialization: Serialization): Any =
serializedToAny((v \ "vt").as[String], (v \ "v").get)
protected[persistence] def serializedToAny(typeName: String, json: JsValue)(implicit serialization: Serialization): Any = {
typeName match {
case "int" => json.as[Int]
case "long" => json.as[Long]
case "float" => json.as[Float]
case "double" => json.as[Double]
case "boolean" => json.as[Boolean]
case "string" => json.as[String]
case t =>
val decoded = BaseEncoding.base64().decode(json.as[String])
serialization.deserialize(decoded, t.toInt, None).get
}
}
}