Castor XML - Best practice Introduction Usage of ClassDescriptorResolver
Introduction
There's many users of Castor XML out there, who (want to) use Castor XML in
in high-volume applications. To fine-tune Castor for such environment, it is
necessary to understand many of the product features in detail and to be able to
balance their use according to the application needs. Even though many of these
features are detailed in various places, people have frequently been asking
for a 'best practise' document, a document that brings together these technical
topics (in one place) and presents them as a set of easy-to-use recipes.
Please be aware that this document is under construction, but still we
believe that - even when in its conception phase - it provides valuable
information to users of Castor XML.
Usage of ClassDescriptorResolver
Whilst the documentation seems to indicate that creating instances of
org.exolab.castor.xml.Marshaller and/or
org.exolab.castor.xml.Unmarshaller for the purpose of
XML data binding is easy to achieve at the API usage level, it still has an
impact on application performance as each instance creation involves
setup operations to be executed.
Whilst this is generally not an issue for one-off invocations, in a
multi-threaded, high volume use scenario this can be become a serious
issue. Internally, Castor uses a collection of Descriptor classes
to keep information about the Java entities to be marshalled and
unmarshalled. With every instance creation of (Un)Marshaller, this collection
will be built from scratch (again and again).
To avoid this initial configuration 'penalty', Castor allows you
to cache these Descriptor classes through its
org.exolab.castor.xml.ClassDescriptorResolver component, and
reuse this instance between (Un)Marshaller invocations.
First create an instance of ClassDescriptorResolver using the
following code fragment:
ClassDescriptorResolver classDescriptorResolver =
ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML);
MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller();
MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML);
classDescriptorResolver.setMappingLoader(mappingLoader); |
|
and then reuse this instance as shown below:
Unmarshaller unmarshaller = new Unmarshaller();
unmarshaller.setResolver((XMLClassDescriptorResolver) cdResolver);
unmarshaller.unmarshall(...); |
|
|