如何在Apache骆驼路线中提及动态目录路径 [英] How to mention dynamic directory path in Apache camel route
问题描述
我正在尝试从不同目录中获取文件.例如我有以下目录结构
I am trying to take files from different directories. For example I have following directory Structure
vendors/dir1/files/heelo.txt
vendors/dir2/files/hello2.txt
这里有3个目录:
1.供应商
2.dir1和dir2
3.文件
1.vendors
2.dir1 and dir2
3.files
由于2.dir1和dir2不同,所以我得动态取.
since 2. dir1 and dir2 is different, so I have to take it dynamically.
我编写了以下代码:
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="com.performancebikes.Inventory1" autoStartup="false">
<from uri="b2bmbFileSystem://com.a/vendors/${file:name}/files"/>
<to uri="b2bmbMailBox://com.b/Files"/>
</route>
</routes>
由于目录 ${file:name}
不起作用,请帮我解决这个问题
since it is directory ${file:name}
is not working, please help me to resolve this
推荐答案
如果您想使用 vendors
下的每个文件,您可以递归地使用文件强>:
<from uri="b2bmbFileSystem://com.a/vendors/?recursive=true"/>
<to uri="b2bmbMailBox://com.b/Files"/>
如果消耗了vendors/dir2/files/hello2.txt
文件,输出的文件会保存在com.b/Files/dir2/files/hello2.txt
下code>,因此它重新创建与源文件系统中相同的相对路径.
If it consumes the file vendors/dir2/files/hello2.txt
, the output file will be saved under com.b/Files/dir2/files/hello2.txt
, so it recreates the same relative path as in the source file system.
如果不想重新创建相同的结构,可以将输出结构展平:
If you don't want to recreate the same structure, you can flatten the output structure:
<from uri="b2bmbFileSystem://com.a/vendors/?recursive=true"/>
<to uri="b2bmbMailBox://com.b/Files?flatten=true"/>
这会带来相同文件名出现在多个子目录中的风险,因此您在目标文件夹中出现冲突.
This comes with the risk that the same filename appears in multiple subdirectories and therefore you got a conflict in the target folder.
如果您想仅从两个特定目录中使用,您可以简单地创建两个路由:
If you want to consume only from the two specific directories you can simply create two routes:
<from uri="b2bmbFileSystem://com.a/vendors/dir1/files/"/>
<to uri="b2bmbMailBox://com.b/Files"/>
<from uri="b2bmbFileSystem://com.a/vendors/dir2/files/"/>
<to uri="b2bmbMailBox://com.b/Files"/>
只要路由不包含也被乘法的处理逻辑,有多个是没有问题的.
As long as the routes do not contain processing logic that is also multiplied, it is no problem to have multiple of them.
即使你有处理逻辑,你也可以像上面那样编写简单的文件收集器路由",然后构建一个使用所有文件收集目录的路由,并在该路由中实现逻辑.
And even if you got processing logic, you could write simple "file collector routes" as the ones above and then build a route that consumes the directory where all files are collected and implement the logic in that route.
如果您想从大量特定目录中使用,您可以在您的应用程序中注入一个路由配置列表.例如,YAML 格式的路由配置可能如下所示:
If you want to consume from lots of specific directories, you could inject a List of route-configurations in your application. A route config in YAML format could for example look like this:
fileConsumer:
routes:
- routeId: "consumer1"
source: "/path/to/source/directory"
target: "/path/to/target/directory"
- routeId: "consumer2"
source: "/path/to/other/source/directory"
target: "/path/to/other/target/directory"
如果您将其作为 List
注入,您可以在 RouteBuilder 类中迭代它以创建所有配置的路由:
If you inject this as List<RouteConfiguration>
you can iterate over it in a RouteBuilder class to create all configured routes:
@Override
public void configure() {
configuration.getRoutes().forEach(this::addRouteToContext);
}
private void addRouteToContext(final RouteConfiguration routeConfiguration) throws Exception {
String fileReaderSourceUri = [build complete endpoint URI from directory];
String fileReaderTargetUri = [build complete endpoint URI from directory];
this.camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(fileReaderSourceUri)
.routeId(routeConfiguration.getRouteId())
.to(fileReaderTargetUri);
}
}
}
这篇关于如何在Apache骆驼路线中提及动态目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!