Dynamically spawning camel routes after consuming a .properties file - apache-camel

I've written a basic route builder that pulls a file a from a location, does some message transformations, and then sends it somewhere else. The start and endpoints I currently have set in a .properties file.
The goal of my project is to be able to have a static camel route that consumes .properties file and creates the routes from the route builder that I've already written with the start and endpoints being specified in the properties file. How would I go about doing something like this? Am I approaching the problem incorrectly?
Thanks in advance guys!
EDIT with more explaination:
So currently my camel routes look something like this:
{{fileInput}} -> camel routes -> {{fileOutputs}}
I have a .properties file that specifies a single input endpoint and a single output endpoint. However, I would like my end code to be something like this:
{{fileInput1}} -> camel routes -> {{fileOutputs1}}
{{fileInput2}} -> camel routes -> {{fileOutputs2}}
{{fileInput3}} -> camel routes -> {{fileOutputs3}}
.
.
.
{{fileInputN}} -> camel routes -> {{fileOutputsN}}
My original idea was to have a separate static camel route that polls a specific file location. If I needed a new route I would just drop a .properties file in there and the route would consume the .properties file and generate one of the above routes. Is there an easier way to do this?

You can simply load the property file inside your RouteBuilder.
InputStream input = new FileInputStream("config.properties");
Properties prop = new Properties();
prop.load(input);
Then use them.
prop.getProperty("MySuperProperty");
Yet I don't understand what you are trying to achieve.

Related

How to configure retrieving folder once when camel start?

My requirement as below,
Starting camel,then processing all of the files with file component ,which is located some folder once.this route is just used once.
So how to configure with spring DSL?
Thanks advance.
Split your requirement as follow
Starting camel
Your research item, cover this will be off-topic in stackoverflow
processing all of the files with file component
Use file component by from, i.e. from("file://...")
is located some folder once
Set a proper parent directory and then use recursive option in file component to enable sub-directories lookup. For further control, you may check following options minDepth, maxDepth, filterDirectory, filter
this route is just used once
Use repeatCount option in file component to control fire count, i.e. repeatCount=1
Combine them together, you have
from("file://path/to/parent/directory?repeatCount=1&recursive=true")
... // follow by your route logic

Apache camel multiple dynamic route for single file

I have a dynamic route which reads and process a file and there could be multiple files. Each route is processing single file at a time. When request comes for different files at the same time there is no issue, as it creates unique URI based on file name. I also need to support parallel request for same file.
I am getting below exception for parallel request.
Failed to start route MyFileProcessorDynamicRoute because of Multiple consumers for the same endpoint is not allowed.
Is there a way where i can create chaining of route at runtime?
Thanks in advance.
As per your context I am getting to know, you have to use same file(having same name) to be used in different route at same time.
You can achieve by setting flag kind of thing(sequence order in which route has to run) in route were you need to process the same name file. By doing this you cannot start second route without completing first route.
This might tricky but achievable.
Thanks.

Apache Camel File component base path

How do I manage to configure the File component of my Camel Context, so that all directory paths provided to its endpoints are prepended with some base path?
For example, if someone writes
file:input/customer12?include=.*\.csv
it will effectively be
file:/usr/local/share/app/exchange/input/customer12?include=.*\.csv
For example, I get the component during the Camel Context initialization like this:
FileComponent file = CAMELCONTEXT.getComponent("file", FileComponent.class);
What do I do next? createComponentConfiguration()?
addition: It's a standalone cli app which I want to be runnable from any directory
The easiest solution is start your app under folder /usr/local/share/app/exchange , but this is not an option as state in addition requirement
If you have a layer between your user input and your code, then you could inject the path. For example, Java DSL with RouteBuilder.
The last solution is override Camel's file component in component class before it create the actual endpoint.

Getting Filename without extension : camel

I have header with filename with extension like this
<setHeader>
<simple>Test.txt</simple>
</setHeader>
I need to fetch only filename ie. Test alone in another header. Can anyone help me to achieve this.
Thanks.
You have multiple options. The examples take the full filename from a header and write the filename without extension into another header.
You write a Java Bean to use full Java power (for example Apache Commons FilenameUtils.getBaseName) and call it from your route. See this Camel documentation how to inject header values into your bean methods. In the route you call the bean like this
.setHeader("filename", method(beanReference, "methodName"))
Or you add camel-groovy to your dependencies to get more scripting power than with Camel Simple. Then you can do it directly in the Camel route
setHeader("filename").groovy("request.headers.get('fullFilename')
.take(request.headers.get('fullFilename').lastIndexOf('.'))")

Camel Sftp - Download Multiple Files

I am able to successfully download one/all files from a sftp directory using the following uri in canmel route definition
Download all files
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true"
Download one file
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true&fileName=one.txt"
My requirement is to download a specific list of files = one.text, two.text.
How can I pass list of fileNames to the Camel route? Preferablly I am looking for a solution where in I can specify something like the below
"sftp://userName#serverName/directoryName?knownHostsFile=./known_hosts&privateKeyFile=./id_rsa&proxy=#proxy&noop=true&fileName=one.txt,two.txt"
The Camel FTP component extends the File component and many of the options from file is also applicable for the FTP component, so read this page
http://camel.apache.org/file2
You can for example use include to specify a regular expression that matches the files you want. Or implement a custom filter class and use the filter option, etc.

Resources