如何将路由的实例变量的值传递给骆驼中的另一个bean

时间:2019-01-23 09:46:48

标签: apache-camel spring-camel

我有如下所示的示例路由,需要将outputStream的值发送到batchContentProcessor类以获取getBatchConent方法。如下所示。.帮助我如何将实例变量值发送到bean方法

2 个答案:

答案 0 :(得分:1)

我不确定您要达到什么目标,我怀疑这种理解将使我有更好的机会给您一个好的答案。

您的Java DSL无法正常工作,因为您试图将outputStream的“字符串”值添加到URL中,这不会做任何有用的事情-只会使URL无效。

阅读bean绑定说明以了解bean调用过程如何工作:http://camel.apache.org/bean-binding.html

由于您已经将流分配给stream标头,所以我认为您可以做到。

    from(DECRYPTION_ENDPOINT).routeId(CcsRoutes.DECRYPTION_ROUTE.name())
     .setHeader("stream", constant(outputStream)).log("About to write ${file:name}")
     .to("bean:batchContentProcessor?method=getBatchConent( ${header.stream} )");

答案 1 :(得分:0)

您应该能够将实例变量作为标头放到交换机上,然后从bean中的Exchange对象访问它。如果您仅将Exchange参数添加到bean方法中,如ColumnListItem所述,骆驼就会自动将Exchange绑定到bean。

例如:

class CamelBean {
    public String getBatchContent(String body, Exchange exchange) {
        ByteArrayOutputStream stream = exchange.getHeader("stream");
    }       
}