Objects that do not require you to register a serializer
Because of the multiple class loaders approach used by OSGi, you must register a Serializer
for each distributable object, except objects of the following types:
Byte | byte[] |
Boolean | char[] |
Character | short[] |
Short | int[] |
Integer | long[] |
Long | float[] |
Float | double[] |
Double | String |
Serialization, Distributed Maps, and the Publish Subscribe service
You must register a serializer for each object that is not listed in Objects that do not require you to register a serializer. After you register use the CoordinationService to register the serializer for an object, you can use that object directly with the Publish Subscribe Service and with Distributed Maps.
Register your serializers before you register subscribers and distributed map listeners.
Remove your serializers after you remove subscribers and distributed map listeners.
Declare Distributable
before you declare Serializable
If a distributable object implements Serializable
, in the class hierarchy from the distributable object to its super classes, you must declare Distributable
classes before you declare Serializable
super classes.
The class hierarchy is analyzed when registering the serializer. If Serializable
is found before Distributable
, an exception is thrown with a message that describes this restriction.
The following example shows declaring a distributable object before declaring a serializable object.
Declaring a distributable object before declaring a serializable object
import com.hp.api.Distributable; class ValidDistributableType implements Distributable { } class ValidDistributableType implements Distributable, Serializable { } class ValidDistributableType extends SerializableType implements Distributable { } class InvalidDistributableType implements Serializable, Distributable { }
Registering a serializer
@Component public class Consumer { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.DYNAMIC) private volatile CoordinationService coordinationService; @Activate public void activate() { coordinationService.registerSerializer(new MyDistributableObjectSerializer(), MyDistributableObject.class); } @Deactivate public void deactivate() { coordinationService.unregisterSerializer(MyDistributableObject.class); } private static class MyDistributableObjectSerializer implements Serializer<MyDistributableObject> { @Override public byte[] serialize(MyDistributableObject subject) { ... } @Override public MyDistributableObject deserialize(byte[] serialization) throws IllegalArgumentException { ... } } }