简体   繁体   English

Apache Camel-动态地从端点构建到端点

[英]Apache Camel - Build both from and to endpoints dynamically

I have a camel route which processes a message from a process queue and sends it to upload queue . 我有一条骆驼路线,该路线可以处理来自process queue的消息,并将其发送到upload queue

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

In impl.process I am populating an Id and destination server path . impl.process我正在填充Iddestination 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) 现在,我需要定义一条新路径,该路径将使用上载队列中的消息,并复制一个本地文件夹(基于先前路由中生成的ID),然后将其上传到ftp服务器的目标文件夹中(这也填充在先前路由中)

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});

Adding a dynamic to endpoint in Camel (as noted in the comment) can be done with the .toD() which is described on this page on the Camel site. 可以使用.toD() 在Camel网站上为端点添加动态(如注释中所述) ,此页面上对此进行了介绍 I don't know of any fromD() equivalent. 我不知道任何fromD()等效项。 However, you could add a dynamic route by calling the addRoutes method on the CamelContext . 但是,你可以通过调用添加动态路由addRoutes的方法CamelContext This is described on this page on the Camel site. 在Camel网站上的本页中进行了描述

Expanding slightly on the example from the Camel site here is something that should get you heading in the right direction. 在Camel网站的示例中稍微扩展一下,应该可以使您朝正确的方向前进。

public void process(Exchange exchange) throws Exception {
   String idFromExchangeObject = ...
   String serverIpFromExchangeObject = ...
   String pathFromExchangeObject = ...

   exchange.getContext().addRoutes(new RouteBuilder() {
       public void configure() {
           from("file:basePath/"+ idFromExchangeObject)
            .to("ftp:"+ serverIpFromExchangeObject +"/"+pathFromExchangeObject);
       }
   });
}

There may be other options in Camel as well since this framework has an amazing number of EIP and capabilities. 由于此框架具有数量惊人的EIP和功能,因此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 - ( eg consumerTemplate & recipientList). 我认为您的情况还有更好的选择,可以认为您使用的骆驼版本比2.16更新。(存在以前版本的替代版本,但更复杂,看起来也不优雅-(例如,ConsumerTemplate和收件人列表) 。

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. 您可以用pollEnrich替换第一个“ dynamic from”,它使用轮询使用者和简单表达式来丰富消息,以构建动态文件端点。 For the second part, as already mentioned, a dynamic uri .toD will do the job. 对于第二部分,正如已经提到的,动态uri.toD将完成工作。 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. See content enricher section "Using dynamic uris" http://camel.apache.org/content-enricher.html . 请参阅“内容丰富器”部分“使用动态uri” http://camel.apache.org/content-enricher.html

    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. 您将需要一种聚合策略,以将原始交换与资源交换结合在一起,以确保在富集之后,头serverIpFromExchangeObject,pathFromExchangeObject将包含在聚合交换中。 If you don't include the custom strategy then Camel will by default use the body obtained from the resource. 如果您不包括自定义策略,那么Camel将默认使用从资源中获取的正文。 Have a look at the ExampleAggregationStrategy example in content-enricher.html to see how this works. 请查看content-enricher.html中的ExampleAggregationStrategy示例,以了解其工作原理。

  2. For the .toD() have a look at http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html 有关.toD()的信息,请参见http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM