title: Apache Camel : ValidatingProcessor : XSD validation
body: <hr>
  
  <p>I'm trying to create a bundle and run it inside ServiceMix.
  I've encoutered a problem, any help is appreciated.</p>
  
  <h2>What I'm trying to do:</h2>
  
  <p>Produce SOAP messages from a CXF Endpoint and validate them against a XSD schema using Apache ServiceMix 5.0.0 (Camel 2.12.3).</p>
  
  <h2>What I've done:</h2>
  
  <p>Implemented a route that produces messages from a CXF endpoint and then validates them.</p>
  
  <p>The route's <code>configure</code> method:</p>
  
  <pre><code>private RouteBuilder getInputRoute() {
  
      SoapValidatingProcessor soapValidatingProcessor = new SoapValidatingProcessor();
  
      inputRoute = new RouteBuilder() {
  
        @Override
        public void configure() throws Exception {
  
          from(cxfEndpointInId())
            .convertBodyTo(java.lang.String.class, "UTF-8")
            .onException(org.apache.camel.ValidationException.class)
              .log(LoggingLevel.INFO, LOG_NAME, "Invalid message received!")
              .handled(true)
              .stop()
              .end()
            .bean(soapValidatingProcessor).id("SoapHeaderValidatingProcessor")
            .to("browse:foo")
  
  }
  </code></pre>
  
  <p>Created a validating processor:</p>
  
  <pre><code>public class SoapValidatingProcessor {
  
    private final String SCHEMA = "schema.xsd";  
  
    public SoapValidatingProcessor() {
  
      validatingProcessor = new ValidatingProcessor();
      validatingProcessor.setFailOnNullHeader(false);
      validatingProcessor.setFailOnNullBody(false);
  
    }
  
    @Handler
    public void validate(Exchange exchange) throws Exception {
  
      Resource validationSchema = context.getApplicationContext().getResource(SCHEMA);
      validatingProcessor.setSchemaUrl(validationSchema.getURL());
      validatingProcessor.loadSchema();
  
      /* Creating a new SchemaFactory instance */
      SchemaFactory xmlSchema = SchemaFactory
          .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   
  
      validatingProcessor.setSchemaFactory(xmlSchema);    
      validatingProcessor.process(exchange);
  
    }
  
  }
  </code></pre>
  
  <h2>Problem:</h2>
  
  <p>The Apache Camel ValidatingProcessor [1] does not behaves well in multi-threating. I'm sending SOAP messages at aprox. 20ms interval and I'm getting the following exception. If I increase the sending interval to something above 200ms, it's all good.</p>
  
  <pre><code>2014-07-11 17:11:47,404 | WARN  | ult-workqueue-18 | PhaseInterceptorChain            | ?                                   ? | 129 - org.ap
  ache.cxf.cxf-api - 2.7.10 | Application {http://ws.service}WSImplService#{http://foo.bar}Update has thrown exception, unwinding now
  org.apache.cxf.interceptor.Fault: FWK005 parse may not be called while parsing.
          at org.apache.camel.component.cxf.CxfConsumer$1.checkFailure(CxfConsumer.java:228)[181:org.apache.camel.camel-cxf:2.12.3]
          at org.apache.camel.component.cxf.CxfConsumer$1.setResponseBack(CxfConsumer.java:206)[181:org.apache.camel.camel-cxf:2.12.3]
          at org.apache.camel.component.cxf.CxfConsumer$1.syncInvoke(CxfConsumer.java:140)[181:org.apache.camel.camel-cxf:2.12.3]
          at org.apache.camel.component.cxf.CxfConsumer$1.invoke(CxfConsumer.java:75)[181:org.apache.camel.camel-cxf:2.12.3]
          at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)[129:org.apache.cxf.cxf-api:2.7.10]
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_45]
          at java.util.concurrent.FutureTask.run(FutureTask.java:262)[:1.7.0_45]
          at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)[129:org.apache.cxf.cxf-api:2.7.10]
          at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:107)[129:org.apache.cxf.cxf-api
  :2.7.10]
          at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)[129:org.apache.cxf.cxf-api:2.7.10]
          at org.apache.cxf.phase.PhaseInterceptorChain.resume(PhaseInterceptorChain.java:242)[129:org.apache.cxf.cxf-api:2.7.10]
          at org.apache.cxf.interceptor.OneWayProcessorInterceptor$1.run(OneWayProcessorInterceptor.java:144)[129:org.apache.cxf.cxf-api:2.7.1
  0]
          at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$3.run(AutomaticWorkQueueImpl.java:428)[129:org.apache.cxf.cxf-api:2.7.10]
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_45]
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_45]
          at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$AWQThreadFactory$1.run(AutomaticWorkQueueImpl.java:353)[129:org.apache.cxf.cxf-ap
  i:2.7.10]
          at java.lang.Thread.run(Thread.java:744)[:1.7.0_45]
  Caused by: org.xml.sax.SAXException: FWK005 parse may not be called while parsing.
          at org.apache.xerces.jaxp.validation.Util.toSAXException(Unknown Source)[:]
  </code></pre>
  
  <p>The only way I'm not getting that exception is if the <code>validate</code> method is <code>synchronized</code> but I don't want that.</p>
  
  <h2>What I've found:</h2>
  
  <p>I see here [2] that the bug was resolved. I've tried to create a new SchemaFactory [3] instance for each message but still no luck. Any ideeas why I'm still getting the error? What I'm doing wrong?</p>
  
  <p>[1] <a href="http://camel.apache.org/maven/camel-2.8.0/camel-core/apidocs/org/apache/camel/processor/validation/ValidatingProcessor.html" rel="nofollow">http://camel.apache.org/maven/camel-2.8.0/camel-core/apidocs/org/apache/camel/processor/validation/ValidatingProcessor.html</a></p>
  
  <p>[2] <a href="https://issues.apache.org/jira/browse/CAMEL-6630" rel="nofollow">https://issues.apache.org/jira/browse/CAMEL-6630</a></p>
  
  <p>[3] <a href="http://docs.oracle.com/javase/6/docs/api/javax/xml/validation/SchemaFactory.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/javax/xml/validation/SchemaFactory.html</a></p>
  
  <p>Thanks!</p>