Description
Hello,
I recently found that when using ClusteredRoutePolicyFactory , adding a route to a CamelContext that has been started gives an error.
follow is exceptionn stak
org.apache.camel.FailedToCreateRouteException: Failed to create route ns1/1: Route(ns1/1)[[From[timer:pocTimer?period=10s]] -> [SetHeader... because of java.lang.RuntimeException: java.lang.NullPointerExceptionorg.apache.camel.FailedToCreateRouteException: Failed to create route ns1/1: Route(ns1/1)[[From[timer:pocTimer?period=10s]] -> [SetHeader... because of java.lang.RuntimeException: java.lang.NullPointerException at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:217) at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1139) at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3736) at org.apache.camel.impl.DefaultCamelContext.addRouteDefinitions(DefaultCamelContext.java:1071) at com.netease.cloud.nsf.server.DefaultServer.onAdd(DefaultServer.java:65) at com.netease.cloud.nsf.resource.DefaultResourceManager.lambda$compareAndNotify$0(DefaultResourceManager.java:51) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)Caused by: org.apache.camel.RuntimeCamelException: java.lang.RuntimeException: java.lang.NullPointerException at org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory.createRoutePolicy(ClusteredRoutePolicyFactory.java:65) at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1304) at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:212) ... 8 moreCaused by: java.lang.RuntimeException: java.lang.NullPointerException at org.apache.camel.impl.cluster.ClusteredRoutePolicy.setCamelContext(ClusteredRoutePolicy.java:140) at org.apache.camel.impl.cluster.ClusteredRoutePolicy.forNamespace(ClusteredRoutePolicy.java:350) at org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory.createRoutePolicy(ClusteredRoutePolicyFactory.java:63) ... 10 moreCaused by: java.lang.NullPointerException at org.apache.camel.impl.cluster.ClusteredRoutePolicy.onCamelContextStarted(ClusteredRoutePolicy.java:285) at org.apache.camel.impl.cluster.ClusteredRoutePolicy.access$800(ClusteredRoutePolicy.java:49) at org.apache.camel.impl.cluster.ClusteredRoutePolicy$CamelContextStartupListener.onCamelContextStarted(ClusteredRoutePolicy.java:338) at org.apache.camel.impl.cluster.ClusteredRoutePolicy$CamelContextStartupListener.onCamelContextStarted(ClusteredRoutePolicy.java:323) at org.apache.camel.impl.DefaultCamelContext.addStartupListener(DefaultCamelContext.java:1565) at org.apache.camel.impl.cluster.ClusteredRoutePolicy.setCamelContext(ClusteredRoutePolicy.java:136) ... 12 more
then i locate the cause:
ClusteredRoutePolicyFactory will call ClusteredRoutePolicy.forNamespace
ClusteredRoutePolicy.forNamespace rely on ClusteredRoutePolicy.setCamelContext
SetCamelContext will determine if the CamelContext has been started, and if so, the following logic will be executed
private void onCamelContextStarted() { LOGGER.debug("Apply cluster policy (stopped-routes='{}', started-routes='{}')", stoppedRoutes.stream().map(Route::getId).collect(Collectors.joining(",")), startedRoutes.stream().map(Route::getId).collect(Collectors.joining(",")) ); clusterView.addEventListener(leadershipEventListener); }
But at this point the clusterView is still not initialized.It will be initialized when the ClusteredRoutePolicy.start() is called.
Attachments
Issue Links
- is related to
-
CAMEL-15454 camel-cluster - Adding a new route after camel is started should start the route if leader
- Resolved