Apache Camel - 动态构建端点和端点 [英] Apache Camel - Build both from and to endpoints dynamically

查看:31
本文介绍了Apache Camel - 动态构建端点和端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个骆驼路由,它处理来自 process queue 的消息并将其发送到 upload queue.

I have a camel route which processes a message from a process queue and sends it to upload queue.

from("activemq:queue:process" ).routeId("activemq_processqueue")
        .process(exchange -> {
            SomeImpl impl = new SomeImpl();
            impl.process(exchange);
        })
        .to(ExchangePattern.InOnly, "activemq:queue:upload");

impl.process 中,我正在填充 Id目标服务器路径.现在我需要定义一个使用上传队列中消息的新路由,并复制一个本地文件夹(基于先前路由中生成的 Id)并将其上传到作为 ftp 服务器的目标文件夹(这也填充在先前的路由中)

In impl.process I am populating an Id and destination server path. Now I need to define a new route which consumes messages from upload queue ,and copy a local folder (based on Id generated in previous route) and upload it to destination folder which is an ftp server (this is also populated in previous route)

那么如何设计一条从端点到端点都是动态的新路由,如下所示?

So how to design a new route where both from and to endpoints are dynamic which would look something like below ?

from("activemq:queue:upload" )
        .from("file:basePath/"+{idFromExchangeObject})
    .to("ftp:"+{serverIpFromExchangeObject}+"/"+{pathFromExchangeObject});

推荐答案

我认为您的情况有更好的替代方案,当然您使用的是比 2.16 更新的 Camel 版本.(存在以前版本的替代方案,但它们更复杂,看起来也不优雅——(例如,consumerTemplate 和接收者列表).

I think there is a better alternative for your case, taking as granted that you are using a Camel version newer than 2.16.(alternatives for a previous version exist but the are more complicated and don't look elegant - ( e.g consumerTemplate & recipientList).

您可以将第一个动态来源"替换为 pollEnrich,它使用轮询消费者和简单表达式来丰富消息以构建动态文件端点.对于第二部分,如前所述,动态 uri .toD 将完成这项工作.所以你的路线看起来像这样:

You can replace the first "dynamic from" with pollEnrich which enriches the message using a polling consumer and simple expression to build the dynamic file endpoint. For the second part, as already mentioned, a dynamic uri .toD will do the job. So your route would look like this:

 from("activemq:queue:upload" )
    .pollEnrich().simple("file:basePath/${header.idFromExchangeObject})
    .aggregationStrategy(new ExampleAggregationStrategy()) // * see explanation 
    .timeout(2000) // the timeout is optional but recommended
    .toD("ftp:${header.serverIpFromExchangeObject}/${header.pathFromExchangeObject}") 

  1. 参见内容丰富器部分使用动态 uri"http://camel.apache.org/content-enricher.html .

您将需要一个聚合策略,将原始交换与资源交换相结合,以确保标头 serverIpFromExchangeObject、pathFromExchangeObject 将包含在丰富后的聚合交换中.如果您不包含自定义策略,那么 Camel 默认使用从资源中获取的主体.查看 content-enricher.html 中的 ExampleAggregationStrategy 示例以了解其工作原理.

You will need an aggregation strategy, to combine the original exchange with the resource exchange in order to make sure that the headers serverIpFromExchangeObject, pathFromExchangeObject will be included in the aggregated exchange after the enrichment. If you don't include the custom strategy then Camel will by default use the body obtained from the resource. Have a look at the ExampleAggregationStrategy example in content-enricher.html to see how this works.

这篇关于Apache Camel - 动态构建端点和端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆