title: How do I improve performance of application that uses the JAXBContext.newInstance operation?
body: <p>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.</p>
  
  <p>My initial proposal is to have a static initializer block that will initialize these two instances only once upon the class loading:</p>
  
  <pre><code>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(...);
              ...
      }
  
  }
  </code></pre>
  
  <p>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?</p>