Reputation: 565
I've recently deployed my application on a glassfish installation running on Unix and I'm having serious performance issues with a section of the code. My application has to communicate with another system via XML, and I'm using Jaxb to do the marshalling/unmarshalling of the exchanged messages. Each communication requires one marshal and one unmarshal operation. When I run the application on my computer (Windows XP), the total amount of both operations is less than 2 seconds, but when I run the same application, with the same data, in Unix, the total amount is near 20 seconds, almost 10x more. I've searched thoroughly the server logs, looking for some clue, but couldn't find nothing useful.
Here is the code for both calls:
public static <T> String marshal(T transaction) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(transaction.getClass().getPackage().getName());
Marshaller u = jc.createMarshaller();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
u.setProperty("jaxb.encoding", "ISO-8859-1");
u.marshal(transaction, baos);
String xml = new String(baos.toByteArray());
baos.close();
return xml;
}
public static <T> T unmarshal(Class<T> docClass, String xml) throws JAXBException, IOException {
String packageName = docClass.getPackage().getName();
InputStream is = new ByteArrayInputStream(xml.getBytes());
JAXBContext jc = JAXBContext.newInstance(packageName);
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal(is);
is.close();
return (T) o;
}
Can someone help me with this issue?
Kind regards,
Carlos Ferreira
Upvotes: 2
Views: 2788
Reputation: 149017
The JAXBContext is threadsafe and can be shared. It does not need to be constantly created. This will improve performance.
Upvotes: 1