Exception while pulling the message from service bus

493 Views Asked by At

I want to pull a message from service bus. The messages are nothing but the XML. I want to use the PEEK_LOCK option for achieving the objective.

For now, there is no message in the queue. My code run through a schedule every 30 seconds. If there are any message in the queue it will pull the message else it will print No more message.

ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
service.getQueue(queueName).getValue().setMaxSizeInMegabytes((long) 1);
BrokeredMessage message;
message = service.receiveQueueMessage(queueName).getValue();
StreamSource source = null;
ResponseEntity<String> response = null;
LOG.debug("Message: " + message);
// LOG.debug("message.getMessageId(): " + message.getMessageId());
// try {
if (message != null) {
    source = new StreamSource(message.getBody());
    StringWriter outWriter = new StringWriter();
    StreamResult result = new StreamResult(outWriter);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    StringBuffer sb = outWriter.getBuffer();
    String finalstring = sb.toString();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource src = new InputSource();
    src.setCharacterStream(new StringReader(finalstring));

    Document doc = builder.parse(src);
    String ecnNo = doc.getElementsByTagName(xmlECNNoTag).item(0).getTextContent();

    LOG.info(ecnNo + " Is pulled from queue "); 
    if (LOG.isDebugEnabled()) {
        LOG.info("XML Content: " + finalstring);
    }
}
service.deleteMessage(message);

If I remove the peek_lock code my code works fine. With the peek_lock I get this error:

javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 50; White spaces are required between publicId and systemId. at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:749) ~[na:1.8.0_66] at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351) ~[na:1.8.0_66] at com.jci.subscriber.service.PLMSubscriberMSServiceImpl.azureMessageSubscriber(PLMSubscriberMSServiceImpl.java:160) ~[classes/:na] at com.jci.subscriber.PLMSubscriberMSApplication.getXML(PLMSubscriberMSApplication.java:118) [classes/:na] at sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_66] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_66] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_66] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_66] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_66] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66] Caused by: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239) ~[na:1.8.0_66] at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:641) ~[na:1.8.0_66] at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737) ~[na:1.8.0_66] ... 15 common frames omitted

1

There are 1 best solutions below

0
On

According to the exception, per my experience, I think the issue was caused by validate the remote DTD schema of the message from service bus with peek_lock mode via DocumentBuilder.

There are some workaround way as below.

  1. Try to use method [DocumentBuilderFactory.setValidating(boolean validating)]1 to disable the validating for message as below.

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    
  2. Try to set a custom EntityResolver object for DocumentBuilder via method DocumentBuilder.setEntityResolver(EntityResolver er), please refer to the anwser of the SO thread Make DocumentBuilder.parse ignore DTD references .

builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (systemId.contains("foo.dtd")) { return new InputSource(new StringReader("")); } else { return null; } } });

  1. Try to use the third party libraries like dom4j to parse the message.

Hope it helps. Any concern, please feel free to let me know.