Permalink
Browse files

Cache JAXBContext, performance improvement of around 300% when runnin…

…g the test suite.

(4:42 down to 1:35).  [HHQ-3136]
  • Loading branch information...
1 parent 327d7cb commit 100c5c0fe11e2a04d1630cd9feebf5fd47110527 Ryan Morgan committed May 20, 2009
Showing with 13 additions and 6 deletions.
  1. +13 −6 src/org/hyperic/hq/hqapi1/XmlUtil.java
@@ -10,22 +10,29 @@
public class XmlUtil {
+ private static JAXBContext CTX;
+
+ static {
+ try {
+ CTX = JAXBContext.newInstance("org.hyperic.hq.hqapi1.types");
+ } catch (JAXBException e) {
+ // Not going to happen
+ System.out.println("Error initializing context: " + e.getMessage());
+ }
+ }
+
public static <T> T deserialize(Class<T> res, InputStream is)
throws JAXBException
{
- String pkg = res.getPackage().getName();
- JAXBContext jc = JAXBContext.newInstance(pkg);
- Unmarshaller u = jc.createUnmarshaller();
+ Unmarshaller u = CTX.createUnmarshaller();
u.setEventHandler(new DefaultValidationEventHandler());
return res.cast(u.unmarshal(is));
}
public static void serialize(Object o, OutputStream os, Boolean format)
throws JAXBException
{
- String pkg = o.getClass().getPackage().getName();
- JAXBContext jc = JAXBContext.newInstance(pkg);
- Marshaller m = jc.createMarshaller();
+ Marshaller m = CTX.createMarshaller();
m.setEventHandler(new DefaultValidationEventHandler());
m.setProperty("jaxb.formatted.output", format);
m.marshal(o, os);

0 comments on commit 100c5c0

Please sign in to comment.