gpt4 book ai didi

apache - 如何在 From() 中使用动态 URI

转载 作者:行者123 更新时间:2023-12-02 03:12:41 25 4
gpt4 key购买 nike

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

(FTPHost, FTPUser, FTPPassword, FTPSourceDir, FTPDestDir)

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

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

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

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

最佳答案

您可以按如下方式从属性文件中读取它,

<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 中声明的键

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

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

你必须从 Spring Bean 或 Camel 生产商那里做到这一点。消费者模板将从给定的组件中消费,并且该生产者模板将调用在camel-context.xml中声明的直接组件

注意:消费者和生产者模板的成本有点高。您可以将两者注入(inject)到 spring 容器中,并让 spring 处理生命周期。

关于apache - 如何在 From() 中使用动态 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32368961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com