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;
}
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;
}
}
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);
}
}
}
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()));
}
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);
}
Aggregations