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 * DataSet consumer. 028 * 029 * @version $Revision: 758 $ 030 */ 031 public class DataSetConsumer extends DefaultConsumer<Exchange> { 032 private static final transient Log LOG = LogFactory.getLog(DataSetConsumer.class); 033 private DataSetEndpoint endpoint; 034 private Processor reporter; 035 036 public DataSetConsumer(DataSetEndpoint endpoint, Processor processor) { 037 super(endpoint, processor); 038 this.endpoint = endpoint; 039 } 040 041 @Override 042 protected void doStart() throws Exception { 043 super.doStart(); 044 045 if (reporter == null) { 046 reporter = createReporter(); 047 } 048 final DataSet dataSet = endpoint.getDataSet(); 049 final long preloadSize = endpoint.getPreloadSize(); 050 051 sendMessages(0, preloadSize); 052 053 endpoint.getExecutorService().execute(new Runnable() { 054 public void run() { 055 sendMessages(preloadSize, dataSet.getSize()); 056 } 057 }); 058 } 059 060 protected void sendMessages(long startIndex, long endIndex) { 061 try { 062 for (long i = startIndex; i < endIndex; i++) { 063 Exchange exchange = endpoint.createExchange(i); 064 getProcessor().process(exchange); 065 066 try { 067 long delay = endpoint.getProduceDelay(); 068 if (delay < 3) { 069 // if no delay set then we must sleep at lest for 3 millis to avoid concurrency 070 // issues with extremely high throughput 071 delay = 3; 072 } 073 Thread.sleep(delay); 074 } catch (InterruptedException e) { 075 // ignore and just log to debug 076 LOG.debug(e); 077 } 078 if (reporter != null) { 079 reporter.process(exchange); 080 } 081 } 082 } catch (Exception e) { 083 LOG.error(e); 084 } 085 } 086 087 protected ThroughputLogger createReporter() { 088 ThroughputLogger answer = new ThroughputLogger(endpoint.getEndpointUri(), (int) endpoint.getDataSet().getReportCount()); 089 answer.setAction("Sent"); 090 return answer; 091 } 092 }