Mopper
Mopper

Reputation: 1727

Is there a way to make the output file of a stream:file?fileName= dynamic?

Given a simple route like this

route.from("direct:foo")
   .split()
   .tokenize("\n")
   .streaming()
   .to("stream:file?fileName=target/streaming${header.count}.txt&closeOnDone=true");

which I then trigger with this

@Test
public void splitAndStreamToFile() {
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < 500; i++) {
        builder.append(i);
        builder.append("\n");
    }

    for(int i = 0; i < 10; i++) {
        template.sendBodyAndHeader(builder.toString(), "count", i);
    }

}

I get one big file that contains 10 times 500 lines, where I would have hoped to have 10 files that contain 500 lines each.

In other words, it seems that the fileName in the stream:file endpoint is not dynamic. I am wondering if this is at all possible? My google-fu turned up nothing so far.

Upvotes: 0

Views: 2080

Answers (3)

Mark Meandro
Mark Meandro

Reputation: 1

In short,

toD uri=stream:file...

will do it.

The "toD" basically translates the "simple" or "file language" before it hits the stream component code...so that works for "fileName=..."

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55550

Its a dynamic to which there is an EIP pattern for:

But it could be a good idea to support the file/simple language on the fileName option as the regular file component does. Fell free to log a JIRA ticket about this improvement.

Upvotes: 2

cringe
cringe

Reputation: 14010

Sourcecode of the StreamProducer looks like it does not support any of the expression languages of Camel yet:

private OutputStream resolveStreamFromFile() throws IOException {
    String fileName = endpoint.getFileName();
    ObjectHelper.notEmpty(fileName, "fileName");
    LOG.debug("About to write to file: {}", fileName);
    File f = new File(fileName);
    // will create a new file if missing or append to existing
    f.getParentFile().mkdirs();
    f.createNewFile();
    return new FileOutputStream(f, true);
}

See sourecode.

If you need dynamic filenames, you should take a look at the file component, which supports the file language and the CamelFileName header.

Upvotes: 1

Related Questions