Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-15450

ClusteredRoutePolicyFactory throw NullPointerException when adding a route to CamelContext that has been started

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.25.2
    • 3.5.0
    • came-core
    • None
    • Novice

    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

          Activity

            People

              davsclaus Claus Ibsen
              491739727@qq.com wupenghuai
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: