Reputation: 3641
I need to add/remove Apache Camel
routes at a runtime in Spring Boot
application. It means that routes should be updated while application is running. It also means that I don't have all routes during the first run of the application and I cannot restart it in order to get them.
I found old answers but still cannot understand how to do it using xml
-defined routes. Can you explain how to do it?
Upvotes: 3
Views: 2120
Reputation: 3641
It's possible to add camel.springboot.xml-routes-reload-directory=<monitoring-directory>
into application.properties
and monitor routes into xml
format.
It will reload all routes, so in order to add delete them, it possible to only add delete files with route definitions.
Upvotes: 1
Reputation: 33
Put your xml under src/main/resources and invoke the following component :
@Component
public class CamelRoutesLoader {
@Autowired
private CamelContext camelContext;
@Value("${camel-routes-filename:#{null}}")
private String routesFilename;
@PostConstruct
private void loadRoutes() {
if(routesFilename == null) {
//error no file name
}
try {
InputStream is = this.getClass().getClassLoader().getResource(routesFilename).openStream();
RoutesDefinition routes = ModelHelper.loadRoutesDefinition(camelContext, is);
camelContext.addRouteDefinitions(routes.getRoutes());
} catch(Exception e) {
//Impossible to load routes
}
}
}
I call my component after it is initialized by Spring, but you can remove @PostConstruct, make it public and pass it the filename
Upvotes: 2