blob: 58fbe6875497ca210c6f9174f4bce14967e2bd36 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.training.exercises.common.sources;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.training.exercises.common.datatypes.TaxiFare;
import org.apache.flink.training.exercises.common.utils.DataGenerator;
import java.time.Duration;
import java.time.Instant;
/**
* This SourceFunction generates a data stream of TaxiFare records.
*
* <p>The stream is generated in order.
*/
public class TaxiFareGenerator implements SourceFunction<TaxiFare> {
private volatile boolean running = true;
private Instant limitingTimestamp = Instant.MAX;
/** Create a bounded TaxiFareGenerator that runs only for the specified duration. */
public static TaxiFareGenerator runFor(Duration duration) {
TaxiFareGenerator generator = new TaxiFareGenerator();
generator.limitingTimestamp = DataGenerator.BEGINNING.plus(duration);
return generator;
}
@Override
public void run(SourceContext<TaxiFare> ctx) throws Exception {
long id = 1;
while (running) {
TaxiFare fare = new TaxiFare(id);
// don't emit events that exceed the specified limit
if (fare.startTime.compareTo(limitingTimestamp) >= 0) {
break;
}
++id;
ctx.collect(fare);
// match our event production rate to that of the TaxiRideGenerator
Thread.sleep(TaxiRideGenerator.SLEEP_MILLIS_PER_EVENT);
}
}
@Override
public void cancel() {
running = false;
}
}