Jsmith
Jsmith

Reputation: 360

Camel rename file after Timer

In Camel, is there any way to rename a file after a set period of time, using the Timer component and a Spring configuration, if the file already exists?

Is it only achievable by having a Timer route that calls a Java method that will rename the file?

So, I have a socket I want to pull data from. The data flows all day, every day. Because of that, I do not just want to keep routing the data into one specific file.

So my thought process led me to just rename the existing file after X period of time and let the Netty route create a new file since it wouldn't exist anymore after the renaming.

I have a route similar to:

<route customId="true" id="socketToFileRoute">
    <from uri="netty4:tcp://localhost:9999?clientMode=true&amp;textline=true"  />
    <transform>
        <simple>${in.body}\n</simple>
    </transform>
    <to uri="file://data?fileName=socketData.txt&amp;charset=utf-8&amp;fileExist=Append"/>
</route>

Is there a way to set up a Timer route along the lines of the following?:

<route customId="true" id="dataFileRenamer">
    <from uri="timer://renameFile?fixedRate=true&amp;period=50"/>
    <to uri="file://data/socketData.txt?rename the file created in the previous route" />
</route>

Upvotes: 1

Views: 1577

Answers (2)

Miloš Milivojević
Miloš Milivojević

Reputation: 5369

You don't need a timer or anything complex, you should just use toD (Dynamic To endpoint):

<route customId="true" id="socketToFileRoute">
    <from uri="netty4:tcp://localhost:9999?clientMode=true&amp;textline=true"  />
    <transform>
        <simple>${in.body}\n</simple>
    </transform>
    <toD uri="file://data?fileName=socketData-${date:now:yyyyMMddHHmmss}.txt&amp;charset=utf-8&amp;fileExist=Append"/>
</route>

This will resolve the endpoint dynamically for each message, resulting in a new file being generated each second, or whatever period you want to use.

Upvotes: 2

Souciance Eqdam Rashti
Souciance Eqdam Rashti

Reputation: 3191

You cannot rename the file, at least not that I am aware of. You could via the timer node enrich your route by reading content of the existing file using the .enrich() syntax and then create a new file with a new timestamp. A rough example:

.from("timer://renameFile?fixedRate=true&period=50")
.enrich("file://pathtofile?fileName=<filename>")
.to("file://pathtofile?fileName=${file:name.noext}-${date:now:yyyyMMdd-HHmmss}.${file:ext}");

Something along those lines. Off course this means you generate a new file for every period. If you really want to rename, then I guess you would have to create a processor class and use the standard java API to see if you can consume and rename it.

Upvotes: 1

Related Questions