具有动态路径的Apache Camel文件组件

时间:2017-05-10 08:11:29

标签: java apache-camel

我正在尝试在camel文件组件中设置动态路径以避免特定于平台的路径。但是camel不允许这样做,因为它不希望在目录路径中使用$。

我要做的是设置一个VM param说file.home,然后将其用于我的文件组件,如

  

文件:\ $ {file.home} \ TYPE1

这将允许我直接消除平台特定路径。 我尝试将其外化到属性文件中,但是然后Spring不理解用于例如骆驼的动态语言。 $ {in.header.abc}

有人可以帮助我实现这一目标。

6 个答案:

答案 0 :(得分:0)

以下是有关在camel和/或spring xml中使用属性的一些详细信息:http://camel.apache.org/using-propertyplaceholder.html

答案 1 :(得分:0)

您可以使用动态uri,但仅限to个端点(使用特定组件)。您不能将其用作from

在那里,您可以找到解释如何使用toD(来自Camel 2.16)或recipientListHow to use dynamic URI in to

但正如我所说 - 只有在to中使用它的可能性。在from中无法使用它。作为一种解决方法,您必须为您希望使用的每个位置编写路线。您也可以使用autoCreate=false选项不自动创建其他目录,因为例如没有autoCreate=false选项的Linux路径:/home/user/test将在Windows c:\home\user\test中创建目录结构

答案 2 :(得分:0)

自Camel 2.16起

我们可以使用

.from("file://folder")
.toD("file://folder/${file:onlyname}")

答案 3 :(得分:0)

这些答案不正确。如果使用BridgePropertyPlaceholderConfigurer和PropertiesComponent,则可以在任何地方使用动态值。

<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="properties">
        <value>
...normal property syntax name=value - use this in test definitions
        </value>
    </property>
</bean>

或者在实际应用中使用类似的东西

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

e.g。     

    <route id="DummyRoute">
        <from uri="file:{{dummy.int.dir}}?delay={{poll.delay}}&amp;initialDelay={{initial.delay}}&amp;{{readlockChanged}}&amp;move={{root}}/{{dummy.arch.dir}}/{{archive.dir}}&amp;moveFailed={{error.dir}}&amp;scheduledExecutorService=#scheduledExecutorService" />
            <to uri="file:{{root}}/{{dummy.int.destination.dir}}" />
    </route>

Camel的更高版本有一个技巧:使用$simple{file.path}而不是${file.path},因此Spring不会剥离您的${}并将裸文件路径传递给Camel。例如。转移&#39;来自&#39; uri可能是这样的:

move=archive/$simple{date:now:yyyyMMdd}/$simple{file:name}

http://camel.apache.org/how-do-i-use-spring-property-placeholder-with-camel-xml.html

http://camel.apache.org/using-propertyplaceholder.html

答案 4 :(得分:0)

根据Camel File Component

  

Camel仅支持配置了起始目录的端点。所以   directoryName必须是目录。如果你想吃一个   仅文件,您可以使用fileName选项,例如,通过设置   fileName =文件名。另外,起始目录不得包含   $ {}占位符的动态表达式。再次使用文件名   选项以指定文件名的动态部分

因此,您可以执行以下操作:

from(...).to("file://?fileName=${somePathAndSomeFile}").to(...)

答案 5 :(得分:0)

此主题中的某些评论/答案具有误导性,因为可以根据要求将“ from”端点URI的值设置为具有从属性文件中获取的目录的值。

将propertyPlaceholder元素放置在camelContext下,并确保可以在类路径中找到属性文件

<propertyPlaceholder location="dir.properties" />
<from id="_fromInputFolder" uri="file:{{fromDir}}?"/>