Question

I have an application with 2500 possible java classes which can be returned by web services (done with CXF).
The problem is that creating a JAXB context for the 2500 classes takes a long time and lots of memory while on most scenarios only 200 classes will really be required. What I would want is a JAXB context which knows to do lazy handling only for the needed classes (delay all the code generation stuff to when the class is needed).
I know the current implementation doesn't support it but I would like to extend it.
If someone knows about possible hooks or a good starting point it will be great.
Another point is which engine will be easier yo extend, moxy or metro. Thanks in advance,
Avner

Was it helpful?

Solution

With EclipseLink JAXB (MOXy) you can use the SESSION_EVENT_LISTENER property to enable lazy initialization on the underlying metadata as follows:

import java.util.*;
import javax.xml.bind.JAXBContext;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.sessions.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.SESSION_EVENT_LISTENER, new SessionEventAdapter() {

            @Override
            public void preLogin(SessionEvent event) {
                for(Map.Entry<Class, ClassDescriptor> entry : event.getSession().getProject().getDescriptors().entrySet()) {
                    XMLDescriptor a;
                    ((XMLDescriptor) entry.getValue()).setLazilyInitialized(true);
                }
            }

        });
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);
    }

}

The JAXB reference implementation has a similar property which I can't find at the moment. If no one posts that as an answer you can post a question to their mailing list:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top