Hi Instead of synchrnoized logic which isn't ideal, we should use doStart / doStop to do this kind of logic. eg you can extend ServiceSupport and then in doStart / doStop you can load the schema, so its ready before use. So then you don't need any scynrhonized code and whatnot.
On Tue, Apr 8, 2014 at 4:07 PM, <ningji...@apache.org> wrote: > Repository: camel > Updated Branches: > refs/heads/master 5a74dfca3 -> 41d7eca93 > > > CAMEL-7349 cach the schema in JaxbDataFormate > > > Project: http://git-wip-us.apache.org/repos/asf/camel/repo > Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/41d7eca9 > Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/41d7eca9 > Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/41d7eca9 > > Branch: refs/heads/master > Commit: 41d7eca93409a71ce731dd9a54f8caa86ce78cfb > Parents: 5a74dfc > Author: Willem Jiang <willem.ji...@gmail.com> > Authored: Tue Apr 8 22:06:45 2014 +0800 > Committer: Willem Jiang <willem.ji...@gmail.com> > Committed: Tue Apr 8 22:06:45 2014 +0800 > > ---------------------------------------------------------------------- > .../camel/converter/jaxb/JaxbDataFormat.java | 70 ++++++++++++-------- > 1 file changed, 42 insertions(+), 28 deletions(-) > ---------------------------------------------------------------------- > > > http://git-wip-us.apache.org/repos/asf/camel/blob/41d7eca9/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java > ---------------------------------------------------------------------- > diff --git > a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java > > b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java > index 1e866e1..d34836d 100644 > --- > a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java > +++ > b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java > @@ -91,6 +91,7 @@ public class JaxbDataFormat extends ServiceSupport > implements DataFormat, CamelC > private JaxbNamespacePrefixMapper namespacePrefixMapper; > private JaxbXmlStreamWriterWrapper xmlStreamWriterWrapper; > private TypeConverter typeConverter; > + private volatile Schema cachedSchema; > > public JaxbDataFormat() { > } > @@ -367,47 +368,60 @@ public class JaxbDataFormat extends ServiceSupport > implements DataFormat, CamelC > } > } > > - protected Unmarshaller createUnmarshaller() throws JAXBException, > SAXException, FileNotFoundException, MalformedURLException { > + protected Unmarshaller createUnmarshaller() throws JAXBException, > SAXException, FileNotFoundException, > + MalformedURLException { > Unmarshaller unmarshaller = getContext().createUnmarshaller(); > if (schema != null) { > - SchemaFactory factory = getOrCreateSchemaFactory(); > - try { > - Schema newSchema = factory.newSchema(getSources()); > - unmarshaller.setSchema(newSchema); > - unmarshaller.setEventHandler(new ValidationEventHandler() { > - public boolean handleEvent(ValidationEvent event) { > - // stop unmarshalling if the event is an ERROR or > FATAL ERROR > - return event.getSeverity() == > ValidationEvent.WARNING; > - } > - }); > - } finally { > - returnSchemaFactory(factory); > - } > + unmarshaller.setSchema(getCachedSchema()); > + unmarshaller.setEventHandler(new ValidationEventHandler() { > + public boolean handleEvent(ValidationEvent event) { > + // stop unmarshalling if the event is an ERROR or FATAL > + // ERROR > + return event.getSeverity() == ValidationEvent.WARNING; > + } > + }); > + > } > > return unmarshaller; > } > > - protected Marshaller createMarshaller() throws JAXBException, > SAXException, FileNotFoundException, MalformedURLException { > + protected Marshaller createMarshaller() throws JAXBException, > SAXException, FileNotFoundException, > + MalformedURLException { > Marshaller marshaller = getContext().createMarshaller(); > if (schema != null) { > - SchemaFactory factory = getOrCreateSchemaFactory(); > - try { > - Schema newSchema = factory.newSchema(getSources()); > - marshaller.setSchema(newSchema); > - marshaller.setEventHandler(new ValidationEventHandler() { > - public boolean handleEvent(ValidationEvent event) { > - // stop marshalling if the event is an ERROR or > FATAL ERROR > - return event.getSeverity() == > ValidationEvent.WARNING; > - } > - }); > - } finally { > - returnSchemaFactory(factory); > - } > + marshaller.setSchema(getCachedSchema()); > + marshaller.setEventHandler(new ValidationEventHandler() { > + public boolean handleEvent(ValidationEvent event) { > + // stop marshalling if the event is an ERROR or FATAL > ERROR > + return event.getSeverity() == ValidationEvent.WARNING; > + } > + }); > + > } > > return marshaller; > } > + > + private Schema getCachedSchema() throws FileNotFoundException, > MalformedURLException, SAXException { > + if (cachedSchema == null) { > + synchronized (this) { > + if (cachedSchema == null) { > + cachedSchema = createSchema(getSources()); > + } > + } > + } > + return cachedSchema; > + } > + > + private Schema createSchema(Source[] sources) throws SAXException { > + SchemaFactory factory = getOrCreateSchemaFactory(); > + try { > + return factory.newSchema(sources); > + } finally { > + returnSchemaFactory(factory); > + } > + } > > private Source[] getSources() throws FileNotFoundException, > MalformedURLException { > // we support multiple schema by delimiting they by ',' > -- Claus Ibsen ----------------- Red Hat, Inc. Email: cib...@redhat.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen Make your Camel applications look hawt, try: http://hawt.io