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