java - Apache Camel 拆分器返回字符串

标签 java apache-camel jbossfuse apache-servicemix

我正在尝试拆分一个“@”分隔的字符串并通过相同的路线处理每个 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/

相关文章:

java - OSGI 捆绑错误 - ojdbc6.jar

java - 为什么 Scanner 的 hasNext() 方法最初会为非空文本文件返回 false?

java - 向 jFreechart 添加图例

Apache Camel MDC 从 Body 添加字段

java - 如何使用 Apache Camel/Spring-boot 订阅持久主题?

java - 如何使用 JBoss Fuse (camel) 将 JSON 解码为多个项目?

jbossfuse - JBOSS FUSE - 社区版

java - Playframework DynamicForm 总是空值

java - 删除数组中的行和列列表后找到最大间隙

grails - ConsumerTemplate:指示接收端点是动态的,因此不应该对其进行缓存