Search in sources :

Example 61 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class ScheduledPollConsumerGreedyTest method test321NotGreedy.

public void test321NotGreedy() throws Exception {
    polled.set(0);
    MockScheduledPollConsumer consumer = new Mock321ScheduledPollConsumer(getMockEndpoint("mock:foo"), null);
    consumer.setGreedy(false);
    consumer.setPollStrategy(new PollingConsumerPollStrategy() {

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

        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
            polled.addAndGet(polledMessages);
        }

        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {
            return false;
        }
    });
    consumer.start();
    consumer.run();
    assertEquals(3, polled.get());
    consumer.run();
    assertEquals(5, polled.get());
    consumer.run();
    assertEquals(6, polled.get());
    consumer.run();
    assertEquals(6, polled.get());
    consumer.stop();
}
Also used : Consumer(org.apache.camel.Consumer) Endpoint(org.apache.camel.Endpoint) PollingConsumerPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy) Endpoint(org.apache.camel.Endpoint)

Example 62 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class ScheduledPollConsumerGreedyTest method test321Greedy.

public void test321Greedy() throws Exception {
    polled.set(0);
    MockScheduledPollConsumer consumer = new Mock321ScheduledPollConsumer(getMockEndpoint("mock:foo"), null);
    consumer.setGreedy(true);
    consumer.setPollStrategy(new PollingConsumerPollStrategy() {

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

        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
            polled.addAndGet(polledMessages);
        }

        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {
            return false;
        }
    });
    consumer.start();
    consumer.run();
    assertEquals(6, polled.get());
    consumer.stop();
}
Also used : Consumer(org.apache.camel.Consumer) Endpoint(org.apache.camel.Endpoint) PollingConsumerPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy) Endpoint(org.apache.camel.Endpoint)

Example 63 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class ScheduledPollConsumerTest method testRetryAtMostThreeTimes.

public void testRetryAtMostThreeTimes() throws Exception {
    counter = 0;
    event = "";
    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) {
            event += "commit";
        }

        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {
            event += "rollback";
            counter++;
            if (retryCounter < 3) {
                return true;
            }
            return false;
        }
    });
    consumer.setUseFixedDelay(true);
    consumer.setDelay(60000);
    consumer.start();
    // poll that throws an exception
    consumer.run();
    consumer.stop();
    // 3 retries + 1 last failed attempt when we give up
    assertEquals(4, counter);
    assertEquals("rollbackrollbackrollbackrollback", event);
}
Also used : Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) PollingConsumerPollStrategy(org.apache.camel.spi.PollingConsumerPollStrategy) Endpoint(org.apache.camel.Endpoint)

Example 64 with Consumer

use of org.apache.camel.Consumer 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)

Example 65 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class SWFEndpoint method createConsumer.

public Consumer createConsumer(Processor processor) throws Exception {
    Consumer consumer = isWorkflow() ? new SWFWorkflowConsumer(this, processor, configuration) : new SWFActivityConsumer(this, processor, configuration);
    configureConsumer(consumer);
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer)

Aggregations

Consumer (org.apache.camel.Consumer)76 Endpoint (org.apache.camel.Endpoint)27 Processor (org.apache.camel.Processor)19 Exchange (org.apache.camel.Exchange)18 Test (org.junit.Test)18 HashMap (java.util.HashMap)10 RestConfiguration (org.apache.camel.spi.RestConfiguration)10 Producer (org.apache.camel.Producer)7 PollingConsumer (org.apache.camel.PollingConsumer)6 File (java.io.File)5 PollingConsumerPollStrategy (org.apache.camel.spi.PollingConsumerPollStrategy)5 FileNotFoundException (java.io.FileNotFoundException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SuspendableService (org.apache.camel.SuspendableService)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CamelContext (org.apache.camel.CamelContext)3 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 Route (org.apache.camel.Route)3