如何提高JAXB性能? [英] How can I improve JAXB performance?

查看:75
本文介绍了如何提高JAXB性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的转换代码。当我们处理大数据时需要花费很长时间...调用方法几乎一百万次...我们可以清楚地看到它持有一段时间的线程。

Here is my conversion code. This is taking long time when we are dealing with large data... Calling the method almost a million times... We could clearly see that it is holding threads for a while.

请建议我提高性能的一些方法!

Please suggest me some ways to improve the performance!

public class GenericObjectXMLConverter<T> {

  private T t = null;
  private static JAXBContext jaxbContext =null;
  public GenericObjectXMLConverter() {

  }
  public GenericObjectXMLConverter(T obj){
    t = obj;
  }

  protected final Logger  log = Logger.getLogger(getClass());

  /**
   * Converts the java Object and into a xml string message type.
   * @param object the object to convert
   * @return String the converted xml message as string
   */
  public String objectToXMLMessage(T object) {
    StringWriter stringWriter = new StringWriter();
    //JAXBContext jaxbContext=null;
    try {
      jaxbContext = JAXBContext.newInstance(object.getClass());
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
      jaxbMarshaller.marshal(object, stringWriter);
    } catch (JAXBException e) {
      log.warn("JAXBException occured while converting the java object into xml string :"+e.getMessage());
    }
       /*if(log.isDebugEnabled())
         log.debug("xml string after conversion:"+stringWriter.toString());*/
       return stringWriter.toString();
  }

  /**
   * Converts a xml string message into a Java Object
   * @param string the string message to convert
   * @return Object the result as Java Object. If the message parameter is null then
     *  this method will simply return null.
   */
  @SuppressWarnings("unchecked")
  public T xmlToObject(String message) {
    if(message.equals("") || message.equals(" ") || message.length()==0){
      return null;
    }else{
      T object=null;
      try {
        jaxbContext = JAXBContext.newInstance(t.getClass());
        StringReader reader = new StringReader(message);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        object = (T)jaxbUnmarshaller.unmarshal(reader);
        } catch (JAXBException e) {
        log.warn("JAXBException occured while converting the xml string into a Java Object :"+e.getMessage());
        }
       /* if(log.isDebugEnabled()){
          log.debug("Java object after conversion:"+object.toString());
        }*/
      return object;
    }
  }

}


推荐答案

性能和JAXB运行时类


  • 您应该避免创建相同的 JAXBContext 一遍又一遍。 JAXBContext 是线程安全的,应该重复使用以提高性能。

  • Marshaller / Unmarshaller 不是线程安全的,但可以快速创建。重复使用它并不是一件大事。

  • You should avoid creating the same JAXBContext over and over. JAXBContext is thread safe and should be reused to improve performance.
  • Marshaller/Unmarshaller are not thread safe, but are quick to create. It's not as big a deal to reuse them.

这篇关于如何提高JAXB性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆