6.3. Using an Advanced Externalizer Using a customized advanced externalizer helps optimize performance in Red Hat JBoss Data Grid. Define and implement the readObject() and writeObject() methods. Link externalizers with marshaller classes. Register the advanced externalizer. Report a bug 6.3.1. Implement the Methods To use advanced externalizers, define and implement the readObject() and writeObject() methods. The following is a sample definition: Example 6.2. Define and Implement the Methods Note This method does not require annotated user classes. As a result, this method is valid for classes where the source code is not available or cannot be modified. Report a bug 6.3.2. Link Externalizers with Marshaller Classes Use an implementation of getTypeClasses() to discover the classes that this externalizer can marshall and to link the readObject() and writeObject() classes. The following is a sample implementation: In the provided sample, the ReplicableCommandExternalizer indicates that it can externalize several command types. This sample marshalls all commands that extend the ReplicableCommand interface but the framework only supports class equality comparison so it is not possible to indicate that the classes marshalled are all children of a particular class or interface. In some cases, the class to be externalized is private and therefore the class instance is not accessible. In such a situation, look up the class with the provided fully qualified class name and pass it back. An example of this is as follows: Report a bug 6.3.3. Register the Advanced Externalizer (Declaratively) After the advanced externalizer is set up, register it for use with Red Hat JBoss Data Grid. This registration is done declaratively (via XML) as follows: Procedure 6.1. Register the Advanced Externalizer Add the global element to the infinispan element. Add the serialization element to the global element. Add the advancedExternalizers element to add information about the new advanced externalizer. Define the externalizer class using the externalizerClass attributes. Replace the USDIdViaAnnotationObj and USDAdvancedExternalizer values as required. Report a bug 6.3.4. Register the Advanced Externalizer (Programmatically) After the advanced externalizer is set up, register it for use with Red Hat JBoss Data Grid. This registration is done programmatically as follows: Example 6.3. Registering the Advanced Externalizer Programmatically Enter the desired information for the GlobalConfigurationBuilder in the first line. Report a bug 6.3.5. Register Multiple Externalizers Alternatively, register multiple advanced externalizers because GlobalConfiguration.addExternalizer() accepts varargs . Before registering the new externalizers, ensure that their IDs are already defined using the @Marshalls annotation. Example 6.4. Registering Multiple Externalizers Report a bug

Commands:
import org.infinispan.commons.marshall.AdvancedExternalizer; public class Person { final String name; final int age; public Person(String name, int age) { this.name = name; this.age = age; } public static class PersonExternalizer implements AdvancedExternalizer<Person> { @Override public void writeObject(ObjectOutput output, Person person) throws IOException { output.writeObject(person.name); output.writeInt(person.age); } @Override public Person readObject(ObjectInput input) throws IOException, ClassNotFoundException { return new Person((String) input.readObject(), input.readInt()); } @Override public Set<Class<? extends Person>> getTypeClasses() { return Util.<Class<? extends Person>>asSet(Person.class); } @Override public Integer getId() { return 2345; } } }
import org.infinispan.util.Util; <!-- Additional configuration information here --> @Override public Set<Class<? extends ReplicableCommand>> getTypeClasses() { return Util.asSet(LockControlCommand.class, GetKeyValueCommand.class, ClusteredGetCommand.class, MultipleRpcCommand.class, SingleRpcCommand.class, CommitCommand.class, PrepareCommand.class, RollbackCommand.class, ClearCommand.class, EvictCommand.class, InvalidateCommand.class, InvalidateL1Command.class, PutKeyValueCommand.class, PutMapCommand.class, RemoveCommand.class, ReplaceCommand.class); }
@Override public Set<Class<? extends List>> getTypeClasses() { return Util.<Class<? extends List>>asSet( Util.<List>loadClass("java.util.CollectionsUSDSingletonList", null)); }
<infinispan> <global> <serialization> <advancedExternalizers> <advancedExternalizer externalizerClass="org.infinispan.marshall.AdvancedExternalizerTestUSDIdViaAnnotationObjUSDExternalizer"/> </advancedExternalizers> </serialization> </global> </infinispan>
GlobalConfigurationBuilder builder = builder.serialization() .addAdvancedExternalizer(new Person.PersonExternalizer());
builder.serialization() .addAdvancedExternalizer(new Person.PersonExternalizer(), new Address.AddressExternalizer());