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.component.dataset;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.impl.DefaultConsumer;
022    import org.apache.camel.processor.ThroughputLogger;
023    
024    /**
025     * DataSet consumer.
026     *
027     * @version $Revision: 766288 $
028     */
029    public class DataSetConsumer extends DefaultConsumer {
030        private DataSetEndpoint endpoint;
031        private Processor reporter;
032    
033        public DataSetConsumer(DataSetEndpoint endpoint, Processor processor) {
034            super(endpoint, processor);
035            this.endpoint = endpoint;
036        }
037    
038        @Override
039        protected void doStart() throws Exception {
040            super.doStart();
041    
042            if (reporter == null) {
043                reporter = createReporter();
044            }
045            final DataSet dataSet = endpoint.getDataSet();
046            final long preloadSize = endpoint.getPreloadSize();
047    
048            sendMessages(0, preloadSize);
049    
050            endpoint.getExecutorService().execute(new Runnable() {
051                public void run() {
052                    sendMessages(preloadSize, dataSet.getSize());
053                }
054            });
055        }
056    
057        protected void sendMessages(long startIndex, long endIndex) {
058            try {
059                for (long i = startIndex; i < endIndex; i++) {
060                    Exchange exchange = endpoint.createExchange(i);
061                    getProcessor().process(exchange);
062    
063                    try {
064                        long delay = endpoint.getProduceDelay();
065                        if (delay > 0) {
066                            Thread.sleep(delay);
067                        }
068                    } catch (InterruptedException e) {
069                        Thread.currentThread().interrupt();
070                        break;
071                    }
072                    if (reporter != null) {
073                        reporter.process(exchange);
074                    }
075                }
076            } catch (Exception e) {
077                handleException(e);
078            }
079        }
080    
081        protected ThroughputLogger createReporter() {
082            ThroughputLogger answer = new ThroughputLogger(endpoint.getEndpointUri(), (int) endpoint.getDataSet().getReportCount());
083            answer.setAction("Sent");
084            return answer;
085        }
086    }