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 java.util.concurrent.atomic.AtomicInteger; 020 021 import org.apache.camel.Component; 022 import org.apache.camel.Consumer; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.Message; 025 import org.apache.camel.PollingConsumer; 026 import org.apache.camel.Processor; 027 import org.apache.camel.Service; 028 import org.apache.camel.component.mock.MockEndpoint; 029 import org.apache.camel.impl.EventDrivenPollingConsumer; 030 import org.apache.camel.util.ExchangeHelper; 031 import org.apache.camel.util.ObjectHelper; 032 import org.apache.commons.logging.Log; 033 import org.apache.commons.logging.LogFactory; 034 035 /** 036 * Endpoint for DataSet. 037 * 038 * @version $Revision: 748469 $ 039 */ 040 public class DataSetEndpoint extends MockEndpoint implements Service { 041 private static final transient Log LOG = LogFactory.getLog(DataSetEndpoint.class); 042 private DataSet dataSet; 043 private AtomicInteger receivedCounter = new AtomicInteger(); 044 private long produceDelay; 045 private long consumeDelay; 046 private long startTime; 047 private long preloadSize; 048 049 public DataSetEndpoint() { 050 } 051 052 public DataSetEndpoint(String endpointUri, Component component, DataSet dataSet) { 053 super(endpointUri, component); 054 this.dataSet = dataSet; 055 } 056 057 public DataSetEndpoint(String endpointUri, DataSet dataSet) { 058 super(endpointUri); 059 this.dataSet = dataSet; 060 } 061 062 public static void assertEquals(String description, Object expected, Object actual, Exchange exchange) { 063 if (!ObjectHelper.equal(expected, actual)) { 064 throw new AssertionError(description + " does not match. Expected: " + expected + " but was: " + actual + " on " + exchange + " with headers: " + exchange.getIn().getHeaders()); 065 } 066 } 067 068 @Override 069 public PollingConsumer createPollingConsumer() throws Exception { 070 return new EventDrivenPollingConsumer(this); 071 } 072 073 @Override 074 public Consumer createConsumer(Processor processor) throws Exception { 075 return new DataSetConsumer(this, processor); 076 } 077 078 @Override 079 public void reset() { 080 super.reset(); 081 receivedCounter.set(0); 082 } 083 084 @Override 085 public int getReceivedCounter() { 086 return receivedCounter.get(); 087 } 088 089 /** 090 * Creates a message exchange for the given index in the {@link DataSet} 091 */ 092 public Exchange createExchange(long messageIndex) throws Exception { 093 Exchange exchange = createExchange(); 094 getDataSet().populateMessage(exchange, messageIndex); 095 096 Message in = exchange.getIn(); 097 in.setHeader(Exchange.DATASET_INDEX, messageIndex); 098 099 return exchange; 100 } 101 102 @Override 103 protected void waitForCompleteLatch() throws InterruptedException { 104 // TODO lets do a much better version of this! 105 long size = getDataSet().getSize(); 106 size *= 4000; 107 setResultWaitTime(size); 108 super.waitForCompleteLatch(); 109 } 110 111 // Properties 112 //------------------------------------------------------------------------- 113 114 public DataSet getDataSet() { 115 return dataSet; 116 } 117 118 public void setDataSet(DataSet dataSet) { 119 this.dataSet = dataSet; 120 } 121 122 public long getPreloadSize() { 123 return preloadSize; 124 } 125 126 /** 127 * Sets how many messages should be preloaded (sent) before the route completes its initialisation 128 */ 129 public void setPreloadSize(long preloadSize) { 130 this.preloadSize = preloadSize; 131 } 132 133 public long getConsumeDelay() { 134 return consumeDelay; 135 } 136 137 /** 138 * Allows a delay to be specified which causes consumers to pause - to simulate slow consumers 139 */ 140 public void setConsumeDelay(long consumeDelay) { 141 this.consumeDelay = consumeDelay; 142 } 143 144 public long getProduceDelay() { 145 return produceDelay; 146 } 147 148 /** 149 * Allows a delay to be specified which causes producers to pause - to simulate slow producers 150 */ 151 public void setProduceDelay(long produceDelay) { 152 this.produceDelay = produceDelay; 153 } 154 155 // Implementation methods 156 //------------------------------------------------------------------------- 157 158 @Override 159 protected void performAssertions(Exchange actual) throws Exception { 160 if (startTime == 0) { 161 startTime = System.currentTimeMillis(); 162 } 163 int receivedCount = receivedCounter.incrementAndGet(); 164 long index = receivedCount - 1; 165 Exchange expected = createExchange(index); 166 167 // now lets assert that they are the same 168 if (LOG.isDebugEnabled()) { 169 LOG.debug("Received message: " + index + " = " + actual); 170 } 171 172 assertMessageExpected(index, expected, actual); 173 174 if (consumeDelay > 0) { 175 Thread.sleep(consumeDelay); 176 } 177 178 long group = getDataSet().getReportCount(); 179 if (receivedCount % group == 0) { 180 reportProgress(actual, receivedCount); 181 } 182 } 183 184 protected void reportProgress(Exchange actual, int receivedCount) { 185 long time = System.currentTimeMillis(); 186 long elapsed = time - startTime; 187 startTime = time; 188 189 LOG.info("Received: " + receivedCount + " messages so far. Last group took: " + elapsed + " millis"); 190 } 191 192 protected void assertMessageExpected(long index, Exchange expected, Exchange actual) throws Exception { 193 long actualCounter = ExchangeHelper.getMandatoryHeader(actual, Exchange.DATASET_INDEX, Long.class); 194 assertEquals("Header: " + Exchange.DATASET_INDEX, index, actualCounter, actual); 195 196 getDataSet().assertMessageExpected(this, expected, actual, index); 197 } 198 199 public void start() throws Exception { 200 long size = getDataSet().getSize(); 201 expectedMessageCount((int) size); 202 } 203 204 public void stop() throws Exception { 205 } 206 }