@Test
  public void serializeActorRefs() {
    final ActorSystem theActorSystem = ActorSystem.create("whatever");
    final ActorRef theActorRef = theActorSystem.deadLetters(); // Of course this should be you

    // #actorref-serializer
    // Serialize
    // (beneath toBinary)
    final Address transportAddress = Serialization.currentTransportAddress().value();
    String identifier;

    // If there is no transportAddress,
    // it means that either this Serializer isn't called
    // within a piece of code that sets it,
    // so either you need to supply your own,
    // or simply use the local path.
    if (transportAddress == null) identifier = theActorRef.path().toString();
    else identifier = theActorRef.path().toStringWithAddress(transportAddress);
    // Then just serialize the identifier however you like

    // Deserialize
    // (beneath fromBinary)
    final ActorRef deserializedActorRef = theActorSystem.actorFor(identifier);
    // Then just use the ActorRef
    // #actorref-serializer
    theActorSystem.shutdown();
  }
  @Test
  public void demonstrateTheProgrammaticAPI() {
    // #programmatic
    ActorSystem system = ActorSystem.create("example");

    // Get the Serialization Extension
    Serialization serialization = SerializationExtension.get(system);

    // Have something to serialize
    String original = "woohoo";

    // Find the Serializer for it
    Serializer serializer = serialization.findSerializerFor(original);

    // Turn it into bytes
    byte[] bytes = serializer.toBinary(original);

    // Turn it back into an object,
    // the nulls are for the class manifest and for the classloader
    String back = (String) serializer.fromBinary(bytes);

    // Voilá!
    assertEquals(original, back);

    // #programmatic
    system.shutdown();
  }
 @After
 public void tearDown() {
   system.shutdown();
 }
Beispiel #4
0
 @AfterClass
 public static void tearDown() {
   system.shutdown();
 }