Search in sources :

Example 21 with ExchangeTimedOutException

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

the class CxfRsInvoker method asyncInvoke.

private Object asyncInvoke(Exchange cxfExchange, final Object serviceObject, Method method, Object[] paramArray, final Continuation continuation, Object response) throws Exception {
    synchronized (continuation) {
        if (continuation.isNew()) {
            final org.apache.camel.Exchange camelExchange = prepareExchange(cxfExchange, method, paramArray, response);
            // we want to handle the UoW
            cxfRsConsumer.createUoW(camelExchange);
            // Now we don't set up the timeout value
            LOG.trace("Suspending continuation of exchangeId: {}", camelExchange.getExchangeId());
            // The continuation could be called before the suspend is called
            continuation.suspend(endpoint.getContinuationTimeout());
            cxfExchange.put(SUSPENED, Boolean.TRUE);
            continuation.setObject(camelExchange);
            cxfRsConsumer.getAsyncProcessor().process(camelExchange, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // make sure the continuation resume will not be called before the suspend method in other thread
                    synchronized (continuation) {
                        LOG.trace("Resuming continuation of exchangeId: {}", camelExchange.getExchangeId());
                        // resume processing after both, sync and async callbacks
                        continuation.resume();
                    }
                }
            });
            return null;
        }
        if (continuation.isResumed()) {
            cxfExchange.put(SUSPENED, Boolean.FALSE);
            org.apache.camel.Exchange camelExchange = (org.apache.camel.Exchange) continuation.getObject();
            try {
                return returnResponse(cxfExchange, camelExchange);
            } finally {
                cxfRsConsumer.doneUoW(camelExchange);
            }
        } else {
            if (!continuation.isPending()) {
                cxfExchange.put(SUSPENED, Boolean.FALSE);
                org.apache.camel.Exchange camelExchange = (org.apache.camel.Exchange) continuation.getObject();
                camelExchange.setException(new ExchangeTimedOutException(camelExchange, endpoint.getContinuationTimeout()));
                try {
                    return returnResponse(cxfExchange, camelExchange);
                } finally {
                    cxfRsConsumer.doneUoW(camelExchange);
                }
            }
        }
    }
    return null;
}
Also used : Exchange(org.apache.cxf.message.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 22 with ExchangeTimedOutException

use of org.apache.camel.ExchangeTimedOutException 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)

Example 23 with ExchangeTimedOutException

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

the class CamelRpcClientFactory method getClient.

@Override
public <S extends RpcRequest, T extends RpcResponse> RpcClient<S, T> getClient(RpcModule<S, T> module) {
    return new RpcClient<S, T>() {

        @Override
        public CompletableFuture<T> execute(S request) {
            if (request.getLocation() == null || request.getLocation().equals(location)) {
                // The request is for the current location, invoke it directly
                return module.execute(request);
            }
            // Save the context map and restore it on callback
            final Map<String, String> clientContextMap = Logging.getCopyOfContextMap();
            // Wrap the request in a CamelRpcRequest and forward it to the Camel route
            final CompletableFuture<T> future = new CompletableFuture<>();
            try {
                template.asyncCallbackSendBody(endpoint, new CamelRpcRequest<>(module, request), new Synchronization() {

                    @Override
                    public void onComplete(Exchange exchange) {
                        try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                            final T response = module.unmarshalResponse(exchange.getOut().getBody(String.class));
                            if (response.getErrorMessage() != null) {
                                future.completeExceptionally(new RemoteExecutionException(response.getErrorMessage()));
                            } else {
                                future.complete(response);
                            }
                        } catch (Throwable ex) {
                            LOG.error("Unmarshalling a response in RPC module {} failed.", module, ex);
                            future.completeExceptionally(ex);
                        }
                        // Ensure that future log statements on this thread are routed properly
                        Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
                    }

                    @Override
                    public void onFailure(Exchange exchange) {
                        try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                            final ExchangeTimedOutException timeoutException = exchange.getException(ExchangeTimedOutException.class);
                            final DirectConsumerNotAvailableException directConsumerNotAvailableException = exchange.getException(DirectConsumerNotAvailableException.class);
                            if (timeoutException != null) {
                                // Wrap timeout exceptions within a RequestTimedOutException
                                future.completeExceptionally(new RequestTimedOutException(exchange.getException()));
                            } else if (directConsumerNotAvailableException != null) {
                                // Wrap consumer not available exceptions with a RequestRejectedException
                                future.completeExceptionally(new RequestRejectedException(exchange.getException()));
                            } else {
                                future.completeExceptionally(exchange.getException());
                            }
                        }
                        // Ensure that future log statements on this thread are routed properly
                        Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
                    }
                });
            } catch (IllegalStateException e) {
                try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
                    // Wrap ProducerTemplate exceptions with a RequestRejectedException
                    future.completeExceptionally(new RequestRejectedException(e));
                }
                // Ensure that future log statements on this thread are routed properly
                Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
            }
            return future;
        }
    };
}
Also used : DirectConsumerNotAvailableException(org.apache.camel.component.direct.DirectConsumerNotAvailableException) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) Synchronization(org.apache.camel.spi.Synchronization) Exchange(org.apache.camel.Exchange) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) RpcClient(org.opennms.core.rpc.api.RpcClient)

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