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