Search code examples
jakarta-eexstream

XStream performance query


I am trying to get the xml conversion response for an object in my code as below

XStream xstream=new XStream();

xstream.processAnnotations(

new Class[]{SomeResponseBean.class,SomeSectionResponseBean.class});
xstream.toXML(objectForConversion);

Do we need to instantiate XStream instance for every conversion or we can have it a single instance and reuse ?

Our application has huge volume requests .


Solution

  • You don't need to instantiate XStream for every conversions since the XStream object is Thread safe. The initial instantiation/configuration of XStream is time-consuming, so it is a good thing to define the instance as static in your class :

       private static final XStream XSTREAM;
       static {
            XSTREAM = new XStream();
            XSTREAM.aliasPackage(...);
            //Other configurations
        }