[英]Apache Camel file component with dynamic path
I am trying to set dynamic path in camel file component to avoid platform specific paths. 我试图在骆驼文件组件中设置动态路径,以避免平台特定的路径。 But camel is not allowing this as it doesn't expect $ in directory path. 但是骆驼不允许这样做,因为它不希望目录路径中有$。
What I am trying to do is to set a VM param say file.home and then use this into my file component like 我想要做的是设置一个虚拟机参数,例如file.home,然后将其用于我的文件组件中,例如
file:\\${file.home}\\type1 文件:\\ $ {file.home} \\ TYPE1
This will allow me to eliminate the platform specific path directly. 这将使我直接消除平台特定的路径。 I tried externalizing this into property file but then Spring doesn't understand the camel specific dynamic language for eg ${in.header.abc} 我尝试将其外部化为属性文件,但是Spring不了解骆驼特定的动态语言,例如$ {in.header.abc}
Can someone help me out in achieving this. 有人可以帮助我实现这一目标。
以下是有关在骆驼和/或spring xml中使用属性的一些详细信息: http : //camel.apache.org/using-propertyplaceholder.html
You can use dynamic uri but only in to
endpoints (using specific components). 您可以使用动态uri,但只能to
端点(使用特定组件)。 You can't use it as from
. 您不能from
使用它。
There you can find explanation how to use toD
(from Camel 2.16) or recipientList
: How to use dynamic URI in to 在这里,您可以找到有关如何使用toD
(来自Camel 2.16)或recipientList
: 如何在其中使用动态URI。
But as I said - there is only possibility to use it in to
. 但正如我所说-只能将其to
。 It's not possible to use it in from
. 这是不可能在使用它from
。 As a workaround, you have to write a route for each location you are expecting to be in use. 解决方法是,必须为预期使用的每个位置编写一条路由。 You can also use autoCreate=false
option to not creating other directories automatically, because for example Linux path without autoCreate=false
option: /home/user/test
will create directory structure in Windows c:\\home\\user\\test
您还可以使用autoCreate=false
选项不自动创建其他目录,因为例如,没有autoCreate=false
选项的Linux路径: /home/user/test
将在Windows c:\\home\\user\\test
创建目录结构
Since Camel 2.16 自骆驼2.16
We can use 我们可以用
.from("file://folder")
.toD("file://folder/${file:onlyname}")
Those answers are not correct. 这些答案是不正确的。 If you use a BridgePropertyPlaceholderConfigurer and a PropertiesComponent, you can use dynamic values everywhere. 如果使用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>
Or use something like this in real application 或在实际应用中使用类似的东西
<bean id="dummyPropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="location" value="classpath:/dummy.properties" />
</bean>
eg 例如
<route id="DummyRoute">
<from uri="file:{{dummy.int.dir}}?delay={{poll.delay}}&initialDelay={{initial.delay}}&{{readlockChanged}}&move={{root}}/{{dummy.arch.dir}}/{{archive.dir}}&moveFailed={{error.dir}}&scheduledExecutorService=#scheduledExecutorService" />
<to uri="file:{{root}}/{{dummy.int.destination.dir}}" />
</route>
There is a trick with later versions of Camel: use $simple{file.path}
instead of ${file.path}
so Spring won't strip your ${}
and pass the bare file.path to Camel. Camel的更高版本有一个技巧:使用$simple{file.path}
而不是${file.path}
这样Spring不会剥离${}
并将裸露的file.path传递给Camel。 Eg a move on an input 'from' uri might be like this: 例如,从“ 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/how-do-i-use-spring-property-placeholder-with-camel-xml.html
http://camel.apache.org/using-propertyplaceholder.html http://camel.apache.org/using-propertyplaceholder.html
According to Camel File Component : 根据骆驼文件组件 :
Camel supports only endpoints configured with a starting directory. Camel仅支持配置了起始目录的端点。 So the directoryName must be a directory. 因此,directoryName必须是目录。 If you want to consume a single file only, you can use the fileName option eg, by setting fileName=thefilename. 如果只想使用一个文件,则可以使用fileName选项,例如,通过设置fileName = thefilename。 Also, the starting directory must not contain dynamic expressions with ${} placeholders. 此外,起始目录不得包含带有$ {}占位符的动态表达式。 Again use the fileName option to specify the dynamic part of the filename . 再次使用fileName选项指定文件名的动态部分 。
So, you could do somethings like: 因此,您可以执行以下操作:
from(...).to("file://?fileName=${somePathAndSomeFile}").to(...)
Some of the comments/answers in this thread are misleading, as it's possible to set the value of "from" endpoint URI to have a value of a directory taken from a properties file as asked. 该线程中的某些注释/答案具有误导性,因为可以按要求将“ from”端点URI的值设置为具有从属性文件中获取的目录的值。
Place propertyPlaceholder element under camelContext, and ensure that the properties file could be found in the classpath 将propertyPlaceholder元素放置在camelContext下,并确保可以在类路径中找到属性文件
<propertyPlaceholder location="dir.properties" />
<from id="_fromInputFolder" uri="file:{{fromDir}}?"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.