Search in sources :

Example 21 with CamelExchangeException

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

the class MinaNoResponseFromServerTest method testNoResponse.

@Test
public void testNoResponse() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(0);
    try {
        template.requestBody("mina:tcp://localhost:{{port}}?sync=true&codec=#myCodec", "Hello World");
        fail("Should throw a CamelExchangeException");
    } catch (RuntimeCamelException e) {
        assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        assertTrue(e.getCause().getMessage().startsWith("No response received from remote server"));
    }
    mock.assertIsSatisfied();
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Example 22 with CamelExchangeException

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

Example 23 with CamelExchangeException

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

the class Mina2NoResponseFromServerTest method testNoResponse.

@Test
public void testNoResponse() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(0);
    try {
        template.requestBody(String.format("mina2:tcp://localhost:%1$s?sync=true&codec=#myCodec", getPort()), "Hello World");
        fail("Should throw a CamelExchangeException");
    } catch (RuntimeCamelException e) {
        assertIsInstanceOf(CamelExchangeException.class, e.getCause());
    }
    mock.assertIsSatisfied();
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Example 24 with CamelExchangeException

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

the class RouteboxDirectProducer method process.

public void process(Exchange exchange) throws Exception {
    Exchange result;
    if ((((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer() == null) && (getRouteboxEndpoint().getConfig().isSendToConsumer())) {
        throw new CamelExchangeException("No consumers available on endpoint: " + getRouteboxEndpoint(), exchange);
    } else {
        LOG.debug("Dispatching to Inner Route {}", exchange);
        RouteboxDispatcher dispatcher = new RouteboxDispatcher(producer);
        result = dispatcher.dispatchSync(getRouteboxEndpoint(), exchange);
    }
    if (getRouteboxEndpoint().getConfig().isSendToConsumer()) {
        ((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer().getProcessor().process(result);
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) RouteboxDispatcher(org.apache.camel.component.routebox.strategy.RouteboxDispatcher)

Example 25 with CamelExchangeException

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

the class SpringBatchProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    JobParameters jobParameters = prepareJobParameters(exchange.getIn().getHeaders());
    String messageJobName = jobParameters.getString(SpringBatchConstants.JOB_NAME);
    Job job2run = this.job;
    if (messageJobName != null) {
        if (jobRegistry != null) {
            job2run = jobRegistry.getJob(messageJobName);
        } else {
            job2run = CamelContextHelper.mandatoryLookup(getEndpoint().getCamelContext(), messageJobName, Job.class);
        }
    }
    if (job2run == null) {
        exchange.setException(new CamelExchangeException("jobName was not specified in the endpoint construction " + " and header " + SpringBatchConstants.JOB_NAME + " could not be found", exchange));
        return;
    }
    JobExecution jobExecution = jobLauncher.run(job2run, jobParameters);
    exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
    exchange.getOut().setBody(jobExecution);
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) CamelExchangeException(org.apache.camel.CamelExchangeException) JobParameters(org.springframework.batch.core.JobParameters) Job(org.springframework.batch.core.Job)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3