T - The type of the elements produced by this source.public interface SourceFunction<T> extends org.apache.flink.api.common.functions.Function, Serializable
run(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T>) method
is called with a Collector that can be used for emitting elements.
The run method can run for as long as necessary. The source must, however, react to an
invocation of cancel() by breaking out of its main loop.
Note about checkpointed sources
Sources that also implement the Checkpointed
interface must ensure that state checkpointing, updating of internal state and emission of
elements are not done concurrently. This is achieved by using the provided checkpointing lock
object to protect update of state and emission of elements in a synchronized block.
This is the basic pattern one should follow when implementing a (checkpointed) source:
{@code
public class ExampleSource implements SourceFunction, Checkpointed {
private long count = 0L;
private volatile boolean isRunning; | Modifier and Type | Interface and Description |
|---|---|
static interface |
SourceFunction.SourceContext<T>
Interface that source functions use to communicate with the outside world.
|
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Cancels the source.
|
void |
run(SourceFunction.SourceContext<T> ctx)
Starts the source.
|
void run(SourceFunction.SourceContext<T> ctx) throws Exception
Collector parameter to emit
elements. Sources that implement
Checkpointed must lock on the
checkpoint lock (using a synchronized block) before updating internal state and/or emitting
elements. Also, the update of state and emission of elements must happen in the same
synchronized block.ctx - The context for interaction with the outside world.Exceptionvoid cancel()
run(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T>) method. You need to ensure that the source will break out of this loop. This
can be achieved by having a volatile field "isRunning" that is checked in the loop and that
is set to false in this method.Copyright © 2014–2015 The Apache Software Foundation. All rights reserved.