当前位置:  开发笔记 > Android > 正文

如何提高JAXB性能?

如何解决《如何提高JAXB性能?》经验,为你挑选了2个好方法。

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

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

public class GenericObjectXMLConverter {

  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;
    }
  }

}

bdoughan.. 19

性能和JAXB运行时类

你应该避免JAXBContext反复创建相同的内容. JAXBContext是线程安全的,应该重用以提高性能.

Marshaller/ Unmarshaller不是线程安全的,但可以快速创建.重用它们并不是什么大不了的事.


Nitin Vavdiy.. 5

您应该JAXBContext为每个bean类创建一个对象。这是我维护JAXBContext每个bean类的单例对象的版本。

public class MyJAXBUtil {
    public static final Map JAXB_MAP = new HashMap<>();
    public static JAXBContext getJAXBContext(Object object) {
        if(JAXB_MAP.get(object.getClass().getCanonicalName()) != null) {
            return JAXB_MAP.get(object.getClass().getCanonicalName());
        }else {
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
                JAXB_MAP.put(object.getClass().getCanonicalName(), jaxbContext);
                return jaxbContext;
            } catch (JAXBException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}

您可以getJAXBContext在需要JAXBContextbean类并Marshaller/Unmarshaller在本地创建时调用method 。



1> bdoughan..:

性能和JAXB运行时类

你应该避免JAXBContext反复创建相同的内容. JAXBContext是线程安全的,应该重用以提高性能.

Marshaller/ Unmarshaller不是线程安全的,但可以快速创建.重用它们并不是什么大不了的事.



2> Nitin Vavdiy..:

您应该JAXBContext为每个bean类创建一个对象。这是我维护JAXBContext每个bean类的单例对象的版本。

public class MyJAXBUtil {
    public static final Map JAXB_MAP = new HashMap<>();
    public static JAXBContext getJAXBContext(Object object) {
        if(JAXB_MAP.get(object.getClass().getCanonicalName()) != null) {
            return JAXB_MAP.get(object.getClass().getCanonicalName());
        }else {
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
                JAXB_MAP.put(object.getClass().getCanonicalName(), jaxbContext);
                return jaxbContext;
            } catch (JAXBException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}

您可以getJAXBContext在需要JAXBContextbean类并Marshaller/Unmarshaller在本地创建时调用method 。

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有