title: Reducing CPU usage for MDB-JMS-JAXB-Hibernate combo application
body: <p>So I have an EAR that reads messages from a <strong>JMS</strong> queue. As per requirement the messages are XML data. I read the data from queue, unmarshall it using JAXB and save the xml data into database. But this whole process takes <strong>90-95% usage</strong> of my CPU. I followed up the changes suggested in this <a href="http://javaeesupportpatterns.blogspot.in/2011/09/jaxbcontext-performance-problem-case.html" rel="nofollow">link</a> so as to reduce my CPU usage. But even after implementing those it still uses 90% of mu CPU till all messages in queue are processed.</p>
  
  <p><strong>Note</strong> that I have created a separate hibernatePersistance.jar which is where I have implemented the JAXB unmarshalling. The hibernate.cfg.xml file is also inside the jar.
  Is this what I think might be the problems for high usage?</p>
  
  <p>Does having a separate jar for persistence establishes and closes database connections each time the save() function is called which might be causing high CPU usage or its something else?</p>
  
  <p>Here is the code</p>
  
  <pre><code>**MDB Listener Class**
  
  /**
       * @see MessageListener#onMessage(Message)
       */
      public void onMessage(Message message) {
          try {
              if (message instanceof BytesMessage) {
                  BytesMessage bytesMessage = (BytesMessage) message;
                  StringBuffer buffer = new StringBuffer();
                  for (int i = 0; i &lt; (int) bytesMessage.getBodyLength(); i++) {
                      buffer.append((char) bytesMessage.readByte());
                  }
                  String cbeXml = buffer.toString().trim();
                  persistAuditMessage(cbeXml);
              } else if (message instanceof TextMessage) {
                  TextMessage textMessage = (TextMessage) message;
                  persistAuditMessage(textMessage.getText());
              }
  
          } catch (JMSException e) {
              e.printStackTrace();
          } catch (Exception jmse) {
              jmse.printStackTrace();
          }
      }
  
      private void persistAuditMessage(String auditMessage) {
          // for without Session Bean Call
          count++;
          if (auditMessage != null &amp;&amp; auditMessage.trim().length() != 0) {
              MarshalImpl impl = new MarshalImpl();
              impl.saveCbeXml(auditMessage);
  
          }
  
  
      }
  </code></pre>
  
  <p><strong>Class inside Persistence JAR</strong></p>
  
  <pre><code>public class MarshalImpl extends AbstractHibernateLayar&lt;BaseEvent, Long&gt; implements Marshal {
  
      private static Logger logger = LoggerFactory.getLogger(MarshalImpl.class);
      private static JAXBContext context = null;
  
      public static synchronized JAXBContext createJAXBContext() throws JAXBException {
          if (context == null) {
              System.out.println("Creating jaxb context");
              context = JAXBContext.newInstance(BaseEvents.class.getPackage().getName());
          }
          return context;
      }
  
  
  
  
      public MarshalImpl() {
          super();
      }
  
  
      @Override
      public void saveCbeXml(String auditMessage) {
          try {
  
  
  
              Unmarshaller unmarshaller = createJAXBContext().createUnmarshaller();
          @SuppressWarnings("unchecked")
          JAXBElement&lt;BaseEvents&gt; root = (JAXBElement&lt;BaseEvents&gt;) unmarshaller.unmarshal(new StreamSource(new StringReader(auditMessage)));
          List&lt;BaseEvent&gt; baseEvent = root.getValue().getCommonBaseEvent();
              // Persist BaseEvent one by one
              for (BaseEvent event : baseEvent) {
                  event.setSequenceNumber(new Long(1));// Indicate Working Flag
                  event.setCreationTimeItem(new Date());
                  save(event);
  
              }
  
          } catch (Exception e) {
              logger.error("MarshalImpl::saveCbeXml::CBE Records not inserted!!::" + e.getMessage(), e);
          } finally {
              System.gc();
  
          }
      }
  </code></pre>