001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.spi;
018
019 import org.apache.camel.Consumer;
020 import org.apache.camel.Endpoint;
021
022 /**
023 * Strategy for a {@link org.apache.camel.PollingConsumer} when polling an {@link org.apache.camel.Endpoint}.
024 * <p/>
025 * This pluggable strategy allows to plugin different implementations what to do, most noticeable what to
026 * do in case the polling goes wrong. This can be handled in the
027 * {@link #rollback(org.apache.camel.Consumer , org.apache.camel.Endpoint , int, Exception) rollback} method.
028 *
029 * @version
030 */
031 public interface PollingConsumerPollStrategy {
032
033 /**
034 * Called when poll is about to begin
035 *
036 * @param consumer the consumer
037 * @param endpoint the endpoint being consumed
038 * @return <tt>true</tt> to begin polling, or <tt>false</tt> to skip polling this time.
039 */
040 boolean begin(Consumer consumer, Endpoint endpoint);
041
042 /**
043 * Called when poll is completed successfully
044 *
045 * @param consumer the consumer
046 * @param endpoint the endpoint being consumed
047 * @param polledMessages number of messages polled, will be <tt>0</tt> if no message was polled at all.
048 */
049 void commit(Consumer consumer, Endpoint endpoint, int polledMessages);
050
051 /**
052 * Called when poll failed
053 *
054 * @param consumer the consumer
055 * @param endpoint the endpoint being consumed
056 * @param retryCounter current retry attempt, starting from 0.
057 * @param cause the caused exception
058 * @throws Exception can be used to rethrow the caused exception. Notice that thrown an exception will
059 * terminate the scheduler and thus Camel will not trigger again. So if you want to let the scheduler
060 * to continue to run then do <b>not</b> throw any exception from this method.
061 * @return whether to retry immediately or not. Return <tt>false</tt> to ignore the problem,
062 * <tt>true</tt> to try immediately again
063 */
064 boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception;
065
066 }