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 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 026 /** 027 * @version $Revision: 37863 $ 028 */ 029 public class DataSetConsumer extends DefaultConsumer<Exchange> { 030 private static final transient Log LOG = LogFactory.getLog(DataSetConsumer.class); 031 private DataSetEndpoint endpoint; 032 private Processor reporter; 033 034 public DataSetConsumer(DataSetEndpoint endpoint, Processor processor) { 035 super(endpoint, processor); 036 this.endpoint = endpoint; 037 } 038 039 @Override 040 protected void doStart() throws Exception { 041 super.doStart(); 042 043 if (reporter == null) { 044 reporter = createReporter(); 045 } 046 final DataSet dataSet = endpoint.getDataSet(); 047 final long preloadSize = endpoint.getPreloadSize(); 048 049 sendMessages(0, preloadSize); 050 051 endpoint.getExecutorService().execute(new Runnable() { 052 public void run() { 053 sendMessages(preloadSize, dataSet.getSize()); 054 } 055 }); 056 } 057 058 protected void sendMessages(long startIndex, long endIndex) { 059 try { 060 for (long i = startIndex; i < endIndex; i++) { 061 Exchange exchange = endpoint.createExchange(i); 062 getProcessor().process(exchange); 063 064 long delay = endpoint.getProduceDelay(); 065 if (delay > 0) { 066 try { 067 Thread.sleep(delay); 068 } catch (InterruptedException e) { 069 LOG.debug(e); 070 } 071 } 072 if (reporter != null) { 073 reporter.process(exchange); 074 } 075 } 076 } catch (Exception e) { 077 LOG.error(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 }