Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I improve performance of application that uses the JAXBContext.newInstance operation?

Tags:

java

xml

jaxb

I use the JAXBContext.newInstance operation in my JBoss based web application. This operation, as I understand, is very heavyweight. I only require two unique instances of the Marshaller class.

My initial proposal is to have a static initializer block that will initialize these two instances only once upon the class loading:

public class MyWebApp {
    private static Marshaller requestMarshaller;
    private static Marshaller responseMarshaller;

    static {
        try {
            // one time instance creation
            requestMarshaller = JAXBContext.newInstance(Request.class).createMarshaller();
            responseMarshaller = JAXBContext.newInstance(Response.class).createMarshaller();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    private void doSomething() {
            requestMarshaller.marshall(...);
            responseMarshaller.marshall(...);
            ...
    }

}

If this is a reasonable solution then I guess I'll have answered my own question, but I would like to know if this is the correct way to do this?

like image 379
ryan Avatar asked May 18 '11 11:05

ryan


People also ask

What is JAXBContext newInstance?

The JAXBContext. newInstance() method accepts jaxb. index files in the same way that it accepts the ObjectFactory class. Using a jaxb. index file requires you to evaluate all the instances in the trace file that indicate a package does not contain an ObjectFactory class, and create the appropriate jaxb.

Is JAXBContext thread safe?

The rules for JAXB in a multi-threaded environment are very simple: you can share the JAXBContext object among threads. Doing so will also improve performance, as the construction of the context may be expensive. All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared.

What is ObjectFactory in JAXB?

jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.


1 Answers

A JAXB implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc) typically initializes its metadata during the JAXBContext.newInstance call. All OXM tools need to initialize mapping metadata at some point and try to minimize the cost of this operation. Since it is impossible to do it with zero cost, it is best to only do it once. Instances of JAXBContext are thread safe, so yes you only need to create it once.

From the JAXB 2.2 Specification, Section 4.2 JAXB Context:

To avoid the overhead involved in creating a JAXBContext instance, a JAXB application is encouraged to reuse a JAXBContext instance. An implementation of abstract class JAXBContext is required to be thread-safe, thus, multiple threads in an application can share the same JAXBContext instance.

Instances of Marshaller and Unmarshaller are not thread safe and must not be shared among threads, they are lightweight to create.

like image 130
bdoughan Avatar answered Oct 06 '22 00:10

bdoughan