Hi Werner,
Thanks for your fast response.

So here is the test case.
The method outputXML would be called with various mapFiles and objects to 
serialize.
So it could be possible that the call to XMLContext.addMapping() will be made 
multiple times for the same mapping:

Version 1 without Mapping cache)
void outputXML(Writer writer, String mapFile, Object object)
            throws DataHelperException {

        XMLContext context = getXMLContext();
        // marshal object to xml
        try {
              // load mapping
        Mapping mapping = getMapping(mapFile);
            // Don't care if context already knows this mapping
                context.addMapping(mapping);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setWriter(writer);
            marshaller.setMarshalAsDocument(false);

            marshaller.marshal(object);

            writer.flush();

        } catch ...
}

Version 2 with Mapping cache:
void outputXML(Writer writer, String mapFile, Object object)
            throws DataHelperException {

        XMLContext context = getXMLContext();
        // marshal object to xml
        try {
            // load mapping
            if (MappingCache.get(mapFile) == null) {
                Mapping mapping = getMapping(mapFile);
                context.addMapping(mapping);
                    MappingCache.put(mapFile, mapping);
            } // else the context already "knows" the mapping and we don't have 
to add it again

            Marshaller marshaller = context.createMarshaller();
            marshaller.setWriter(writer);
            marshaller.setMarshalAsDocument(false);

            marshaller.marshal(object);

            writer.flush();

        } catch ...
}

I will profile both versions and verify,
but would be interested in your opinion, too.

Ciao,
Andreas

-----Ursprüngliche Nachricht-----
Von: Werner Guttmann [mailto:[email protected]] 
Gesendet: Donnerstag, 9. April 2009 22:57
An: [email protected]
Betreff: Re: [castor-user] XMLContext and caching of mappings

Hi Andreas,

Furtner, Andreas wrote:
> Hello,
> 
> I have a question regarding the usage of Castor in a multi-threaded, 
> high volume use scenario like explained in 
> http://www.castor.org/xml-best-practice.html#Performance-Consideration
> s
> 
> We use Castor with a lot of mapping (XML) files - one for each class 
> we want to serialize.
> 
> In such a scenario, should I use
> 1) ONE XMLContext with all the mapping files added to it, or is it 
> better to
> 2) use another XMLContext with each mapping file?
In my opinion, you should use one XMLContext() instance only.

> As I read in other postings in the list (1) should be the prefered 
> way, but because it would be unconvenient to initialize XMLContext 
> with all possible mappings, we use a lazy on-demand approach. So:
> 
> Is it safe to call XMLContext.addMapping() with the same mapping twice?
It should be, as far as I remember.

> Will XMLContext then cache the mappings in calls to
> XMLContext.addMapping() and return fast? 
> 
> Or should we cache used mappings ourselves and prevent another call to 
> XMLContext.addMapping()?
Can you provide me with a *small* but complete test case that shows the problem 
at hand ?

> We noticed, that the overhead of initialization and setting mappings 
> is much bigger than the actual (de)serialization work afterwards. So 
> I'd like to keep it as low as possible.
> 
> Thanks a lot,
> Andreas Furtner
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
> 
>     http://xircles.codehaus.org/manage_email
> 
> 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to