Exemplo n.º 1
0
  protected void createJmxConnector(String host) throws IOException {
    ObjectHelper.notEmpty(serviceUrlPath, "serviceUrlPath");
    ObjectHelper.notNull(registryPort, "registryPort");

    try {
      registry = LocateRegistry.createRegistry(registryPort);
      LOG.debug("Created JMXConnector RMI registry on port {}", registryPort);
    } catch (RemoteException ex) {
      // The registry may had been created, we could get the registry instead
    }

    // must start with leading slash
    String path = serviceUrlPath.startsWith("/") ? serviceUrlPath : "/" + serviceUrlPath;
    // Create an RMI connector and start it
    final JMXServiceURL url;
    if (connectorPort > 0) {
      url =
          new JMXServiceURL(
              "service:jmx:rmi://"
                  + host
                  + ":"
                  + connectorPort
                  + "/jndi/rmi://"
                  + host
                  + ":"
                  + registryPort
                  + path);
    } else {
      url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + registryPort + path);
    }

    cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);

    // use async thread for starting the JMX Connector
    // (no need to use a thread pool or enlist in JMX as this thread is terminated when the JMX
    // connector has been started)
    String threadName =
        camelContext.getExecutorServiceManager().resolveThreadName("JMXConnector: " + url);
    Thread thread =
        getCamelContext()
            .getExecutorServiceManager()
            .newThread(
                threadName,
                new Runnable() {
                  public void run() {
                    try {
                      LOG.debug("Staring JMX Connector thread to listen at: {}", url);
                      cs.start();
                      LOG.info("JMX Connector thread started and listening at: {}", url);
                    } catch (IOException ioe) {
                      LOG.warn(
                          "Could not start JMXConnector thread at: "
                              + url
                              + ". JMX Connector not in use.",
                          ioe);
                    }
                  }
                });
    thread.start();
  }
  public void performAction(Exchange exchange) throws Exception {
    final CamelContext context = exchange.getContext();

    // Check if action and method are set as headers
    String routeId = (String) exchange.getIn().getHeader(ROUTE_ID, getRouteId());
    final String action = (String) exchange.getIn().getHeader(ACTION_ID, getAction());

    if (routeId == null) {
      // We get the from route id in the case the routeId is still null
      // please note that the route id we obtain here is the route which
      // initiated the flow and not necessary this particular route.
      routeId = exchange.getFromRouteId();
    }

    if (routeId.equals(exchange.getFromRouteId())) {
      // Run the controlling in a separate thread in order for the current
      // flow to finish
      final String finalRouteId = routeId;
      context
          .getExecutorServiceManager()
          .newSingleThreadExecutor(this, "RouteControllerSelf")
          .execute(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    performAction(finalRouteId, action, context);
                  } catch (Exception e) {
                    // Cannot do much with this exception except log it
                    logger.warn(
                        "Exception thrown while attempting to "
                            + action
                            + " route "
                            + finalRouteId
                            + " asynchronously.",
                        e);
                  }
                }
              });
    } else {
      performAction(routeId, action, context);
    }
  }
Exemplo n.º 3
0
 /**
  * Strategy to create the thread pool for the aggregator background task which waits for and
  * aggregates completed tasks when running in parallel mode.
  *
  * @param name the suggested name for the background thread
  * @return the thread pool
  */
 protected synchronized ExecutorService createAggregateExecutorService(String name) {
   // use a cached thread pool so we each on-the-fly task has a dedicated thread to process
   // completions as they come in
   return camelContext.getExecutorServiceManager().newCachedThreadPool(this, name);
 }
Exemplo n.º 4
0
  @SuppressWarnings("deprecation")
  public MethodInfo(
      CamelContext camelContext,
      Class<?> type,
      Method method,
      List<ParameterInfo> parameters,
      List<ParameterInfo> bodyParameters,
      boolean hasCustomAnnotation,
      boolean hasHandlerAnnotation) {
    this.camelContext = camelContext;
    this.type = type;
    this.method = method;
    this.parameters = parameters;
    this.bodyParameters = bodyParameters;
    this.hasCustomAnnotation = hasCustomAnnotation;
    this.hasHandlerAnnotation = hasHandlerAnnotation;
    this.parametersExpression = createParametersExpression();

    Map<Class<?>, Annotation> collectedMethodAnnotation = collectMethodAnnotations(type, method);

    Pattern oneway = findOneWayAnnotation(method);
    if (oneway != null) {
      pattern = oneway.value();
    }

    org.apache.camel.RoutingSlip routingSlipAnnotation =
        (org.apache.camel.RoutingSlip)
            collectedMethodAnnotation.get(org.apache.camel.RoutingSlip.class);
    if (routingSlipAnnotation != null && matchContext(routingSlipAnnotation.context())) {
      routingSlip = new RoutingSlip(camelContext);
      routingSlip.setDelimiter(routingSlipAnnotation.delimiter());
      routingSlip.setIgnoreInvalidEndpoints(routingSlipAnnotation.ignoreInvalidEndpoints());
      // add created routingSlip as a service so we have its lifecycle managed
      try {
        camelContext.addService(routingSlip);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }

    org.apache.camel.DynamicRouter dynamicRouterAnnotation =
        (org.apache.camel.DynamicRouter)
            collectedMethodAnnotation.get(org.apache.camel.DynamicRouter.class);
    if (dynamicRouterAnnotation != null && matchContext(dynamicRouterAnnotation.context())) {
      dynamicRouter = new DynamicRouter(camelContext);
      dynamicRouter.setDelimiter(dynamicRouterAnnotation.delimiter());
      dynamicRouter.setIgnoreInvalidEndpoints(dynamicRouterAnnotation.ignoreInvalidEndpoints());
      // add created dynamicRouter as a service so we have its lifecycle managed
      try {
        camelContext.addService(dynamicRouter);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }

    org.apache.camel.RecipientList recipientListAnnotation =
        (org.apache.camel.RecipientList)
            collectedMethodAnnotation.get(org.apache.camel.RecipientList.class);
    if (recipientListAnnotation != null && matchContext(recipientListAnnotation.context())) {
      recipientList = new RecipientList(camelContext, recipientListAnnotation.delimiter());
      recipientList.setStopOnException(recipientListAnnotation.stopOnException());
      recipientList.setIgnoreInvalidEndpoints(recipientListAnnotation.ignoreInvalidEndpoints());
      recipientList.setParallelProcessing(recipientListAnnotation.parallelProcessing());
      recipientList.setParallelAggregate(recipientListAnnotation.parallelAggregate());
      recipientList.setStreaming(recipientListAnnotation.streaming());
      recipientList.setTimeout(recipientListAnnotation.timeout());
      recipientList.setShareUnitOfWork(recipientListAnnotation.shareUnitOfWork());

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.executorServiceRef())) {
        ExecutorService executor =
            camelContext
                .getExecutorServiceManager()
                .newDefaultThreadPool(this, recipientListAnnotation.executorServiceRef());
        recipientList.setExecutorService(executor);
      }

      if (recipientListAnnotation.parallelProcessing()
          && recipientList.getExecutorService() == null) {
        // we are running in parallel so we need a thread pool
        ExecutorService executor =
            camelContext.getExecutorServiceManager().newDefaultThreadPool(this, "@RecipientList");
        recipientList.setExecutorService(executor);
      }

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.strategyRef())) {
        AggregationStrategy strategy =
            CamelContextHelper.mandatoryLookup(
                camelContext, recipientListAnnotation.strategyRef(), AggregationStrategy.class);
        recipientList.setAggregationStrategy(strategy);
      }

      if (ObjectHelper.isNotEmpty(recipientListAnnotation.onPrepareRef())) {
        Processor onPrepare =
            CamelContextHelper.mandatoryLookup(
                camelContext, recipientListAnnotation.onPrepareRef(), Processor.class);
        recipientList.setOnPrepare(onPrepare);
      }

      // add created recipientList as a service so we have its lifecycle managed
      try {
        camelContext.addService(recipientList);
      } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
      }
    }
  }