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

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

问题描述

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

 (FTPHost,FTPUser,FTPPassword,FTPSourceDir,FTPDestDir)

我会读这些配置,并在运行时动态传递给Camel路由。

我必须动态写入

 <路径> 
< /路线>

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

解决方案

你可以从属性文件中读取它, / p>

 < bean id =bridgePropertyPlaceholderclass =org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer> 
< property name =locationvalue =classpath:/config/Test.properties/>
< / bean>

<路线>
< to uri ={{ftpDestinationDir}}/>
< /路线>

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



如果您想要动态地从您的交易所获取这些变量,则无法按照您的示例中提到的常规方式执行此操作。您必须按如下方式使用消费者模板,

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

你必须从spring bean或骆驼制作者那里做到这一点。消费者模板将从给定组件消耗,并且该生产者模板将调用在camel-context.xml中声明的直接组件。注意:消费者和生产者模板有点昂贵。你可以在弹簧容器中注入这两个元素,并让弹簧处理生命周期。

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)

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>

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

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

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