我的初始要求是我无需再次调用Web服务。如果我在n次获得响应,我需要更新状态代码成功。如果n时间超过,那么我需要更新DB中的故障。为此我尝试了一些POC。我在验证代码的简单表达式
时得到了异常 <simple>${body.counter} < 4 </simple>
我附上了整个源代码。
public class MyBean {
public String appendCamel(String msg, int i) throws Exception {
System.out.println("The Value of i : " + i);
i = i + 1;
if (i < 3)
throw new Exception("Manually Throwed Error " + i);
else
return "Camel ".concat(msg);
}
public Message increment(Message m) {
m.setCounter(m.getCounter() + 1);
return m;
}
}
消息:
public class Message {
String text;
int counter;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
@Override
public String toString() {
return "Message [text=" + text + ", counter=" + counter + "]";
}
}
骆驼语境:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="hello">
<from uri="direct:hello" />
<camel:doTry>
<to uri="bean:myBean?method=appendCamel(${body.text},${body.counter})" />
<to uri="stream:out" />
<camel:doCatch>
<exception>java.lang.Exception</exception>
<camel:to uri="bean:myBean?method=increment(${header.originalvalue})" />
<camel:choice>
<camel:when>
<simple>${body.counter} < 4 </simple>
<camel:to uri="direct:hello" />
</camel:when>
<camel:otherwise>
<camel:to uri="stream:out" />
</camel:otherwise>
</camel:choice>
</camel:doCatch>
</camel:doTry>
</route>
</camelContext>
MyMain Class
public class CamelHelloWorldSpringExample {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CamelContext camelContext = SpringCamelContext.springCamelContext(
appContext, false);
try {
ProducerTemplate template = camelContext.createProducerTemplate();
camelContext.start();
Message message = new Message();
message.setCounter(0);
message.setText("Hello World");
template.sendBody("direct:hello", message);
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Stopping camel context");
camelContext.stop();
}
}
}
生成例外:
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 26 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 26; columnNumber: 33; The content of elements must consist of well-formed character data or markup.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.javacodegeeks.camel.CamelHelloWorldSpringExample.main(CamelHelloWorldSpringExample.java:11)
Caused by: org.xml.sax.SAXParseException; lineNumber: 26; columnNumber: 33; The content of elements must consist of well-formed character data or markup.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1388)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.startOfMarkup(XMLDocumentFragmentScannerImpl.java:2567)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2665)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:237)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
... 14 more