A General-Purpose Object
Marshalling Framework
David M. Lloyd
Senior Software Engineer
JBoss, a division of Red Hat, Inc.
[email protected]
About the Presenter
Developing software for around 12 years
• Primary focus on networking software (clients and servers)
Started at JBoss in August 2006
Joined the JBoss Remoting team in 2007
• Work was begun on the next-generation Remoting implementation
• This work lead to the development of JBoss Marshalling
What is Marshalling?
In this context, Marshalling is the process of converting
an object or object graph into a stream of bytes.
• “Serialization” and “Marshalling” are synonymous
• JBoss Marshalling is so named to differentiate from standard Java
Serialization, as well as the existing (but different) JBoss
Serialization project
Why isn't Java Serialization good enough?
Monolithic (sometimes confusing) API
• Very large and complex base classes
• Protocol extensions can only be implemented by subclassing the
Object*Stream classes, sometimes with great difficulty
Customizing class descriptor format
• Some things are almost impossible to change (at least, without resorting to
reflection)
Customizing (or removing) the stream header format
Pre-sizing internal tables
Performance limited implementation
• Cannot specify tuning parameters
• Protocol structures are quite large; often unnecessarily so
Why isn't Java Serialization good enough?
(cont.)
Heavyweight construction
• Makes pooling very difficult
• Stream header is written on construction
• Relatively expensive permission check is required to construct an instance
Bugs tied to JDK
• If you find a problem, the only solution is file a bug and wait for the next JDK
release, which might entail re-testing more than just the module which
encountered the problem
Why is JBoss Marshalling better?
Lightweight marshaller/unmarshaller instance creation
• Permission checking is moved to factory construction rather than instance
construction
• Instances may be easily reused if desired
Easy to configure
Ability to support multiple implementations easily
• Marshalling API elements are mostly interfaces
Highly tunable
Very customizable with special marshalling strategies
Constructing Marshallers and Unmarshallers
Every protocol implementation has a corresponding
MarshallerFactory
A MarshallerFactory has a create method for Marshallers
and one for Unmarshallers
A MarshallerConfiguration instance is required by either
method
Usage example: HelloWorld
(example)
Configurable Parameters
Expected instance count
• Number of distinct objects expected to be sent
Expected class count
• Number of distinct classes expected to be sent
Buffer size
• Used by any implementation which employs an internal buffer
Pluggable Features
Stream Header
Class Table
Object Table
Class Resolver
Object Resolver
Class Externalizer Factory
Creator
Stream Header
Used mainly for compatibility with existing serialization
protocols, to write out or consume a fixed header
Convenience static methods for fixed byte patterns
Usage example: StreamHeader
(example)
Class Table
Purpose
• To avoid sending lengthy class descriptor information by using short user-
defined sequences of bytes to represent certain classes
Uses
• Improve performance by potentially drastically decreasing message size
• Most effective when sending and receiving class hierarchies are known to
match
Writing serialized data to a file, reading it back by the same program later
on
Sending data between two identical nodes in a cluster
Usage example: ClassTable
(example)
Object Table
Purpose
• To allow certain object instances to be written as user-defined sequences of
bytes
Uses
• Remote references
• Space-efficient serialization of constants and singletons
Usage example: ObjectTable
(example)
Class Resolver
Purpose
• To allow hooks into the class descriptor reading and writing process
Uses
• To specify a ClassLoader policy to use upon unmarshalling
• To substitute class names on the fly
• To use shortened abbreviations for common class names
Equivalent to...
• ObjectInputStream.resolveClass() and resolveProxyClass()
• ObjectOutputStream.annotateClass() and annotateProxyClass()
• Other features not directly available in std. serialization
Usage example: ClassResolver
(example)
Object Resolver
Purpose
• Provide a means to substitute an object before marshalling or after it has
been unmarshalled.
Uses
• Sending a Serializable substitute for a non-Serializable object (substituting
on both ends)
• Interning Strings or similarly cached objects (on receiving end)
Equivalent to...
• ObjectInputStream.resolveObject()
• ObjectOutputStream.replaceObject()
Usage example: ObjectResolver
(example)
Externalizers
Purpose
• An Externalizer is an object which knows how to “externalize” a (possibly
non-Serializable) class
Uses
• Replace uses of java.io.Externalizable in order to:
Prevent public exposure of the “readExternal” and “writeExternal”
methods
Allow initialization of classes with final fields
• Provide the most efficient object serialization possible
• Integrate with other marshalling frameworks
Example: Protocol Buffers
Externalizers (cont.)
Two ways to define an Externalizer
• By specifying a ClassExternalizerFactory in the configuration
Returns an Externalizer given a Class argument
• Through use of the @Externalize annotation
Specifies a class with a no-arg constructor, which implements
Externalizer
The Externalizer is sent with the class information
• Therefore, the Externalizer itself needs to be Serializable, Externalizable, or
have an Externalizer of its own
• Using an ObjectTable entry will also suffice
Usage example: Externalizers
(example)
Creator
Purpose
• To provide an object creation strategy
Uses
• Used to create uninitialized objects
• Two implementations are included
ReflectiveCreator: uses reflection to attempt to locate and execute a no-
args constructor
SunReflectiveCreator: create an object using the internal JDK API
reserved for serialization object creation (may not be available on all
platforms)
Usage example: Creator
(example)
Questions
Thanks!
All demos are available here:
http://anonsvn.jboss.org/repos/sandbox/david.lloyd/marshalling-demos
More questions? Visit the #jboss IRC channel on irc.freenode.net!