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

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

问题描述

我有一个骆驼路由,它处理来自进程队列的消息并将其发送到上传队列.

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 版本.(存在先前版本的替代方案,但更复杂,看起来不优雅 - (例如消费者模板和收件人列表).

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天全站免登陆