此内容没有您所选择的语言版本。
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()
andwriteObject()
methods. - Link externalizers with marshaller classes.
- Register the advanced externalizer.
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.
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:
@Override public Set<Class<? extends List>> getTypeClasses() { return Util.<Class<? extends List>>asSet( Util.<List>loadClass("java.util.Collections$SingletonList", null)); }
@Override
public Set<Class<? extends List>> getTypeClasses() {
return Util.<Class<? extends List>>asSet(
Util.<List>loadClass("java.util.Collections$SingletonList", null));
}
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 theinfinispan
element:<infinispan> <global /> </infinispan>
<infinispan> <global /> </infinispan>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the
serialization
element to theglobal
element as follows:<infinispan> <global> <serialization /> </global> </infinispan>
<infinispan> <global> <serialization /> </global> </infinispan>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the
advancedExternalizers
element to add information about the new advanced externalizer as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Define the externalizer class using the
externalizerClass
attribute as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace the $IdViaAnnotationObj and $AdvancedExternalizer values as required.
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
GlobalConfigurationBuilder builder = ... builder.serialization() .addAdvancedExternalizer(new Person.PersonExternalizer());
GlobalConfigurationBuilder builder = ...
builder.serialization()
.addAdvancedExternalizer(new Person.PersonExternalizer());
Enter the desired information for the GlobalConfigurationBuilder in the first line.
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
builder.serialization() .addAdvancedExternalizer(new Person.PersonExternalizer(), new Address.AddressExternalizer());
builder.serialization()
.addAdvancedExternalizer(new Person.PersonExternalizer(),
new Address.AddressExternalizer());