How to mention dynamic directory path in Apache camel route

梦想的初衷 提交于 2021-01-29 11:01:01

问题


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

in this there are 3 dirs:

1.vendors
2.dir1 and dir2
3.files

since 2. dir1 and dir2 is different, so I have to take it dynamically.

I have written following code:

<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>

since it is directory ${file:name} is not working, please help me to resolve this


回答1:


If you want to consume every file under vendors you can consume files recursively:

<from uri="b2bmbFileSystem://com.a/vendors/?recursive=true"/>
<to uri="b2bmbMailBox://com.b/Files"/>

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.

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"

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


来源:https://stackoverflow.com/questions/51901947/how-to-mention-dynamic-directory-path-in-apache-camel-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!