Search in sources :

Example 91 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class MulticastProcessor method doProcessSequential.

private boolean doProcessSequential(final Exchange original, final AtomicExchange result, final Iterable<ProcessorExchangePair> pairs, final Iterator<ProcessorExchangePair> it, final ProcessorExchangePair pair, final AsyncCallback callback, final AtomicInteger total) {
    boolean sync = true;
    final Exchange exchange = pair.getExchange();
    Processor processor = pair.getProcessor();
    final Producer producer = pair.getProducer();
    TracedRouteNodes traced = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getTracedRouteNodes() : null;
    // compute time taken if sending to another endpoint
    final StopWatch watch = producer != null ? new StopWatch() : null;
    try {
        // prepare tracing starting from a new block
        if (traced != null) {
            traced.pushBlock();
        }
        if (producer != null) {
            EventHelper.notifyExchangeSending(exchange.getContext(), exchange, producer.getEndpoint());
        }
        // let the prepared process it, remember to begin the exchange pair
        AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
        pair.begin();
        sync = async.process(exchange, new AsyncCallback() {

            public void done(boolean doneSync) {
                // we are done with the exchange pair
                pair.done();
                // okay we are done, so notify the exchange was sent
                if (producer != null) {
                    long timeTaken = watch.stop();
                    Endpoint endpoint = producer.getEndpoint();
                    // emit event that the exchange was sent to the endpoint
                    EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
                }
                // we only have to handle async completion of the routing slip
                if (doneSync) {
                    return;
                }
                // continue processing the multicast asynchronously
                Exchange subExchange = exchange;
                // Decide whether to continue with the multicast or not; similar logic to the Pipeline
                // remember to test for stop on exception and aggregate before copying back results
                boolean continueProcessing = PipelineHelper.continueProcessing(subExchange, "Sequential processing failed for number " + total.get(), LOG);
                if (stopOnException && !continueProcessing) {
                    if (subExchange.getException() != null) {
                        // wrap in exception to explain where it failed
                        subExchange.setException(new CamelExchangeException("Sequential processing failed for number " + total, subExchange, subExchange.getException()));
                    } else {
                        // we want to stop on exception, and the exception was handled by the error handler
                        // this is similar to what the pipeline does, so we should do the same to not surprise end users
                        // so we should set the failed exchange as the result and be done
                        result.set(subExchange);
                    }
                    // and do the done work
                    doDone(original, subExchange, pairs, callback, false, true);
                    return;
                }
                try {
                    if (parallelAggregate) {
                        doAggregateInternal(getAggregationStrategy(subExchange), result, subExchange);
                    } else {
                        doAggregate(getAggregationStrategy(subExchange), result, subExchange);
                    }
                } catch (Throwable e) {
                    // wrap in exception to explain where it failed
                    subExchange.setException(new CamelExchangeException("Sequential processing failed for number " + total, subExchange, e));
                    // and do the done work
                    doDone(original, subExchange, pairs, callback, false, true);
                    return;
                }
                total.incrementAndGet();
                // maybe there are more processors to multicast
                while (it.hasNext()) {
                    // prepare and run the next
                    ProcessorExchangePair pair = it.next();
                    subExchange = pair.getExchange();
                    updateNewExchange(subExchange, total.get(), pairs, it);
                    boolean sync = doProcessSequential(original, result, pairs, it, pair, callback, total);
                    if (!sync) {
                        LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", original.getExchangeId());
                        return;
                    }
                    // Decide whether to continue with the multicast or not; similar logic to the Pipeline
                    // remember to test for stop on exception and aggregate before copying back results
                    continueProcessing = PipelineHelper.continueProcessing(subExchange, "Sequential processing failed for number " + total.get(), LOG);
                    if (stopOnException && !continueProcessing) {
                        if (subExchange.getException() != null) {
                            // wrap in exception to explain where it failed
                            subExchange.setException(new CamelExchangeException("Sequential processing failed for number " + total, subExchange, subExchange.getException()));
                        } else {
                            // we want to stop on exception, and the exception was handled by the error handler
                            // this is similar to what the pipeline does, so we should do the same to not surprise end users
                            // so we should set the failed exchange as the result and be done
                            result.set(subExchange);
                        }
                        // and do the done work
                        doDone(original, subExchange, pairs, callback, false, true);
                        return;
                    }
                    // must catch any exceptions from aggregation
                    try {
                        if (parallelAggregate) {
                            doAggregateInternal(getAggregationStrategy(subExchange), result, subExchange);
                        } else {
                            doAggregate(getAggregationStrategy(subExchange), result, subExchange);
                        }
                    } catch (Throwable e) {
                        // wrap in exception to explain where it failed
                        subExchange.setException(new CamelExchangeException("Sequential processing failed for number " + total, subExchange, e));
                        // and do the done work
                        doDone(original, subExchange, pairs, callback, false, true);
                        return;
                    }
                    total.incrementAndGet();
                }
                // do the done work
                subExchange = result.get() != null ? result.get() : null;
                doDone(original, subExchange, pairs, callback, false, true);
            }
        });
    } finally {
        // pop the block so by next round we have the same staring point and thus the tracing looks accurate
        if (traced != null) {
            traced.popBlock();
        }
    }
    return sync;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) AsyncProcessor(org.apache.camel.AsyncProcessor) Processor(org.apache.camel.Processor) AsyncCallback(org.apache.camel.AsyncCallback) StopWatch(org.apache.camel.util.StopWatch) AtomicExchange(org.apache.camel.util.concurrent.AtomicExchange) Exchange(org.apache.camel.Exchange) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint) AsyncProcessor(org.apache.camel.AsyncProcessor) TracedRouteNodes(org.apache.camel.spi.TracedRouteNodes)

Example 92 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class Enricher method process.

/**
     * 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 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.
     */
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    // which producer to use
    final Producer producer;
    final 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
        producer = producerCache.acquireProducer(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;
    }
    final Exchange resourceExchange = createResourceExchange(exchange, ExchangePattern.InOut);
    final Endpoint destination = producer.getEndpoint();
    EventHelper.notifyExchangeSending(exchange.getContext(), resourceExchange, destination);
    // record timing for sending the exchange using the producer
    final StopWatch watch = new StopWatch();
    AsyncProcessor ap = AsyncProcessorConverterHelper.convert(producer);
    boolean sync = ap.process(resourceExchange, new AsyncCallback() {

        public void done(boolean doneSync) {
            // we only have to handle async completion of the routing slip
            if (doneSync) {
                return;
            }
            // emit event that the exchange was sent to the endpoint
            long timeTaken = watch.stop();
            EventHelper.notifyExchangeSent(resourceExchange.getContext(), resourceExchange, destination, timeTaken);
            if (!isAggregateOnException() && 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);
                    Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
                    if (aggregatedExchange != null) {
                        // copy aggregation result onto original exchange (preserving pattern)
                        copyResultsPreservePattern(exchange, aggregatedExchange);
                    }
                } catch (Throwable e) {
                    // if the aggregationStrategy threw an exception, set it on the original exchange
                    exchange.setException(new CamelExchangeException("Error occurred during aggregation", exchange, e));
                    callback.done(false);
                    // we failed so break out now
                    return;
                }
            }
            // set property with the uri of the endpoint enriched so we can use that for tracing etc
            exchange.setProperty(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri());
            // return the producer back to the cache
            try {
                producerCache.releaseProducer(endpoint, producer);
            } catch (Exception e) {
            // ignore
            }
            callback.done(false);
        }
    });
    if (!sync) {
        LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId());
        // so we break out now, then the callback will be invoked which then continue routing from where we left here
        return false;
    }
    LOG.trace("Processing exchangeId: {} is continued being processed synchronously", exchange.getExchangeId());
    // emit event that the exchange was sent to the endpoint
    long timeTaken = watch.stop();
    EventHelper.notifyExchangeSent(resourceExchange.getContext(), resourceExchange, destination, timeTaken);
    if (!isAggregateOnException() && 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);
            Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
            if (aggregatedExchange != null) {
                // copy aggregation result onto original exchange (preserving pattern)
                copyResultsPreservePattern(exchange, aggregatedExchange);
            }
        } catch (Throwable e) {
            // if the aggregationStrategy threw an exception, set it on the original exchange
            exchange.setException(new CamelExchangeException("Error occurred during aggregation", exchange, e));
            callback.done(true);
            // we failed so break out now
            return true;
        }
    }
    // set property with the uri of the endpoint enriched so we can use that for tracing etc
    exchange.setProperty(Exchange.TO_ENDPOINT, producer.getEndpoint().getEndpointUri());
    // return the producer back to the cache
    try {
        producerCache.releaseProducer(endpoint, producer);
    } catch (Exception e) {
    // ignore
    }
    callback.done(true);
    return true;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) CamelExchangeException(org.apache.camel.CamelExchangeException) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback) CamelExchangeException(org.apache.camel.CamelExchangeException) StopWatch(org.apache.camel.util.StopWatch)

Example 93 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class DirectVmProducerBlockingTest method testProducerBlocksForSuspendedConsumer.

public void testProducerBlocksForSuspendedConsumer() throws Exception {
    DirectVmEndpoint endpoint = getMandatoryEndpoint("direct-vm:suspended", DirectVmEndpoint.class);
    endpoint.getConsumer().suspend();
    StopWatch watch = new StopWatch();
    try {
        template.sendBody("direct-vm:suspended?block=true&timeout=2000", "hello world");
        fail("Expected CamelExecutionException");
    } catch (CamelExecutionException e) {
        DirectVmConsumerNotAvailableException cause = assertIsInstanceOf(DirectVmConsumerNotAvailableException.class, e.getCause());
        assertIsInstanceOf(CamelExchangeException.class, cause);
        assertTrue(watch.taken() > 1500);
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) StopWatch(org.apache.camel.util.StopWatch)

Example 94 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class RedeliveryErrorHandlerBreakoutDuringShutdownTest method testRedelivery.

public void testRedelivery() throws Exception {
    getMockEndpoint("mock:before").expectedMessageCount(1);
    getMockEndpoint("mock:after").expectedMessageCount(0);
    template.sendBody("seda:start", "Hello World");
    assertMockEndpointsSatisfied();
    // use a stop watch to time how long it takes to force the shutdown
    StopWatch watch = new StopWatch();
    // force quicker shutdown
    context.getShutdownStrategy().setTimeout(1);
    context.stop();
    // should take less than 5 seconds
    assertTrue("Should take less than 5 seconds, was {}", watch.stop() < 5000);
}
Also used : StopWatch(org.apache.camel.util.StopWatch)

Example 95 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class RoutePerformanceTest method testPerformance.

public void testPerformance() throws Exception {
    StopWatch watch = new StopWatch();
    MockEndpoint endpoint = getMockEndpoint(uri);
    endpoint.expectedMessageCount((int) dataSet.getSize());
    endpoint.expectedHeaderReceived("foo", 123);
    // wait 30 sec for slow servers
    MockEndpoint.assertIsSatisfied(context, 30, TimeUnit.SECONDS);
    log.info("RoutePerformanceTest: Sent: " + size + " Took: " + watch.taken() + " ms");
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StopWatch(org.apache.camel.util.StopWatch)

Aggregations

StopWatch (org.apache.camel.util.StopWatch)101 Test (org.junit.Test)40 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 CamelExecutionException (org.apache.camel.CamelExecutionException)10 Exchange (org.apache.camel.Exchange)8 CamelExchangeException (org.apache.camel.CamelExchangeException)6 File (java.io.File)5 ExecutorService (java.util.concurrent.ExecutorService)5 AsyncProcessor (org.apache.camel.AsyncProcessor)5 Producer (org.apache.camel.Producer)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Future (java.util.concurrent.Future)4 AsyncCallback (org.apache.camel.AsyncCallback)4 Endpoint (org.apache.camel.Endpoint)4 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)4 NotifyBuilder (org.apache.camel.builder.NotifyBuilder)4 Date (java.util.Date)3 GenericFile (org.apache.camel.component.file.GenericFile)3