Java & Apache-Camel: From direct-endpoint to file-endpoint -


i've tried build route copy files 1 directory other directory. instead of using: from(file://source-directory).to(file://destination-directory) want this:

from(direct:start)   .to(direct:dostuff)   .to(direct:readdirectory)   .to(file://destination-folder) 

i've done following stuff:

route

@component public class route extends abstractroutebuilder {   @override   public void configure() throws exception {       from("direct:start")         .bean(lookup(readdirectory.class))         .split(body())           .setheader("filename", method(lookup(createfilename.class)))           .to("file:///path/to/my/output/directory/?filename=${header.filename}");   } 

processor

@component public class readdirectory implements camelprocessorbean {   @handler   public immutablelist<file> apply(@header("source_dir") final string sourcedir) {     final file directory = new file(sourcedir);     final file[] files = directory.listfiles();     if (files == null) {       return immutablelist.copyof(lists.<file>newarraylist());     }     return immutablelist.copyof(files);   } } 

i can start route using following pseudo-test (the point can manually start route producer.sendbodyandheader(..))

public class routeit extends standardit {   @produce   private producertemplate producer;    @test   public void testroute() throws exception {     final string uri = "direct:start";     producer.sendbodyandheaders(uri, inout, null, header());   }    private map<string, object> header() {     final map<string, object> header = maps.newhashmap();     header.put("source_dir", "/path/to/my/input/directory/");     return header;   } } 
  • abstractroutebuilderextends springroutebuilder
  • camelprocessorbean marker-interface
  • standardit loads springcontext , stuff

the problem is, must set filename. i've read stuff camel sets header camelfilenameproduced (during file endpoint). generic string timestamp , if don't set filename - written files generic string filename.

my question is: there more beautiful solution copy files (but starting direct-endpoint , read directory in middle of route) , keep filename destination? (i don't have set filename when use from("file:source").to("file:destination"), why must now?)

you can set file name when send using producer template, long header propagated during routing between routes fine, camel default.

for example

  @test   public void testroute() throws exception {     final string uri = "direct:start";     map headers = ...     headers.put(exchange.file_name, "myfile.txt");     producer.sendbodyandheaders(uri, inout, null, headers);   } 

the file component talks more how control file name


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -