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

查看:246
本文介绍了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中,我正在填充Iddestination server path.现在,我需要定义一个新路径,该路径使用上载队列中的消息,并复制一个本地文件夹(基于先前路由中生成的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替换第一个"dynamic from",它使用轮询使用者和简单表达式来构建动态文件端点,从而丰富了消息.对于第二部分,正如已经提到的,动态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. 请参阅内容丰富器部分使用动态uris" http://camel.apache.org/content-enricher.html .

  1. See content enricher section "Using dynamic uris" 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天全站免登陆