Hi everybody,
here is my solution so far. I discovered that we would need to remove
the existing route and recreate it using its initial RouteDefinition.
That is at least the way ReloadStrategies are supposed to work. Here is
some demo code. It restarts a ticker route with random period parameter
values.
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.main.Main;
import org.apache.camel.model.RouteDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
import java.util.Random;
public class DynamicRouteReloadExperiment {
protected final Loggerlog = LoggerFactory.getLogger(getClass());
public static void main(String... args)throws Exception {
Main main =new Main();
Properties props =new Properties();
props.setProperty("period","1500");
PropertiesComponent propertiesComponent =new PropertiesComponent();
propertiesComponent.setOverrideProperties(props);
main.bind("properties", propertiesComponent);
main.addRouteBuilder(new RouteBuilder() {
@Override public void configure()throws Exception {
from("timer:tick?period={{period}}")
.routeId("A")
.log("Tick!");
from("timer:tick?period=8000")
.routeId("B")
.process(exchange -> {
CamelContext camelContext = exchange.getContext();
Random random =new Random();
camelContext.getComponent("properties",
PropertiesComponent.class)
.getOverrideProperties()
.setProperty("period", String.valueOf(1000
+ random.nextInt(1000)));
RouteDefinition routeDefinition =
camelContext.getRouteDefinition("A");
camelContext.removeRouteDefinition(routeDefinition);
camelContext.addRouteDefinition(routeDefinition);
});
}
});
main.run(args);
}
}
IMHO this "hard restart" should be a feature of the ControlBus component
which can already stop and resume routes.
-Ralf
On 10/16/19 9:28 AM, Ralf Claussnitzer wrote:
Hello all,
is there a way to restart defined routes so that the from() URI gets
updated?
We have tried several things using the "Controlbus" component, but we
cannot restart a defined and running route /with changed URI parameters/.
Background (what do we want to achieve):
We have several Kafka Consumer routes which we want to restart with
different options depending on other events. In particular we want
to restart a running route and change the URI parameter "seekTo" to
trigger reprocessing past events.
While reprocessing Kafka events is our main topic at the moment, we
imagine that restarting a route and recalculate the consumer URI might
be useful for other use cases as well. Let us know what you think!
Regards,
Ralf