How to add date dynamically to camel route xml 

2019-09-13 11:19发布

问题:

I am adding new camel route xml. I am having trouble getting date value in to to URI.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://www.myCompany.com/123/api/route/1.0">
//...some lines
....

<from>
<uri>...</uri>
  ....
.....
</from>
<to>
  <uri>file://C:/mywork/${date:now:yyyy}-${date:now:MM}-${date:now:dd}</uri>
</to>
</route>

When I use this as above getting CaughtExceptionType: groovy.lang.MissingPropertyException, CaughtExceptionMessage: No such property: yyyy for class: script1465913927580309309860, StackTrace: groovy.lang.MissingPropertyException: No such property: yyyy for class: script1465913927580309309860

In to URI I have mentioned the folder name with yyyy-MM-dd format.I do not know how to get date there. In short I want to uri as 'C:/myWork/2011-01-01'. THe problem I am thinking of is because of groovy parsing the 'to uri' value Thanks in advance.

回答1:

I would probably not do it like that.

It would be better if you create a processor and construct the filename there and write the filename to the Exchange.FILE_NAME header and then send the data to the uri:

<uri>file://C:/mywork</uri>

You may want to change the filename later or do some other processing and doing it in the processor is easier and hides it way from the xml dsl.

Even if you want to create it in the xml dsl, I still think for dynamic file names, you need to create it and set it to that header.



回答2:

Try changing your date format to: ${date:now:yyyy-MM-dd} This format will let you express the desired date in a single replacement.

So your endpoint should look like:

<to>
  <uri>file://C:/mywork/${date:now:yyyy-MM-dd}</uri>
</to>


回答3:

use dynamic routing. Use <toD uri="/path/to/file/${date:now:yyyy-MM-dd}"> in order to dynamically route based on run time values