use of org.apache.camel.Processor in project camel by apache.
the class FromJmsToJdbcIdempotentConsumerToJmsTest method testJmsToJdbcJmsRollbackAtA.
@Ignore("see the TODO below")
@Test
public void testJmsToJdbcJmsRollbackAtA() throws Exception {
checkInitialState();
// use a notify to know that after 1+6 (1 original + 6 redelivery) attempts from AcitveMQ
NotifyBuilder notify = new NotifyBuilder(context).whenDone(7).create();
// TODO: occasionally we get only 6 instead of 7 expected exchanges which's most probably an issue in ActiveMQ itself
getMockEndpoint("mock:a").expectedMessageCount(7);
// force exception to occur at mock a
getMockEndpoint("mock:a").whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
throw new ConnectException("Forced cannot connect to database");
}
});
getMockEndpoint("mock:b").expectedMessageCount(0);
template.sendBodyAndHeader("activemq2:queue:inbox", "A", "uid", 123);
// assert mock and wait for the message to be done
assertMockEndpointsSatisfied();
assertTrue("Should complete 7 message", notify.matchesMockWaitTime());
// check that there is a message in the database and JMS queue
assertEquals(new Integer(0), jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
assertNull(consumer.receiveBody("activemq2:queue:outbox", 3000));
// the message should have been moved to the AMQ DLQ queue
assertEquals("A", consumer.receiveBody("activemq2:queue:ActiveMQ.DLQ", 3000));
}
use of org.apache.camel.Processor in project camel by apache.
the class JmsToHttpWithOnExceptionRoute method configure.
public void configure() throws Exception {
port = AvailablePortFinder.getNextAvailable(8000);
// configure a global transacted error handler
errorHandler(transactionErrorHandler(required));
// if its a 404 then regard it as handled
onException(HttpOperationFailedException.class).onWhen(new Predicate() {
public boolean matches(Exchange exchange) {
HttpOperationFailedException e = exchange.getException(HttpOperationFailedException.class);
return e != null && e.getStatusCode() == 404;
}
}).handled(true).to("mock:404").transform(constant(noAccess));
from("activemq:queue:data").policy(required).to("http://localhost:" + port + "/sender").convertBodyTo(String.class).choice().when().xpath("/reply/status != 'ok'").to("mock:rollback").rollback().otherwise().end();
// this is our http router
from("jetty:http://localhost:" + port + "/sender").process(new Processor() {
public void process(Exchange exchange) throws Exception {
// first hit is always a error code 500 to force the caller to retry
if (counter++ < 1) {
// simulate http error 500
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
exchange.getOut().setBody("Damn some internal server error");
return;
}
String user = exchange.getIn().getHeader("user", String.class);
if ("unknown".equals(user)) {
// no page for a unknown user
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
exchange.getOut().setBody("Page does not exists");
return;
} else if ("guest".equals(user)) {
// not okay for guest user
exchange.getOut().setBody(nok);
return;
}
exchange.getOut().setBody(ok);
}
});
}
use of org.apache.camel.Processor in project camel by apache.
the class JmsConsumerShutdownTest method testSedaConsumerShutdownWithMessageInFlight.
// Just for the sake of comparison test the SedaConsumer as well
@Test
@DirtiesContext
public void testSedaConsumerShutdownWithMessageInFlight() throws InterruptedException {
end.expectedMessageCount(0);
end.setResultWaitTime(2000);
// direct:dir route always fails
exception.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
throw new Exception("Kaboom!");
}
});
seda.sendBody("seda:start", "Hello");
end.assertIsSatisfied();
}
use of org.apache.camel.Processor in project camel by apache.
the class JettyJmsTwowayTest method testSendingRequest.
@Test
public void testSendingRequest() throws Exception {
assertNotNull("the camelContext should not be null", camelContext);
ProducerTemplate template = camelContext.createProducerTemplate();
Exchange exchange = template.send(URL, new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("<hello>Willem</hello>");
exchange.getIn().setHeader("Operation", "greetMe");
}
});
assertEquals("get result ", "<response><hello>Willem</hello></response>", exchange.getOut().getBody(String.class));
template.stop();
}
use of org.apache.camel.Processor in project camel by apache.
the class JmsToHttpWithRollbackRoute method configure.
public void configure() throws Exception {
port = AvailablePortFinder.getNextAvailable(8000);
// configure a global transacted error handler
errorHandler(transactionErrorHandler(required));
from(data).policy(required).to("http://localhost:" + port + "/sender").convertBodyTo(String.class).choice().when().xpath("/reply/status != 'ok'").to("mock:rollback").rollback().otherwise().end();
// this is our http route that will fail the first 2 attempts
// before it sends an ok response
from("jetty:http://localhost:" + port + "/sender").process(new Processor() {
public void process(Exchange exchange) throws Exception {
if (counter++ < 2) {
exchange.getOut().setBody(nok);
} else {
exchange.getOut().setBody(ok);
}
}
});
}
Aggregations