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 */
017package org.apache.camel;
018
019import java.util.Queue;
020
021/**
022 * A consumer of a batch of message exchanges from an {@link Endpoint}
023 */
024public interface BatchConsumer extends Consumer {
025
026    /**
027     * Sets a maximum number of messages as a limit to poll at each polling.
028     * <p/>
029     * Can be used to limit e.g. to 100 to avoid reading thousands or more 
030     * messages within the first polling at startup.
031     * <p/>
032     * Is default unlimited, but use 0 or negative number to disable it as unlimited.
033     *
034     * @param maxMessagesPerPoll  maximum messages to poll.
035     */
036    void setMaxMessagesPerPoll(int maxMessagesPerPoll);
037
038    /**
039     * Processes the list of {@link org.apache.camel.Exchange} objects in a batch. 
040     * <p/>
041     * Each message exchange will be processed individually but the batch
042     * consumer will add properties with the current index and total in the batch.
043     * The items in the Queue may actually be Holder objects that store other 
044     * data alongside the Exchange.
045     *
046     * @param exchanges list of items in this batch
047     * @return number of messages actually processed
048     * @throws Exception if an internal processing error has occurred.
049     */
050    int processBatch(Queue<Object> exchanges) throws Exception;
051
052    /**
053     * Whether processing the batch is still allowed.
054     * <p/>
055     * This is used during shutdown to indicate whether to complete the pending
056     * exchanges or stop after the current exchange has been processed.
057     *
058     * @return <tt>true</tt> to continue processing from the batch, or <tt>false</tt> to stop.
059     * @see org.apache.camel.ShutdownRunningTask
060     */
061    boolean isBatchAllowed();
062}