Search in sources :

Example 11 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException in project camel by apache.

the class ReplyManagerSupport method processReply.

public void processReply(ReplyHolder holder) {
    if (holder != null && isRunAllowed()) {
        try {
            Exchange exchange = holder.getExchange();
            boolean timeout = holder.isTimeout();
            if (timeout) {
                // timeout occurred do a WARN log so its easier to spot in the logs
                if (log.isWarnEnabled()) {
                    log.warn("Timeout occurred after {} millis waiting for reply message with correlationID [{}] on destination {}." + " Setting ExchangeTimedOutException on {} and continue routing.", holder.getRequestTimeout(), holder.getCorrelationId(), replyTo, ExchangeHelper.logIds(exchange));
                }
                // no response, so lets set a timed out exception
                String msg = "reply message with correlationID: " + holder.getCorrelationId() + " not received on destination: " + replyTo;
                exchange.setException(new ExchangeTimedOutException(exchange, holder.getRequestTimeout(), msg));
            } else {
                messageConverter.populateRabbitExchange(exchange, null, holder.getProperties(), holder.getMessage(), true);
                // restore correlation id in case the remote server messed with it
                if (holder.getOriginalCorrelationId() != null) {
                    if (exchange.hasOut()) {
                        exchange.getOut().setHeader(RabbitMQConstants.CORRELATIONID, holder.getOriginalCorrelationId());
                    } else {
                        exchange.getIn().setHeader(RabbitMQConstants.CORRELATIONID, holder.getOriginalCorrelationId());
                    }
                }
            }
        } finally {
            // notify callback
            AsyncCallback callback = holder.getCallback();
            callback.done(false);
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 12 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException in project camel by apache.

the class RouteboxSedaProducer method process.

public boolean process(final Exchange exchange, final AsyncCallback callback) {
    // use a new copy of the exchange to route async and handover the on completion to the new copy
    // so its the new copy that performs the on completion callback when its done
    Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, true);
    // set a new from endpoint to be the seda queue
    copy.setFromEndpoint(endpoint);
    WaitForTaskToComplete wait = waitForTaskToComplete;
    if (exchange.getProperty(Exchange.ASYNC_WAIT) != null) {
        wait = exchange.getProperty(Exchange.ASYNC_WAIT, WaitForTaskToComplete.class);
    }
    if (wait == WaitForTaskToComplete.Always || (wait == WaitForTaskToComplete.IfReplyExpected && ExchangeHelper.isOutCapable(exchange))) {
        // latch that waits until we are complete
        final CountDownLatch latch = new CountDownLatch(1);
        // we should wait for the reply so install a on completion so we know when its complete
        copy.addOnCompletion(new SynchronizationAdapter() {

            @Override
            public void onDone(Exchange response) {
                // check for timeout, which then already would have invoked the latch
                if (latch.getCount() == 0) {
                    if (log.isTraceEnabled()) {
                        log.trace("{}. Timeout occurred so response will be ignored: {}", this, response.hasOut() ? response.getOut() : response.getIn());
                    }
                    return;
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace("{} with response: {}", this, response.hasOut() ? response.getOut() : response.getIn());
                    }
                    try {
                        ExchangeHelper.copyResults(exchange, response);
                    } finally {
                        // always ensure latch is triggered
                        latch.countDown();
                    }
                }
            }

            @Override
            public boolean allowHandover() {
                return false;
            }

            @Override
            public String toString() {
                return "onDone at [" + endpoint.getEndpointUri() + "]";
            }
        });
        queue.add(copy);
        if (timeout > 0) {
            log.trace("Waiting for task to complete using timeout (ms): {} at [{}]", timeout, endpoint.getEndpointUri());
            // lets see if we can get the task done before the timeout
            boolean done = false;
            try {
                done = latch.await(timeout, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
            // ignore
            }
            if (!done) {
                exchange.setException(new ExchangeTimedOutException(exchange, timeout));
                // count down to indicate timeout
                latch.countDown();
            }
        } else {
            log.trace("Waiting for task to complete (blocking) at [{}]", endpoint.getEndpointUri());
            // no timeout then wait until its done
            try {
                latch.await();
            } catch (InterruptedException e) {
            // ignore
            }
        }
    } else {
        // no wait, eg its a InOnly then just add to queue and return
        queue.add(copy);
    }
    // we use OnCompletion on the Exchange to callback and wait for the Exchange to be done
    // so we should just signal the callback we are done synchronously
    callback.done(true);
    return true;
}
Also used : Exchange(org.apache.camel.Exchange) WaitForTaskToComplete(org.apache.camel.WaitForTaskToComplete) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 13 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException in project camel by apache.

the class JettyContentExchange9 method onExpire.

protected void onExpire() {
    LOG.trace("onExpire");
    // need to close the request input stream
    closeRequestContentSource();
    doTaskCompleted(new ExchangeTimedOutException(exchange, client.getConnectTimeout()));
}
Also used : ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 14 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException in project camel by apache.

the class Mina2ExchangeTimeOutTest method testUsingTimeoutParameter.

@Test
public void testUsingTimeoutParameter() throws Exception {
    // use a timeout value of 2 seconds (timeout is in millis) so we should actually get a response in this test
    Endpoint endpoint = context.getEndpoint(String.format("mina2:tcp://localhost:%1$s?textline=true&sync=true&timeout=500", getPort()));
    Producer producer = endpoint.createProducer();
    producer.start();
    Exchange exchange = endpoint.createExchange();
    exchange.getIn().setBody("Hello World");
    try {
        producer.process(exchange);
        fail("Should have thrown an ExchangeTimedOutException wrapped in a RuntimeCamelException");
    } catch (Exception e) {
        assertTrue("Should have thrown an ExchangeTimedOutException", e instanceof ExchangeTimedOutException);
    }
    producer.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) Test(org.junit.Test)

Example 15 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException in project camel by apache.

the class DisruptorProducer method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    WaitForTaskToComplete wait = waitForTaskToComplete;
    if (exchange.getProperty(Exchange.ASYNC_WAIT) != null) {
        wait = exchange.getProperty(Exchange.ASYNC_WAIT, WaitForTaskToComplete.class);
    }
    if (wait == WaitForTaskToComplete.Always || (wait == WaitForTaskToComplete.IfReplyExpected && ExchangeHelper.isOutCapable(exchange))) {
        // do not handover the completion as we wait for the copy to complete, and copy its result back when it done
        final Exchange copy = prepareCopy(exchange, false);
        // latch that waits until we are complete
        final CountDownLatch latch = new CountDownLatch(1);
        // we should wait for the reply so install a on completion so we know when its complete
        copy.addOnCompletion(new SynchronizationAdapter() {

            @Override
            public void onDone(final Exchange response) {
                // check for timeout, which then already would have invoked the latch
                if (latch.getCount() == 0) {
                    if (log.isTraceEnabled()) {
                        log.trace("{}. Timeout occurred so response will be ignored: {}", this, response.hasOut() ? response.getOut() : response.getIn());
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace("{} with response: {}", this, response.hasOut() ? response.getOut() : response.getIn());
                    }
                    try {
                        ExchangeHelper.copyResults(exchange, response);
                    } finally {
                        // always ensure latch is triggered
                        latch.countDown();
                    }
                }
            }

            @Override
            public boolean allowHandover() {
                // at this point in the routing (at this leg), instead of at the very last (this ensure timeout is honored)
                return false;
            }

            @Override
            public String toString() {
                return "onDone at endpoint: " + endpoint;
            }
        });
        doPublish(copy);
        if (timeout > 0) {
            if (log.isTraceEnabled()) {
                log.trace("Waiting for task to complete using timeout (ms): {} at [{}]", timeout, endpoint.getEndpointUri());
            }
            // lets see if we can get the task done before the timeout
            boolean done = false;
            try {
                done = latch.await(timeout, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
            // ignore
            }
            if (!done) {
                // Remove timed out Exchange from disruptor endpoint.
                // We can't actually remove a published exchange from an active Disruptor.
                // Instead we prevent processing of the exchange by setting a Property on the exchange and the value
                // would be an AtomicBoolean. This is set by the Producer and the Consumer would look up that Property and
                // check the AtomicBoolean. If the AtomicBoolean says that we are good to proceed, it will process the
                // exchange. If false, it will simply disregard the exchange.
                // But since the Property map is a Concurrent one, maybe we don't need the AtomicBoolean. Check with Simon.
                // Also check the TimeoutHandler of the new Disruptor 3.0.0, consider making the switch to the latest version.
                exchange.setProperty(DisruptorEndpoint.DISRUPTOR_IGNORE_EXCHANGE, true);
                exchange.setException(new ExchangeTimedOutException(exchange, timeout));
                // count down to indicate timeout
                latch.countDown();
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Waiting for task to complete (blocking) at [{}]", endpoint.getEndpointUri());
            }
            // no timeout then wait until its done
            try {
                latch.await();
            } catch (InterruptedException e) {
            // ignore
            }
        }
    } else {
        // no wait, eg its a InOnly then just publish to the ringbuffer and return
        // handover the completion so its the copy which performs that, as we do not wait
        final Exchange copy = prepareCopy(exchange, true);
        doPublish(copy);
    }
    // we use OnCompletion on the Exchange to callback and wait for the Exchange to be done
    // so we should just signal the callback we are done synchronously
    callback.done(true);
    return true;
}
Also used : Exchange(org.apache.camel.Exchange) WaitForTaskToComplete(org.apache.camel.WaitForTaskToComplete) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Aggregations

ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)23 Exchange (org.apache.camel.Exchange)11 CountDownLatch (java.util.concurrent.CountDownLatch)5 CamelExecutionException (org.apache.camel.CamelExecutionException)4 StopWatch (org.apache.camel.util.StopWatch)4 Test (org.junit.Test)4 TimeoutException (java.util.concurrent.TimeoutException)3 AsyncCallback (org.apache.camel.AsyncCallback)3 Processor (org.apache.camel.Processor)3 WaitForTaskToComplete (org.apache.camel.WaitForTaskToComplete)3 SynchronizationAdapter (org.apache.camel.support.SynchronizationAdapter)3 CamelExchangeException (org.apache.camel.CamelExchangeException)2 Endpoint (org.apache.camel.Endpoint)2 Producer (org.apache.camel.Producer)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 EtcdKeyDeleteRequest (mousio.etcd4j.requests.EtcdKeyDeleteRequest)1 EtcdKeyGetRequest (mousio.etcd4j.requests.EtcdKeyGetRequest)1