Exemplo n.º 1
0
 /**
  * Aggregate the {@link Exchange} with the current result
  *
  * @param strategy the aggregation strategy to use
  * @param result the current result
  * @param exchange the exchange to be added to the result
  */
 protected synchronized void doAggregate(
     AggregationStrategy strategy, AtomicExchange result, Exchange exchange) {
   if (strategy != null) {
     // prepare the exchanges for aggregation
     Exchange oldExchange = result.get();
     ExchangeHelper.prepareAggregation(oldExchange, exchange);
     result.set(strategy.aggregate(oldExchange, exchange));
   }
 }
Exemplo n.º 2
0
  /**
   * Enriches the input data (<code>exchange</code>) by first obtaining additional data from an
   * endpoint represented by an endpoint <code>producer</code> and second by aggregating input data
   * and additional data. Aggregation of input data and additional data is delegated to an {@link
   * org.apache.camel.processor.aggregate.AggregationStrategy} object set at construction time. If
   * the message exchange with the resource endpoint fails then no aggregation will be done and the
   * failed exchange content is copied over to the original message exchange.
   *
   * @param exchange input data.
   */
  @Override
  public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
      preCheckPoll(exchange);
    } catch (Exception e) {
      exchange.setException(new CamelExchangeException("Error during pre poll check", exchange, e));
      callback.done(true);
      return true;
    }

    Exchange resourceExchange;
    if (timeout < 0) {
      LOG.debug("Consumer receive: {}", consumer);
      resourceExchange = consumer.receive();
    } else if (timeout == 0) {
      LOG.debug("Consumer receiveNoWait: {}", consumer);
      resourceExchange = consumer.receiveNoWait();
    } else {
      LOG.debug("Consumer receive with timeout: {} ms. {}", timeout, consumer);
      resourceExchange = consumer.receive(timeout);
    }

    if (resourceExchange == null) {
      LOG.debug("Consumer received no exchange");
    } else {
      LOG.debug("Consumer received: {}", resourceExchange);
    }

    if (resourceExchange != null && resourceExchange.isFailed()) {
      // copy resource exchange onto original exchange (preserving pattern)
      copyResultsPreservePattern(exchange, resourceExchange);
    } else {
      prepareResult(exchange);

      try {
        // prepare the exchanges for aggregation
        ExchangeHelper.prepareAggregation(exchange, resourceExchange);
        // must catch any exception from aggregation
        Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
        if (aggregatedExchange != null) {
          // copy aggregation result onto original exchange (preserving pattern)
          copyResultsPreservePattern(exchange, aggregatedExchange);
          // handover any synchronization
          if (resourceExchange != null) {
            resourceExchange.handoverCompletions(exchange);
          }
        }
      } catch (Throwable e) {
        exchange.setException(
            new CamelExchangeException("Error occurred during aggregation", exchange, e));
        callback.done(true);
        return true;
      }
    }

    // set header with the uri of the endpoint enriched so we can use that for tracing etc
    if (exchange.hasOut()) {
      exchange.getOut().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
    } else {
      exchange.getIn().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
    }

    callback.done(true);
    return true;
  }
Exemplo n.º 3
0
  /**
   * Enriches the input data (<code>exchange</code>) by first obtaining additional data from an
   * endpoint represented by an endpoint <code>producer</code> and second by aggregating input data
   * and additional data. Aggregation of input data and additional data is delegated to an {@link
   * org.apache.camel.processor.aggregate.AggregationStrategy} object set at construction time. If
   * the message exchange with the resource endpoint fails then no aggregation will be done and the
   * failed exchange content is copied over to the original message exchange.
   *
   * @param exchange input data.
   */
  @Override
  public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
      preCheckPoll(exchange);
    } catch (Exception e) {
      exchange.setException(new CamelExchangeException("Error during pre poll check", exchange, e));
      callback.done(true);
      return true;
    }

    // which consumer to use
    PollingConsumer consumer;
    Endpoint endpoint;

    // use dynamic endpoint so calculate the endpoint to use
    Object recipient = null;
    try {
      recipient = expression.evaluate(exchange, Object.class);
      endpoint = resolveEndpoint(exchange, recipient);
      // acquire the consumer from the cache
      consumer = consumerCache.acquirePollingConsumer(endpoint);
    } catch (Throwable e) {
      if (isIgnoreInvalidEndpoint()) {
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Endpoint uri is invalid: " + recipient + ". This exception will be ignored.", e);
        }
      } else {
        exchange.setException(e);
      }
      callback.done(true);
      return true;
    }

    Exchange resourceExchange;
    try {
      if (timeout < 0) {
        LOG.debug("Consumer receive: {}", consumer);
        resourceExchange = consumer.receive();
      } else if (timeout == 0) {
        LOG.debug("Consumer receiveNoWait: {}", consumer);
        resourceExchange = consumer.receiveNoWait();
      } else {
        LOG.debug("Consumer receive with timeout: {} ms. {}", timeout, consumer);
        resourceExchange = consumer.receive(timeout);
      }

      if (resourceExchange == null) {
        LOG.debug("Consumer received no exchange");
      } else {
        LOG.debug("Consumer received: {}", resourceExchange);
      }
    } catch (Exception e) {
      exchange.setException(new CamelExchangeException("Error during poll", exchange, e));
      callback.done(true);
      return true;
    } finally {
      // return the consumer back to the cache
      consumerCache.releasePollingConsumer(endpoint, consumer);
    }

    try {
      if (!isAggregateOnException() && (resourceExchange != null && resourceExchange.isFailed())) {
        // copy resource exchange onto original exchange (preserving pattern)
        copyResultsPreservePattern(exchange, resourceExchange);
      } else {
        prepareResult(exchange);

        // prepare the exchanges for aggregation
        ExchangeHelper.prepareAggregation(exchange, resourceExchange);
        // must catch any exception from aggregation
        Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
        if (aggregatedExchange != null) {
          // copy aggregation result onto original exchange (preserving pattern)
          copyResultsPreservePattern(exchange, aggregatedExchange);
          // handover any synchronization
          if (resourceExchange != null) {
            resourceExchange.handoverCompletions(exchange);
          }
        }
      }

      // set header with the uri of the endpoint enriched so we can use that for tracing etc
      if (exchange.hasOut()) {
        exchange.getOut().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
      } else {
        exchange.getIn().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
      }
    } catch (Throwable e) {
      exchange.setException(
          new CamelExchangeException("Error occurred during aggregation", exchange, e));
      callback.done(true);
      return true;
    }

    callback.done(true);
    return true;
  }