Search in sources :

Example 6 with StopWatch

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

the class DirectBlockingProducer method awaitConsumer.

private DirectConsumer awaitConsumer() throws InterruptedException {
    DirectConsumer answer = null;
    StopWatch watch = new StopWatch();
    boolean done = false;
    while (!done) {
        // sleep a bit to give chance for the consumer to be ready
        Thread.sleep(500);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Waited {} for consumer to be ready", watch.taken());
        }
        answer = endpoint.getConsumer();
        if (answer != null) {
            return answer;
        }
        // we are done if we hit the timeout
        done = watch.taken() >= endpoint.getTimeout();
    }
    return answer;
}
Also used : StopWatch(org.apache.camel.util.StopWatch)

Example 7 with StopWatch

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

the class ProducerCache method doInAsyncProducer.

/**
     * Sends an exchange to an endpoint using a supplied callback supporting the asynchronous routing engine.
     * <p/>
     * If an exception was thrown during processing, it would be set on the given Exchange
     *
     * @param endpoint         the endpoint to send the exchange to
     * @param exchange         the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
     * @param pattern          the exchange pattern, can be <tt>null</tt>
     * @param callback         the asynchronous callback
     * @param producerCallback the producer template callback to be executed
     * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
     */
public boolean doInAsyncProducer(final Endpoint endpoint, final Exchange exchange, final ExchangePattern pattern, final AsyncCallback callback, final AsyncProducerCallback producerCallback) {
    Producer target;
    try {
        // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
        target = doGetProducer(endpoint, true);
        if (target == null) {
            if (isStopped()) {
                LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
                callback.done(true);
                return true;
            } else {
                exchange.setException(new IllegalStateException("No producer, this processor has not been started: " + this));
                callback.done(true);
                return true;
            }
        }
    } catch (Throwable e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    final Producer producer = target;
    // record timing for sending the exchange using the producer
    final StopWatch watch = eventNotifierEnabled && exchange != null ? new StopWatch() : null;
    try {
        if (eventNotifierEnabled && exchange != null) {
            EventHelper.notifyExchangeSending(exchange.getContext(), exchange, endpoint);
        }
        // invoke the callback
        AsyncProcessor asyncProcessor = AsyncProcessorConverterHelper.convert(producer);
        return producerCallback.doInAsyncProducer(producer, asyncProcessor, exchange, pattern, doneSync -> {
            try {
                if (eventNotifierEnabled && watch != null) {
                    long timeTaken = watch.stop();
                    EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
                }
                if (producer instanceof ServicePoolAware) {
                    pool.release(endpoint, producer);
                } else if (!producer.isSingleton()) {
                    try {
                        ServiceHelper.stopAndShutdownService(producer);
                    } catch (Exception e) {
                        LOG.warn("Error stopping/shutting down producer: " + producer, e);
                    }
                }
            } finally {
                callback.done(doneSync);
            }
        });
    } catch (Throwable e) {
        // ensure exceptions is caught and set on the exchange
        if (exchange != null) {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
}
Also used : Producer(org.apache.camel.Producer) AsyncProcessor(org.apache.camel.AsyncProcessor) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) StopWatch(org.apache.camel.util.StopWatch)

Example 8 with StopWatch

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

the class MulticastProcessor method doProcessParallel.

private void doProcessParallel(final ProcessorExchangePair pair) throws Exception {
    final Exchange exchange = pair.getExchange();
    Processor processor = pair.getProcessor();
    Producer producer = pair.getProducer();
    TracedRouteNodes traced = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getTracedRouteNodes() : null;
    // compute time taken if sending to another endpoint
    StopWatch watch = null;
    if (producer != null) {
        watch = new StopWatch();
    }
    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();
        // we invoke it synchronously as parallel async routing is too hard
        AsyncProcessorHelper.process(async, exchange);
    } finally {
        pair.done();
        // pop the block so by next round we have the same staring point and thus the tracing looks accurate
        if (traced != null) {
            traced.popBlock();
        }
        if (producer != null) {
            long timeTaken = watch.stop();
            Endpoint endpoint = producer.getEndpoint();
            // emit event that the exchange was sent to the endpoint
            // this is okay to do here in the finally block, as the processing is not using the async routing engine
            //( we invoke it synchronously as parallel async routing is too hard)
            EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
        }
    }
}
Also used : AtomicExchange(org.apache.camel.util.concurrent.AtomicExchange) Exchange(org.apache.camel.Exchange) AsyncProcessor(org.apache.camel.AsyncProcessor) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint) AsyncProcessor(org.apache.camel.AsyncProcessor) TracedRouteNodes(org.apache.camel.spi.TracedRouteNodes) StopWatch(org.apache.camel.util.StopWatch)

Example 9 with StopWatch

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

the class ActiveMQUuidGeneratorTest method testPerformance.

public void testPerformance() {
    ActiveMQUuidGenerator uuidGenerator = new ActiveMQUuidGenerator();
    StopWatch watch = new StopWatch();
    LOG.info("First id: " + uuidGenerator.generateUuid());
    for (int i = 0; i < 500000; i++) {
        uuidGenerator.generateUuid();
    }
    LOG.info("Last id:  " + uuidGenerator.generateUuid());
    LOG.info("Took " + TimeUtils.printDuration(watch.stop()));
}
Also used : StopWatch(org.apache.camel.util.StopWatch)

Example 10 with StopWatch

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

the class RedeliveryErrorHandlerNoRedeliveryOnShutdownTest method testRedeliveryErrorHandlerNoRedeliveryOnShutdown.

public void testRedeliveryErrorHandlerNoRedeliveryOnShutdown() throws Exception {
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    template.sendBody("seda:foo", "Hello World");
    assertMockEndpointsSatisfied();
    // should not take long to stop the route
    StopWatch watch = new StopWatch();
    context.stopRoute("foo");
    watch.stop();
    assertTrue("Should stop route faster, was " + watch.taken(), watch.taken() < 4000);
}
Also used : 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