Externalize Apache Camel Route Configurations for Spring Boot Apps
Time to get your Camel where it needs to go.
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will discuss how we can externalize Apache Camel route configurations in a Spring Boot application using Spring profiles.
Below is a sample code of a Camel Route, which we will externalize.
package com.demo.camelspringboot.route;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
public class CamelRoute extends RouteBuilder {
public void configure() throws Exception {
from("timer:test?period=5s")
.log("Timer has been invoked")
.pollEnrich("file:inputdirectory/input?delete=true")
.to("file:outputdirectory/output");
}
}
Create application-dev.yml, application-tst.yml, application-prod.yml, and move the route configurations specific to the environments (dev, tst, prod) to the respective YML files.
application-dev.yml
xxxxxxxxxx
server
port8081
routeStart timer dev?period=5s
routeFrom file dev/input?delete=true
routeTo file dev/output
application-tst.yml
xxxxxxxxxx
server
port8081
routeStart timer test?period=5s
routeFrom file tst/input?delete=true
routeTo file tst/output
application-prod.yml
xxxxxxxxxx
server
port8081
routeStart timer prod?period=5s
routeFrom file prd/input?delete=true
routeTo file prd/output
Reference the values from the YML file in the code as below:
x
package com.demo.camelspringboot.route;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
public class CamelRoute extends RouteBuilder {
public void configure() throws Exception {
from("{{routeStart}}") //Referenced from yml
.log("Timer has been invoked")
.pollEnrich("{{routeFrom}}") //Referenced from yml
.to("{{routeTo}}"); //Referenced from yml
}
}
Create input directories specific to the environment as shown:
Edit the Run configuration of the Spring Boot application and provide the Active Profile as dev and start the application.
Notice that the file is deleted from the input directory of the dev folder and copied to the output directory as per the configuration.
Similarly, modify the Run configuration and set the Active profile as tst and prod. Notice that the file is deleted from the input directory of the respective environment folder and copied to the output directory as per the configuration and active profile set.
Opinions expressed by DZone contributors are their own.
Comments