コード例 #1
0
  public void onRoutesAdd(Collection<Route> routes) {
    for (Route route : routes) {

      // if we are starting CamelContext or either of the two options has been
      // enabled, then enlist the route as a known route
      if (getCamelContext().getStatus().isStarting()
          || getManagementStrategy().getManagementAgent().getRegisterAlways()
          || getManagementStrategy().getManagementAgent().getRegisterNewRoutes()) {
        // register as known route id
        knowRouteIds.add(route.getId());
      }

      if (!shouldRegister(route, route)) {
        // avoid registering if not needed, skip to next route
        continue;
      }

      Object mr = getManagementObjectStrategy().getManagedObjectForRoute(camelContext, route);

      // skip already managed routes, for example if the route has been restarted
      if (getManagementStrategy().isManaged(mr, null)) {
        LOG.trace("The route is already managed: {}", route);
        continue;
      }

      // get the wrapped instrumentation processor from this route
      // and set me as the counter
      if (route instanceof EventDrivenConsumerRoute) {
        EventDrivenConsumerRoute edcr = (EventDrivenConsumerRoute) route;
        Processor processor = edcr.getProcessor();
        if (processor instanceof CamelInternalProcessor && mr instanceof ManagedRoute) {
          CamelInternalProcessor internal = (CamelInternalProcessor) processor;
          ManagedRoute routeMBean = (ManagedRoute) mr;

          CamelInternalProcessor.InstrumentationAdvice task =
              internal.getAdvice(CamelInternalProcessor.InstrumentationAdvice.class);
          if (task != null) {
            // we need to wrap the counter with the camel context so we get stats updated on the
            // context as well
            if (camelContextMBean != null) {
              CompositePerformanceCounter wrapper =
                  new CompositePerformanceCounter(routeMBean, camelContextMBean);
              task.setCounter(wrapper);
            } else {
              task.setCounter(routeMBean);
            }
          }
        }
      }

      try {
        manageObject(mr);
      } catch (JMException e) {
        LOG.warn("Could not register Route MBean", e);
      } catch (Exception e) {
        LOG.warn("Could not create Route MBean", e);
      }
    }
  }
コード例 #2
0
  /**
   * Add the route in the database if it's not already, or update its state.
   *
   * @param routes being added to the app
   */
  @Override
  public void onRoutesAdd(Collection<Route> routes) {
    for (Route routeCamel : routes) {
      // adding a performance counter on the route
      if (routeCamel instanceof EventDrivenConsumerRoute) {
        EventDrivenConsumerRoute edcr = (EventDrivenConsumerRoute) routeCamel;
        Processor processor = edcr.getProcessor();
        if (processor instanceof InstrumentationProcessor) {
          InstrumentationProcessor ip = (InstrumentationProcessor) processor;
          ConsolePerformanceCounter counter =
              new ConsolePerformanceCounter(routeCamel.getId(), repository);
          ip.setCounter(counter);
          log.debug("Adding a counter" + counter.toString() + " to " + routeCamel.getId());
        }
      }

      // saving route in database
      log.debug("Route added " + routeCamel.getId());
      com.ninja_squad.console.Route route = repository.findRoute(routeCamel.getId());
      if (route == null) {
        route =
            new com.ninja_squad.console.Route(routeCamel.getId())
                .state(State.Started)
                .uri(routeCamel.getEndpoint().getEndpointUri());
        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector introspector =
            new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        // make serializer use JAXB annotations (only)
        mapper.setAnnotationIntrospector(introspector);
        String definition = null;
        RouteDefinition routeDefinition = routeCamel.getRouteContext().getRoute();
        try {
          definition = mapper.writeValueAsString(routeDefinition);
        } catch (IOException e) {
          log.error("Error while marshalling route definition", e);
        }
        route.setDefinition(definition);
        for (ProcessorDefinition<?> stepDefinition : routeDefinition.getOutputs()) {
          if (stepDefinition.getId() == null) {
            stepDefinition.setId(stepDefinition.getClass().getSimpleName());
          }
          route.getSteps().put(stepDefinition.getId(), stepDefinition.getLabel());
        }
        repository.save(route);
      }

      // saving state in database
      RouteState routeState = repository.lastRouteState(routeCamel.getId());
      if (routeState == null || routeState.getState().equals(State.Stopped)) {
        routeState = new RouteState();
        routeState.setRouteId(routeCamel.getId());
        routeState.setState(State.Started);
        routeState.setTimestamp(DateTime.now().getMillis());
        repository.save(routeState);
      }
    }
  }