From 3112988aa680ed0f16054f1891a97b790d67e791 Mon Sep 17 00:00:00 2001 From: chaks03 Date: Wed, 14 Jul 2021 23:39:47 +0530 Subject: [PATCH] Speed: serialization; deserialization --- dependency-reduced-pom.xml | 2 +- .../java/messagerosa/core/model/XMessage.java | 23 ++++++++++++++----- .../java/messagerosa/xml/XMessageParser.java | 15 ++++++++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 9214461..b3c9829 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -7,7 +7,7 @@ pom.xml 4.0.0 - com.samagra + com.uci message-rosa message-rosa 0.0.1-SNAPSHOT diff --git a/src/main/java/messagerosa/core/model/XMessage.java b/src/main/java/messagerosa/core/model/XMessage.java index 518b7de..284d63e 100644 --- a/src/main/java/messagerosa/core/model/XMessage.java +++ b/src/main/java/messagerosa/core/model/XMessage.java @@ -86,13 +86,24 @@ public enum MessageType { private XMessageThread thread; private XMessagePayload payload; + private static JAXBContext context; + private static Marshaller marshaller; + + static { + try { + context = JAXBContext.newInstance(XMessage.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + } catch (JAXBException e) { + e.printStackTrace(); + } + } + + public String toXML() throws JAXBException { - JAXBContext context = JAXBContext.newInstance(XMessage.class); - Marshaller marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - StringWriter sw = new StringWriter(); - marshaller.marshal(this, sw); - return sw.toString(); + StringWriter stringWriter = new StringWriter(); + marshaller.marshal(this, stringWriter); + return stringWriter.toString(); } public void completeTransform() { diff --git a/src/main/java/messagerosa/xml/XMessageParser.java b/src/main/java/messagerosa/xml/XMessageParser.java index 4962105..20bbf01 100644 --- a/src/main/java/messagerosa/xml/XMessageParser.java +++ b/src/main/java/messagerosa/xml/XMessageParser.java @@ -19,9 +19,20 @@ */ public class XMessageParser { + + public static JAXBContext context; + public static Unmarshaller jaxbUnmarshaller; + + static { + try { + context = JAXBContext.newInstance(XMessage.class); + jaxbUnmarshaller = context.createUnmarshaller(); + } catch (JAXBException e) { + e.printStackTrace(); + } + } + public static XMessage parse(InputStream stream) throws JAXBException { - JAXBContext context = JAXBContext.newInstance(XMessage.class); - Unmarshaller jaxbUnmarshaller = context.createUnmarshaller(); return (XMessage) jaxbUnmarshaller.unmarshal(stream); } }