Search in sources :

Example 6 with PollingConsumerPollStrategy

use of org.apache.camel.spi.PollingConsumerPollStrategy in project camel by apache.

the class ScheduledPollConsumerTest method testExceptionOnPollAndCanStartAgain.

public void testExceptionOnPollAndCanStartAgain() throws Exception {
    final Exception expectedException = new Exception("Hello, I should be thrown on shutdown only!");
    final Endpoint endpoint = getMockEndpoint("mock:foo");
    MockScheduledPollConsumer consumer = new MockScheduledPollConsumer(endpoint, expectedException);
    consumer.setPollStrategy(new PollingConsumerPollStrategy() {

        public boolean begin(Consumer consumer, Endpoint endpoint) {
            return true;
        }

        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
        }

        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {
            if (e == expectedException) {
                rollback = true;
            }
            return false;
        }
    });
    consumer.start();
    // poll that throws an exception
    consumer.run();
    consumer.stop();
    assertEquals("Should have rollback", true, rollback);
    // prepare for 2nd run but this time it should not thrown an exception on poll
    rollback = false;
    consumer.setExceptionToThrowOnPoll(null);
    // start it again and we should be able to run
    consumer.start();
    consumer.run();
    // should be able to stop with no problem
    consumer.stop();
    assertEquals("Should not have rollback", false, rollback);
}
Also used : Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) PollingConsumerPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy) Endpoint(org.apache.camel.Endpoint)

Aggregations

Consumer (org.apache.camel.Consumer)6 Endpoint (org.apache.camel.Endpoint)6 PollingConsumerPollStrategy (org.apache.camel.spi.PollingConsumerPollStrategy)6