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: 748469 $ 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 && delay < 3) { 066 // if no delay set then we must sleep at lest for 3 millis to avoid concurrency 067 // issues with extremely high throughput 068 delay = 3; 069 } 070 // to allow -1 to force none delay at all 071 if (delay > 0) { 072 Thread.sleep(delay); 073 } 074 } catch (InterruptedException e) { 075 Thread.currentThread().interrupt(); 076 break; 077 } 078 if (reporter != null) { 079 reporter.process(exchange); 080 } 081 } 082 } catch (Exception e) { 083 handleException(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 }