Issue
I have existing Spring Boot application that uses Camel framework for getting data from different folders. All routes poll data in similar way:
from(fileUriWithCurrentDay(path, config.getParams())).routeId(ROUTE_ID)
where method defined as follows:
public static String fileUriWithCurrentDay(String path, Map<String, String> params) {
path = path.endsWith(SEPARATOR) ? path : path + SEPARATOR;
return "file:" + path + currentDayPath() + fileParams(params);
}
and returns something like depending on the day:
c:/test/data/2021/242?noop=true&idempotent=false&autoCreate=false&includeExt=xml&scheduler=spring&scheduler.cron = "0 0 13 ? * *"
The cron is set to start one time per day. When cron time is reached the route starts to perform its job and finishes it correctly. But on the next day it starts to poll the same folder, e.g. 242 but it should be 243.
So the problem is: from endpoint should be calculated dynamically at its start.
I've read about dynamic from: in Camel, but didn't find any information that such option is supported out of the box.
I've tried to use pollEnrich()
option using header that is calculated, but it seem doesn't support dynamic endpoints as returns next error:
org.apache.camel.ResolveEndpointFailedException Dynamic expressions with ${ } placeholders is not allowed. Use the fileName option to set the dynamic expression.
How from endpoint can be refreshed after route is started?
I also see 2 possible solutions but didn't know will it help:
- Restart routes for recalculating from endpoint uri.
- Restart whole Camel context. Currently all routes are marked as Spring
@Component
.
Solution
Looking at the documentation for the File component what you are trying to do is not possible purely with the from-endpoint URI.
I would suggest using a common base directory in the URI and creating an implementation of GenericFileFilter that you'd use with the from-endpoint. It allows for a lot of freedom in implementing the logic for which files are and which aren't processed.
To read files from base/path/2021
create something like this:
@Component
public class FileFilter<T> implements GenericFileFilter<T> {
@Override
public boolean accept(GenericFile<T> file) {
if (file.isDirectory()) {
return "2021".equals(file.getFileName());
}
return file.getFileName().startsWith("2021/");
}
}
with endpoint URI like this:
file://base/path?recursive=true&filter=#fileFilter
When implementing the GenericFileFilter you should be mindful that accept()
will be called for the directories as well.
Answered By - jwwallin