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: 817190 $
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                    if (endpoint.getInitialDelay() > 0) {
053                        try {
054                            Thread.sleep(endpoint.getInitialDelay());
055                        } catch (InterruptedException e) {
056                            Thread.currentThread().interrupt();
057                            return;
058                        }
059                    }
060    
061                    sendMessages(preloadSize, dataSet.getSize());
062                }
063            });
064        }
065    
066        protected void sendMessages(long startIndex, long endIndex) {
067            try {
068                for (long i = startIndex; i < endIndex; i++) {
069                    Exchange exchange = endpoint.createExchange(i);
070                    getProcessor().process(exchange);
071    
072                    try {
073                        long delay = endpoint.getProduceDelay();
074                        if (delay > 0) {
075                            Thread.sleep(delay);
076                        }
077                    } catch (InterruptedException e) {
078                        Thread.currentThread().interrupt();
079                        break;
080                    }
081                    if (reporter != null) {
082                        reporter.process(exchange);
083                    }
084                }
085            } catch (Exception e) {
086                handleException(e);
087            }
088        }
089    
090        protected ThroughputLogger createReporter() {
091            ThroughputLogger answer = new ThroughputLogger(endpoint.getEndpointUri(), (int) endpoint.getDataSet().getReportCount());
092            answer.setAction("Sent");
093            return answer;
094        }
095    }