Search in sources :

Example 71 with StopWatch

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

the class AggregateProcessor method doForceCompletionOnStop.

private void doForceCompletionOnStop() {
    int expected = forceCompletionOfAllGroups();
    StopWatch watch = new StopWatch();
    while (inProgressCompleteExchanges.size() > 0) {
        LOG.trace("Waiting for {} inflight exchanges to complete", getInProgressCompleteExchanges());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // break out as we got interrupted such as the JVM terminating
            LOG.warn("Interrupted while waiting for {} inflight exchanges to complete.", getInProgressCompleteExchanges());
            break;
        }
    }
    if (expected > 0) {
        LOG.info("Forcing completion of all groups with {} exchanges completed in {}", expected, TimeUtils.printDuration(watch.stop()));
    }
}
Also used : Endpoint(org.apache.camel.Endpoint) StopWatch(org.apache.camel.util.StopWatch)

Example 72 with StopWatch

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

the class SendProcessor method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    if (!isStarted()) {
        exchange.setException(new IllegalStateException("SendProcessor has not been started: " + this));
        callback.done(true);
        return true;
    }
    // we should preserve existing MEP so remember old MEP
    // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
    final ExchangePattern existingPattern = exchange.getPattern();
    counter++;
    // if we have a producer then use that as its optimized
    if (producer != null) {
        // record timing for sending the exchange using the producer
        final StopWatch watch = new StopWatch();
        final Exchange target = configureExchange(exchange, pattern);
        EventHelper.notifyExchangeSending(exchange.getContext(), target, destination);
        LOG.debug(">>>> {} {}", destination, exchange);
        boolean sync = true;
        try {
            sync = producer.process(exchange, new AsyncCallback() {

                @Override
                public void done(boolean doneSync) {
                    try {
                        // restore previous MEP
                        target.setPattern(existingPattern);
                        // emit event that the exchange was sent to the endpoint
                        long timeTaken = watch.stop();
                        EventHelper.notifyExchangeSent(target.getContext(), target, destination, timeTaken);
                    } finally {
                        callback.done(doneSync);
                    }
                }
            });
        } catch (Throwable throwable) {
            exchange.setException(throwable);
            callback.done(sync);
        }
        return sync;
    }
    // send the exchange to the destination using the producer cache for the non optimized producers
    return producerCache.doInAsyncProducer(destination, exchange, pattern, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern pattern, final AsyncCallback callback) {
            final Exchange target = configureExchange(exchange, pattern);
            LOG.debug(">>>> {} {}", destination, exchange);
            return asyncProducer.process(target, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // restore previous MEP
                    target.setPattern(existingPattern);
                    // signal we are done
                    callback.done(doneSync);
                }
            });
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Producer(org.apache.camel.Producer) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback) StopWatch(org.apache.camel.util.StopWatch)

Example 73 with StopWatch

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

the class Debug method wrapProcessorInInterceptors.

public Processor wrapProcessorInInterceptors(final CamelContext context, final ProcessorDefinition<?> definition, final Processor target, final Processor nextTarget) throws Exception {
    return new DelegateAsyncProcessor(target) {

        @Override
        public boolean process(final Exchange exchange, final AsyncCallback callback) {
            debugger.beforeProcess(exchange, target, definition);
            final StopWatch watch = new StopWatch();
            return processor.process(exchange, new AsyncCallback() {

                public void done(boolean doneSync) {
                    long diff = watch.stop();
                    debugger.afterProcess(exchange, processor, definition, diff);
                    // must notify original callback
                    callback.done(doneSync);
                }
            });
        }

        @Override
        public String toString() {
            return "Debug[" + target + "]";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) DelegateAsyncProcessor(org.apache.camel.processor.DelegateAsyncProcessor) StopWatch(org.apache.camel.util.StopWatch)

Example 74 with StopWatch

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

the class BeanOgnlPerformanceTest method testBeanOgnlPerformance.

public void testBeanOgnlPerformance() throws Exception {
    StopWatch watch = new StopWatch();
    getMockEndpoint("mock:result").expectedMessageCount(size);
    for (int i = 0; i < size; i++) {
        template.sendBody("direct:start", "Hello World");
    }
    assertMockEndpointsSatisfied();
    log.info("Took {} millis", watch.stop());
}
Also used : StopWatch(org.apache.camel.util.StopWatch)

Example 75 with StopWatch

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

the class SedaInOutChainedTimeoutTest method testSedaInOutChainedTimeout.

public void testSedaInOutChainedTimeout() throws Exception {
    // time timeout after 2 sec should trigger a immediately reply
    StopWatch watch = new StopWatch();
    try {
        template.requestBody("seda:a?timeout=5000", "Hello World");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        ExchangeTimedOutException cause = assertIsInstanceOf(ExchangeTimedOutException.class, e.getCause());
        assertEquals(2000, cause.getTimeout());
    }
    long delta = watch.stop();
    assertTrue("Should be faster than 4000 millis, was: " + delta, delta < 4000);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) 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