Apache Camel MultiCasting - InvalidPayloadException

时间:2016-05-01 18:34:15

标签: java apache-camel

我正在尝试将Multi-Casting企业集成模式(EIP)与Apache Camel一起使用,但遇到了大量的运行时异常。我认为主要错误如下。如果有人可以帮我解决这个问题,我想我可能会得到这个小程序。请参阅下面的错误和程序。

错误:Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: 100 of type: java.lang.Integer on: Message: 100. Caused by: No type converter available to convert from type: java.lang.Integer to the required type: java.io.InputStream with value 100. Exchange[Message: 100]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.lang.Integer to the required type: java.io.InputStream with value 100]

程序:

使用主要方法启动程序的Java类:

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.ProducerTemplate;

public class MainMulti {

    public static void main(String[] args) { 
        CamelContext c = new DefaultCamelContext();

        try { 
        c.addRoutes(new MultiRoute());
        c.start();
        ProducerTemplate pro = c.createProducerTemplate();

        pro.sendBody("direct:start", 100);
        pro.sendBody("direct:start", 500);

        Thread.sleep(5000);

        c.stop();

        } catch(Exception ex) { 
            System.out.println(ex);
            ex.printStackTrace();
        }


    }

}

Java DSL路由:

import org.apache.camel.builder.RouteBuilder;

public class MultiRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("direct:start")
                .multicast(new HighestAggregator())
                    .parallelProcessing()
                    .to("file:target/a", "file:target/b", "file:target/c")
                .end()
                .to("file:target/result");
    }

}

聚合Java bean:

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;

public class HighestAggregator implements AggregationStrategy {


    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        if (oldExchange == null) {
            return newExchange;
        }

        Integer currentQuote = oldExchange.getIn().getBody(Integer.class);
        Integer newQuote = newExchange.getIn().getBody(Integer.class);
        return currentQuote.compareTo(newQuote) > 0 ? oldExchange : newExchange;
    }
}

1 个答案:

答案 0 :(得分:2)

您正在写入多播中的文件,并且传入的邮件正文不是受支持的类型。您将输入作为整数发送。并且不支持开箱即用将单个整数写入文件。

因此,更改示例代码以将数据作为字符串发送,例如

pro.sendBody("direct:start", "100");
pro.sendBody("direct:start", "500");