Class Serialization

java.lang.Object
org.apache.pekko.serialization.Serialization
All Implemented Interfaces:
Extension

public class Serialization extends Object implements Extension
Serialization module. Contains methods for serialization and deserialization as well as locating a Serializer for a particular class as defined in the mapping in the configuration.
  • Constructor Details

  • Method Details

    • serializedActorPath

      public static String serializedActorPath(ActorRef actorRef)
      The serialized path of an actorRef, based on the current transport serialization information. If there is no external address available in the given ActorRef then the systems default address will be used and that is retrieved from the ThreadLocal Serialization.Information that was set with withTransportInformation(org.apache.pekko.actor.ExtendedActorSystem, scala.Function0<T>).
    • withTransportInformation

      public static <T> T withTransportInformation(ExtendedActorSystem system, scala.Function0<T> f)
      Sets serialization information in a ThreadLocal and runs f. The information is needed for serializing local actor refs, or if serializer library e.g. custom serializer/deserializer in Jackson need access to the current ActorSystem. The current Serialization.Information can be accessed within f via getCurrentTransportInformation().

      Pekko Remoting sets this value when serializing and deserializing messages, and when using the ordinary serialize and deserialize methods in Serialization the value is also set automatically.

      Returns:
      value returned by f
    • getCurrentTransportInformation

      public static Serialization.Information getCurrentTransportInformation()
      Gets the serialization information from a ThreadLocal that was assigned via withTransportInformation(org.apache.pekko.actor.ExtendedActorSystem, scala.Function0<T>). The information is needed for serializing local actor refs, or if serializer library e.g. custom serializer/deserializer in Jackson need access to the current ActorSystem.

      Throws:
      IllegalStateException - if the information was not set
    • system

      public ExtendedActorSystem system()
    • settings

      public Serialization.Settings settings()
    • AllowJavaSerialization

      public boolean AllowJavaSerialization()
    • log

      public LoggingAdapter log()
    • serialize

      public scala.util.Try<byte[]> serialize(Object o)
      Serializes the given AnyRef/java.lang.Object according to the Serialization configuration to either an Array of Bytes or an Exception if one was thrown.
    • deserialize

      public scala.util.Try<Object> deserialize(byte[] bytes, int serializerId, String manifest)
      Deserializes the given array of bytes using the specified serializer id, using the optional type hint to the Serializer. Returns either the resulting object or an Exception if one was thrown.
    • deserializeByteBuffer

      public Object deserializeByteBuffer(ByteBuffer buf, int serializerId, String manifest) throws NotSerializableException
      Deserializes the given ByteBuffer of bytes using the specified serializer id, using the optional type hint to the Serializer. Returns either the resulting object or throws an exception if deserialization fails.
      Throws:
      NotSerializableException
    • deserialize

      public <T> scala.util.Try<T> deserialize(byte[] bytes, Class<T> clazz)
      Deserializes the given array of bytes using the specified type to look up what Serializer should be used. Returns either the resulting object or an Exception if one was thrown.
    • findSerializerFor

      public Serializer findSerializerFor(Object o)
      Returns the Serializer configured for the given object, returns the NullSerializer if it's null.

      Throws org.apache.pekko.ConfigurationException if no serialization-bindings is configured for the class of the object.

    • serializerFor

      public Serializer serializerFor(Class<?> clazz) throws NotSerializableException
      Returns the configured Serializer for the given Class. The configured Serializer is used if the configured class isAssignableFrom from the clazz, i.e. the configured class is a super class or implemented interface. In case of ambiguity it is primarily using the most specific configured class, and secondly the entry configured first.

      Throws java.io.NotSerializableException if no serialization-bindings is configured for the class.

      Throws:
      NotSerializableException
    • serializerOf

      public scala.util.Try<Serializer> serializerOf(String serializerFQN)
      Tries to load the specified Serializer by the fully-qualified name; the actual loading is performed by the system&rsquo;s pekko.actor.DynamicAccess.
    • serializerByIdentity

      public scala.collection.immutable.Map<Object,Serializer> serializerByIdentity()
      Maps from a Serializer Identity (Int) to a Serializer instance (optimization)