11

I am using the date in a path in Camel:

fileName=${date:now:dd-MM-yyyy}

but what I need is now - 1 day. Is that possible?

Channing Walton
  • 3,977
  • 1
  • 30
  • 59

4 Answers4

13

Well, not directly. The date: object in the simple language can only grab the current time (or some time value you have placed inside a header - which you could do in java or similar.

But you could also do like this. Create a class:

public class YesterdayBean{
    public String getYesterday(){
        Calendar cal = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        cal.add(Calendar.DATE, -1); 
        return dateFormat.format(cal.getTime());  
    }
}

Wire it to your Camel (or spring, if you use that) registry as a bean. If you are unsure how to do that, lookup registry and the "using" section of bean.

Let's say you named the bean "yesterday" in the registry, with spring:

<bean id="yesterday" class="some.package.YesterdayBean"/>

then just use it with the file component.

.to("file:fo/bar?fileName=${bean:yesterday}")

If this is just one single place you need it, and you are using Java DSL, you could also just pre-create the date with a java processor and place it in a header.

Like this:

from("file:somewhere")
        .process(new Processor(){
            public void process(Exchange ex){
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1); 
                ex.getIn().setHeader("yesterday",cal.getTime());
            }
        })
       .to("file:target?fileName=${date:header.yesterday:dd-MM-yyyy}");
}
Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
  • and we can call method as .to("file:fo/bar?fileName=${bean:yesterday?method=getYesterday}") – Nirbhay Mishra Aug 09 '16 at 06:25
  • 1
    This is no more the optimal solution. Camel [Simple](https://camel.apache.org/components/latest/languages/simple-language.html) language has added a feature to support fixed offsets with date command like `now-1d`. – user272735 Mar 22 '22 at 14:42
  • 1
    Thanks @user272735! The answer is 10 years old, so updates are very welcome – Petter Nordlander Mar 23 '22 at 10:01
4

Camel Simple language date variable supports command with offset:

Supported commands are: now for current timestamp, [...] Command accepts offsets such as: now-24h or header.xxx+1h or even now+1h30m-100.

So you can write your assigment as:

fileName=${date:now-1d:dd-MM-yyyy}

Note that -1d equals to -24h even not mentioned in the documentation.

user272735
  • 10,473
  • 9
  • 65
  • 96
Alberto
  • 2,881
  • 7
  • 35
  • 66
3

I was curious about this and sought out some help from the camel mailing list. You can in fact do what you're asking with inline scripts, such as groovy. See here.

I got this to work for me:

<camelContext id="contextname">
    <route id="routename">
        <from uri="file://\temp\?fileName=#myGroovyExp" />
        <split>
            <tokenize token="(?=MSH\|)" regex="true" />
            <to uri="bean:filePickupByDateTest?method=test" />
        </split>
    </route>
</camelContext>

<spring:bean id="myGroovyExp" class="org.apache.camel.model.language.GroovyExpression">
    <spring:constructor-arg index="0" value="new Date().previous().format('MMddyy') + 'pa'" />
</spring:bean>

Where my file names are yesterday: MMddyypa

You would just need to change your script body to:

new Date().previous().format('dd-MM-yyyy')

You need camel-groovy (or whatever script lang you use) on your path of course.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
0

A solution using filename filter :

Create a class that implements org.apache.camel.component.file.GenericFileFilter, and implement the method accept to validate the filename

public class CustomFileName implements GenericFileFilter {

    public boolean accept(GenericFile file) {

        Calendar cal = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        cal.add(Calendar.DATE, -1); 

        return file.getFileName().equals ("FILENAME_PREFIX"+dateFormat.format(cal.getTime()) + ".EXT");
    }
} 

In Spring config

<bean id="customFileFilter" class="com.sample.proj.util.CustomFileName"/>

and

<route>
    <description>Route for copying file from one location to another with custom file name filter</description>
    <from uri="file://C:\Source?filter=#customFileFilter" />
    <to uri="file://C:\Destination" />
</route>
Ach J
  • 510
  • 5
  • 9