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.impl;
018    
019    import org.apache.camel.Consumer;
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.StatefulService;
022    import org.apache.camel.spi.PollingConsumerPollStrategy;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    /**
027     * A default implementation that just logs a <tt>WARN</tt> level log in case of rollback.
028     * <p/>
029     * The implement will <b>not</b> log if the rollback occurred during shutdown.
030     *
031     * @version 
032     */
033    public class DefaultPollingConsumerPollStrategy implements PollingConsumerPollStrategy {
034    
035        protected final transient Logger log = LoggerFactory.getLogger(getClass());
036    
037        public boolean begin(Consumer consumer, Endpoint endpoint) {
038            return true;
039        }
040    
041        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
042            // noop
043        }
044    
045        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception e) throws Exception {
046            boolean runAllowed = true;
047            if (consumer instanceof StatefulService) {
048                runAllowed = ((StatefulService) consumer).isRunAllowed();
049            }
050    
051            // only log warn if we are running, otherwise we are just stopping which we should not log the issue in the logs
052            if (runAllowed) {
053                log.warn("Consumer " + consumer +  " could not poll endpoint: " + endpoint + " caused by: " + e.getMessage(), e);
054            }
055    
056            // we do not want to retry
057            return false;
058        }
059    
060    }