Search in sources :

Example 61 with Processor

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));
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) Exchange(org.apache.camel.Exchange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Processor(org.apache.camel.Processor) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 62 with Processor

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);
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Predicate(org.apache.camel.Predicate)

Example 63 with Processor

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();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 64 with Processor

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();
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) Test(org.junit.Test)

Example 65 with Processor

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);
            }
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor)

Aggregations

Processor (org.apache.camel.Processor)1469 Exchange (org.apache.camel.Exchange)1369 Test (org.junit.Test)634 RouteBuilder (org.apache.camel.builder.RouteBuilder)544 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)164 Message (org.apache.camel.Message)119 ArrayList (java.util.ArrayList)66 HashMap (java.util.HashMap)64 IOException (java.io.IOException)55 CamelExecutionException (org.apache.camel.CamelExecutionException)52 Endpoint (org.apache.camel.Endpoint)46 Map (java.util.Map)45 File (java.io.File)38 List (java.util.List)35 Producer (org.apache.camel.Producer)33 DefaultExchange (org.apache.camel.impl.DefaultExchange)29 SendProcessor (org.apache.camel.processor.SendProcessor)26 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)26 CountDownLatch (java.util.concurrent.CountDownLatch)24 Expression (org.apache.camel.Expression)24