7.3. Using an Advanced Externalizer

Using a customized advanced externalizer helps optimize performance in Red Hat JBoss Data Grid.
  1. Define and implement the readObject() and writeObject() methods.
  2. Link externalizers with marshaller classes.
  3. Register the advanced externalizer.

7.3.1. Implement the Methods

To use advanced externalizers, define and implement the readObject() and writeObject() methods. The following is a sample definition:

Example 7.2. Define and Implement the Methods

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> {
      public void writeObject(ObjectOutput output, Person person)
            throws IOException {
      public Person readObject(ObjectInput input)
            throws IOException, ClassNotFoundException {
         return new Person((String) input.readObject(), input.readInt());
      public Set<Class<? extends Person>> getTypeClasses() {
         return Util.<Class<? extends Person>>asSet(Person.class);
      public Integer getId() {
         return 2345;


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.

7.3.3. 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 7.3. Registering the Advanced Externalizer Programmatically

GlobalConfigurationBuilder builder = ...
   .addAdvancedExternalizer(new Person.PersonExternalizer());
Enter the desired information for the GlobalConfigurationBuilder in the first line.

7.3.4. 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 7.4. Registering Multiple Externalizers

   .addAdvancedExternalizer(new Person.PersonExternalizer(),
                            new Address.AddressExternalizer());