Search in sources :

Example 6 with ExchangeTimedOutException

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

the class DefaultExceptionPolicyStrategyTest method testClosetMatch1.

public void testClosetMatch1() {
    setupPolicies();
    OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ValidationException(null, ""));
    assertEquals(type1, result);
    result = strategy.getExceptionPolicy(policies, null, new ExchangeTimedOutException(null, 0));
    assertEquals(type1, result);
}
Also used : ValidationException(org.apache.camel.ValidationException) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

Example 7 with ExchangeTimedOutException

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

the class JettyHttpProducerTimeoutTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    // give Jetty time to startup properly
    Thread.sleep(1000);
    final MyInputStream is = new MyInputStream("Content".getBytes());
    Exchange reply = template.request(url, new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(is);
        }
    });
    Exception e = reply.getException();
    assertNotNull("Should have thrown an exception", e);
    ExchangeTimedOutException cause = assertIsInstanceOf(ExchangeTimedOutException.class, e);
    assertEquals(2000, cause.getTimeout());
    assertTrue("The input stream should be closed", is.isClosed());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 8 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.", new Object[] { 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 {
                Message message = holder.getMessage();
                Session session = holder.getSession();
                JmsMessage response = new JmsMessage(message, session, endpoint.getBinding());
                // the JmsBinding is designed to be "pull-based": it will populate the Camel message on demand
                // therefore, we link Exchange and OUT message before continuing, so that the JmsBinding has full access 
                // to everything it may need, and can populate headers, properties, etc. accordingly (solves CAMEL-6218).
                exchange.setOut(response);
                Object body = response.getBody();
                if (endpoint.isTransferException() && body instanceof Exception) {
                    log.debug("Reply was an Exception. Setting the Exception on the Exchange: {}", body);
                    // we got an exception back and endpoint was configured to transfer exception
                    // therefore set response as exception
                    exchange.setException((Exception) body);
                } else {
                    log.debug("Reply received. OUT message body set to reply payload: {}", body);
                }
                if (endpoint.isTransferFault()) {
                    // remove the header as we do not want to keep it on the Camel Message either
                    Object faultHeader = response.removeHeader(JmsConstants.JMS_TRANSFER_FAULT);
                    if (faultHeader != null) {
                        boolean isFault = exchange.getContext().getTypeConverter().tryConvertTo(boolean.class, faultHeader);
                        log.debug("Transfer fault on OUT message: {}", isFault);
                        if (isFault) {
                            exchange.getOut().setFault(true);
                        }
                    }
                }
                // restore correlation id in case the remote server messed with it
                if (holder.getOriginalCorrelationId() != null) {
                    JmsMessageHelper.setCorrelationId(message, holder.getOriginalCorrelationId());
                    exchange.getOut().setHeader("JMSCorrelationID", holder.getOriginalCorrelationId());
                }
            }
        } finally {
            // notify callback
            AsyncCallback callback = holder.getCallback();
            callback.done(false);
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(javax.jms.Message) JmsMessage(org.apache.camel.component.jms.JmsMessage) AsyncCallback(org.apache.camel.AsyncCallback) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) JMSException(javax.jms.JMSException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) Session(javax.jms.Session) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Example 9 with ExchangeTimedOutException

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

the class MinaExchangeTimeOutTest 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("mina:tcp://localhost:{{port}}?textline=true&sync=true&timeout=2000");
    Producer producer = endpoint.createProducer();
    producer.start();
    Exchange exchange = producer.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 10 with ExchangeTimedOutException

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

the class Mina2Producer method doProcess.

@SuppressWarnings("deprecation")
protected void doProcess(Exchange exchange) throws Exception {
    if (session == null && !lazySessionCreation) {
        throw new IllegalStateException("Not started yet!");
    }
    if (session == null || !session.isConnected()) {
        openConnection();
    }
    // set the exchange encoding property
    if (getEndpoint().getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOConverter.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
    }
    Object body = Mina2PayloadHelper.getIn(getEndpoint(), exchange);
    if (body == null) {
        noReplyLogger.log("No payload to send for exchange: " + exchange);
        // exit early since nothing to write
        return;
    }
    // if textline enabled then covert to a String which must be used for textline
    if (getEndpoint().getConfiguration().isTextline()) {
        body = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, body);
    }
    // if sync is true then we should also wait for a response (synchronous mode)
    if (sync) {
        // only initialize responseLatch if we should get a response
        responseLatch = new CountDownLatch(1);
        // reset handler if we expect a response
        handler.reset();
    }
    // log what we are writing
    if (LOG.isDebugEnabled()) {
        Object out = body;
        if (body instanceof byte[]) {
            // byte arrays is not readable so convert to string
            out = exchange.getContext().getTypeConverter().convertTo(String.class, body);
        }
        LOG.debug("Writing body: {}", out);
    }
    // write the body
    Mina2Helper.writeBody(session, body, exchange);
    if (sync) {
        // wait for response, consider timeout
        LOG.debug("Waiting for response using timeout {} millis.", timeout);
        boolean done = responseLatch.await(timeout, TimeUnit.MILLISECONDS);
        if (!done) {
            throw new ExchangeTimedOutException(exchange, timeout);
        }
        // did we get a response
        if (handler.getCause() != null) {
            throw new CamelExchangeException("Error occurred in ResponseHandler", exchange, handler.getCause());
        } else if (!handler.isMessageReceived()) {
            // no message received
            throw new ExchangeTimedOutException(exchange, timeout);
        } else {
            // set the result on either IN or OUT on the original exchange depending on its pattern
            if (ExchangeHelper.isOutCapable(exchange)) {
                Mina2PayloadHelper.setOut(exchange, handler.getMessage());
            } else {
                Mina2PayloadHelper.setIn(exchange, handler.getMessage());
            }
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

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