如何在 From() 中使用动态 URI [英] How to use a dynamic URI in From()

查看:46
本文介绍了如何在 From() 中使用动态 URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache Camel,它允许在 To() 中写入动态 URI,是否允许在 From() 中写入动态 URI.因为我需要根据配置调用多个 FTP 位置来下载文件,然后将其存储在数据库中.

As mentioned in Apache Camel, it allows to write dynamic URI in To(), does it allows to write dynamic URI in From(). Cause I need to call the multiple FTP locations to download the files on the basis of configuration which I am going to store it in database.

(FTPHost, FTPUser, FTPPassword, FTPSourceDir, FTPDestDir)

我将从数据库中读取这些配置,并在运行时将其动态传递给 Camel 路由.

I will read these configuration from the DB and will pass it to the Camel route dynamically at runtime.

示例:这是我必须动态编写的骆驼路线示例

Example: This is the camel route example that I have to write dynamically

<Route>
    <from uri="ftp://${ftpUser}@${ftpHost}:${ftpPort}/${FTPSourceDir}?password=${ftpPassword}&delete=true"/>
    <to uri="${ftpDestinationDir}"/>
</Route>

正如您在示例中看到的,我需要动态传递这些提到的参数.那么如何在From()中使用动态uri

As you see in example, I need to pass these mentioned parameters dynamically. So how to use dynamic uri in From()

推荐答案

可以从属性文件中读取,如下所示,

You can read it from property file as follows,

<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="location" value="classpath:/config/Test.properties"/>
  </bean> 

<Route>
    <from uri="ftp://{{ftpUser})@${{ftpHost}}:{{ftpPort}}/${{FTPSourceDir}}?password={{ftpPassword}}&delete=true"/>
    <to uri="{{ftpDestinationDir}}"/>
</Route>

ftpUser, ftpHost.... - 都是在 Test.properties 中声明的键

ftpUser, ftpHost.... - all are keys declared in Test.properties

如果您想从您的交易所动态获取这些变量,则不能像您在示例中提到的那样以常规方式进行.您必须按如下方式使用消费者模板,

If you want to get those variables from your exchange dynamically, you cannot do it in regular way as you mentioned in your example. You have to use consumer template as follows,

Exchange exchange = consumerTemplate.receive("ftp:"+url);
producerTemplate.send("direct:uploadFileFTP",exchange );

您必须从春豆或骆驼生产商那里做到这一点.消费者模板将从给定的组件中消费,该生产者模板将调用在您的camel-context.xml 中声明的直接组件

You have to do that from a spring bean or camel producer. Consumer template will consume from given component, and that producer template will invoke direct component declared in your camel-context.xml

注意:Consumer 和 Producer 模板有点贵.您可以将两者注入 spring 容器并让 spring 处理生命周期.

Note: Consumer and Producer templates are bit costly. you can inject both in spring container and let the spring handle the life cycle.

这篇关于如何在 From() 中使用动态 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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