我正在尝试拆分一个“@”分隔的字符串并通过相同的路线处理每个 token 。
camel-context.xml:
<split streaming="true">
<tokenize token="@"/>
<to uri="validateResubmitTransactionIdProcessor"/>
</split>
以下是处理器代码片段:
epublic class ValidateResubmitTransactionIdProcessor implements Processor {
public void process(Exchange exchng) throws Exception {
Object[] args = exchng.getIn().getBody(Object[].class);
}}
我收到以下异常:
eCaused by: org.apache.camel.InvalidPayloadException: No body available of type: org.apache.camel.Exchange but has value: 11484 of type: java.lang.String on: Message: 11484. Caused by: No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.Exchange with value 11484. Exchange[Message: 11484]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.Exchange with value 11484] at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101) at org.apache.camel.builder.ExpressionBuilder$35.evaluate(ExpressionBuilder.java:847) Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.Exchange with value 11484 at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:169) at org.apache.camel.core.osgi.OsgiTypeConverter.mandatoryConvertTo(OsgiTypeConverter.java:110) at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)nter
我不确定我是否以正确的方式使用分离器。另外,知道如何将 java.lang.String 转换为 Exchange。这似乎不受 Camel 支持。
最佳答案
Splitter EIP创建一种循环,并且您的处理器会为每个 token 调用。因此,交换的主体包含一个简单的字符串,而不是一个列表。
更新: 请参阅此示例:
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:start")
.split().tokenize("@").streaming()
.process(new MyProcessor())
;
}
});
context.start();
context.createProducerTemplate().sendBody("direct:start", "1@2@3");
您的处理器方法可能如下所示:
public void process(final Exchange exchange) throws Exception
{
System.out.println(exchange.getIn().getBody());
}
这将输出三行数字。
关于java - Apache Camel 拆分器返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400765/