Create new JAXB Marshaller instance in static method… is it ThreadSafe?

好久不见. 提交于 2020-01-11 13:38:05

问题


I have implemented a util class with the following static method:

public static String objToXml(JAXBContext jaxbContext, ClassA obj) throws Exception{
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    StringWriter writer = new StringWriter();
    marshaller.marshal(obj, writer);
    return writer.toString();
}

My understanding is that JAXB Marshaller and StringWriter are both thread safe ... but I want to double confirm if I implement a static method like this.. and have multiple threads calling objToXml method concurrently, would there be any potential issue?


回答1:


Yes you must create a new Marshaller each time around if you are planning to use them multi-threaded.

See Unofficial JAXB Guide

The JAXBContext class is thread safe, but the Marshaller, Unmarshaller, and Validator classes are not thread safe.



来源:https://stackoverflow.com/questions/36062415/create-new-jaxb-marshaller-instance-in-static-method-is-it-threadsafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!