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.Processor;
026 import org.apache.camel.Service;
027 import org.apache.camel.component.mock.MockEndpoint;
028 import org.apache.camel.processor.ThroughputLogger;
029 import org.apache.camel.util.ExchangeHelper;
030 import org.apache.camel.util.ObjectHelper;
031 import org.apache.commons.logging.Log;
032 import org.apache.commons.logging.LogFactory;
033
034 /**
035 * Endpoint for DataSet.
036 *
037 * @version $Revision: 817190 $
038 */
039 public class DataSetEndpoint extends MockEndpoint implements Service {
040 private final transient Log log;
041 private DataSet dataSet;
042 private AtomicInteger receivedCounter = new AtomicInteger();
043 private int minRate;
044 private long produceDelay = 3;
045 private long consumeDelay;
046 private long preloadSize;
047 private long initialDelay = 1000;
048 private Processor reporter;
049
050 public DataSetEndpoint() {
051 this.log = LogFactory.getLog(DataSetEndpoint.class);
052 }
053
054 public DataSetEndpoint(String endpointUri, Component component, DataSet dataSet) {
055 super(endpointUri, component);
056 this.dataSet = dataSet;
057 this.log = LogFactory.getLog(endpointUri);
058 }
059
060 public DataSetEndpoint(String endpointUri, DataSet dataSet) {
061 super(endpointUri);
062 this.dataSet = dataSet;
063 this.log = LogFactory.getLog(endpointUri);
064 }
065
066 public static void assertEquals(String description, Object expected, Object actual, Exchange exchange) {
067 if (!ObjectHelper.equal(expected, actual)) {
068 throw new AssertionError(description + " does not match. Expected: " + expected + " but was: " + actual + " on " + exchange + " with headers: " + exchange.getIn().getHeaders());
069 }
070 }
071
072 @Override
073 public Consumer createConsumer(Processor processor) throws Exception {
074 return new DataSetConsumer(this, processor);
075 }
076
077 @Override
078 public void reset() {
079 super.reset();
080 receivedCounter.set(0);
081 }
082
083 @Override
084 public int getReceivedCounter() {
085 return receivedCounter.get();
086 }
087
088 /**
089 * Creates a message exchange for the given index in the {@link DataSet}
090 */
091 public Exchange createExchange(long messageIndex) throws Exception {
092 Exchange exchange = createExchange();
093 getDataSet().populateMessage(exchange, messageIndex);
094
095 Message in = exchange.getIn();
096 in.setHeader(Exchange.DATASET_INDEX, messageIndex);
097
098 return exchange;
099 }
100
101 public int getMinRate() {
102 return minRate;
103 }
104
105 public void setMinRate(int minRate) {
106 this.minRate = minRate;
107 }
108
109 @Override
110 protected void waitForCompleteLatch(long timeout) throws InterruptedException {
111 super.waitForCompleteLatch(timeout);
112
113 if (minRate > 0) {
114 int count = getReceivedCounter();
115 do {
116 // wait as long as we get a decent message rate
117 super.waitForCompleteLatch(1000L);
118 count = getReceivedCounter() - count;
119 } while (count >= minRate);
120 }
121 }
122
123 // Properties
124 //-------------------------------------------------------------------------
125
126 public DataSet getDataSet() {
127 return dataSet;
128 }
129
130 public void setDataSet(DataSet dataSet) {
131 this.dataSet = dataSet;
132 }
133
134 public long getPreloadSize() {
135 return preloadSize;
136 }
137
138 /**
139 * Sets how many messages should be preloaded (sent) before the route completes its initialization
140 */
141 public void setPreloadSize(long preloadSize) {
142 this.preloadSize = preloadSize;
143 }
144
145 public long getConsumeDelay() {
146 return consumeDelay;
147 }
148
149 /**
150 * Allows a delay to be specified which causes consumers to pause - to simulate slow consumers
151 */
152 public void setConsumeDelay(long consumeDelay) {
153 this.consumeDelay = consumeDelay;
154 }
155
156 public long getProduceDelay() {
157 return produceDelay;
158 }
159
160 /**
161 * Allows a delay to be specified which causes producers to pause - to simulate slow producers
162 */
163 public void setProduceDelay(long produceDelay) {
164 this.produceDelay = produceDelay;
165 }
166
167 /**
168 * Sets a custom progress reporter
169 */
170 public void setReporter(Processor reporter) {
171 this.reporter = reporter;
172 }
173
174 public long getInitialDelay() {
175 return initialDelay;
176 }
177
178 public void setInitialDelay(long initialDelay) {
179 this.initialDelay = initialDelay;
180 }
181
182 // Implementation methods
183 //-------------------------------------------------------------------------
184
185 @Override
186 protected void performAssertions(Exchange actual) throws Exception {
187 int receivedCount = receivedCounter.incrementAndGet();
188 long index = receivedCount - 1;
189 Exchange expected = createExchange(index);
190
191 // now lets assert that they are the same
192 if (log.isDebugEnabled()) {
193 Integer dsi = actual.getIn().getHeader(Exchange.DATASET_INDEX, Integer.class);
194 log.debug("Received message: " + index + " (DataSet index=" + dsi + ") = " + actual);
195 }
196
197 assertMessageExpected(index, expected, actual);
198
199 if (reporter != null) {
200 reporter.process(actual);
201 }
202
203 if (consumeDelay > 0) {
204 Thread.sleep(consumeDelay);
205 }
206 }
207
208 protected void assertMessageExpected(long index, Exchange expected, Exchange actual) throws Exception {
209 long actualCounter = ExchangeHelper.getMandatoryHeader(actual, Exchange.DATASET_INDEX, Long.class);
210 assertEquals("Header: " + Exchange.DATASET_INDEX, index, actualCounter, actual);
211
212 getDataSet().assertMessageExpected(this, expected, actual, index);
213 }
214
215 protected ThroughputLogger createReporter() {
216 ThroughputLogger answer = new ThroughputLogger(this.getEndpointUri(), (int) this.getDataSet().getReportCount());
217 answer.setAction("Received");
218 return answer;
219 }
220
221 public void start() throws Exception {
222 long size = getDataSet().getSize();
223 expectedMessageCount((int) size);
224 if (reporter == null) {
225 reporter = createReporter();
226 }
227 log.info("Start: " + this + " expecting " + size + " messages");
228 }
229
230 public void stop() throws Exception {
231 log.info("Stop: " + this);
232 }
233
234 }