@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(); }
/** * Testing akka serialization * * @param system the system to test on * @param test the object to test */ public static void testSerialization(ActorSystem system, Object test) throws Exception { // Get the Serialization Extension Serialization serialization = SerializationExtension.get(system); // Find the Serializer for it Serializer serializer = serialization.findSerializerFor(test); serializer.toBinary(test); serializer.fromBinary(serializer.toBinary(test)); }
@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(); }
private void createTransaction(CreateTransaction createTransaction) { try { if (TransactionType.fromInt(createTransaction.getTransactionType()) != TransactionType.READ_ONLY && failIfIsolatedLeader(getSender())) { return; } ActorRef transactionActor = createTransaction( createTransaction.getTransactionType(), createTransaction.getTransactionId(), createTransaction.getTransactionChainId(), createTransaction.getVersion()); getSender() .tell( new CreateTransactionReply( Serialization.serializedActorPath(transactionActor), createTransaction.getTransactionId()) .toSerializable(), getSelf()); } catch (Exception e) { getSender().tell(new akka.actor.Status.Failure(e), getSelf()); } }
@Test public void testToSerializable() { TestActorRef<Actor> testActor = factory.createTestActor(MessageCollectorActor.props()); RegisterChangeListener registerChangeListener = new RegisterChangeListener( TestModel.TEST_PATH, testActor, AsyncDataBroker.DataChangeScope.BASE); ListenerRegistrationMessages.RegisterChangeListener serialized = registerChangeListener.toSerializable(); NormalizedNodeMessages.InstanceIdentifier path = serialized.getInstanceIdentifierPath(); assertEquals( "urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", path.getCode(0)); assertEquals( Serialization.serializedActorPath(testActor), serialized.getDataChangeListenerActorPath()); assertEquals(AsyncDataBroker.DataChangeScope.BASE.ordinal(), serialized.getDataChangeScope()); }