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