|
|
## Write you own producer
|
|
|
|
|
|
The following code shows a simple producer stage. It takes a list of elements are sends them to the ongoing stage.
|
|
|
The following code shows a simple producer stage. It takes a list of elements and sends them one by one to its output port.
|
|
|
|
|
|
```java
|
|
|
public final class InitialElementProducer<T> extends AbstractProducerStage<T> {
|
... | ... | @@ -24,7 +24,7 @@ public final class InitialElementProducer<T> extends AbstractProducerStage<T> { |
|
|
for (final T element : this.elements) {
|
|
|
this.outputPort.send(element);
|
|
|
}
|
|
|
this.terminate();
|
|
|
this.terminateStage();
|
|
|
}
|
|
|
|
|
|
}
|
... | ... | @@ -33,20 +33,16 @@ public final class InitialElementProducer<T> extends AbstractProducerStage<T> { |
|
|
First, we implement ``execute``.
|
|
|
It simply iterates over the given list and sends all elements by calling ``this.outputPort.send(element)``.
|
|
|
|
|
|
Furthermore, we want to check if the given iterable is not null.
|
|
|
Furthermore, we want to check if the given iterable is not ``null``.
|
|
|
This can be achieved by running a check while starting the stage.
|
|
|
For this, we override the method ``onStarting`` and add a null check to it.
|
|
|
This method will be called once by the framework before it executes the stage for the first time.
|
|
|
**Note:** You also need to make sure the super method is also called.
|
|
|
|
|
|
Finally, we want to terminate the stage.
|
|
|
Calling the method ``terminate`` will do so and additionally send a signal to all ongoing stages to terminate.
|
|
|
But do we need to do, if there is no kind of "termination point"?
|
|
|
In some cases, you want to execute something in an endless loop manner.
|
|
|
Take a look at the [Clock stage](https://build.se.informatik.uni-kiel.de/gitlab/teetime/teetime/blob/master/src/main/java/teetime/stage/Clock.java).
|
|
|
It endlessly generates elements and has not point where it terminates.
|
|
|
To indicate TeeTime this behavior, we need to modify the termination strategy of the stage.
|
|
|
By default, it terminates itself, like the InitialElementProducer of the example above.
|
|
|
In such cases like the Clock, we need to terminate it by interrupt.
|
|
|
To tell TeeTime this, simply override ```getTerminationStrategy()``` and return the Enum ```TerminationStrategy.BY_INTERRUPT```.
|
|
|
If the configuration terminates, this stage will be interrupted by the framework. |
|
|
**Note:** You also need to make sure that the super method is called, too.
|
|
|
|
|
|
Finally, we need to tell the framework that this stage has finished its work.
|
|
|
Calling the method ``terminateStage`` will do so.
|
|
|
Moreover, it automatically propagates a termination signal through the whole P&F architecture.
|
|
|
In this way, all stages terminate themselves after processing their inputs.
|
|
|
Thus, no external termination action is required by the user.
|
|
|
|
|
|
Have a look at the [Clock stage](https://build.se.informatik.uni-kiel.de/gitlab/teetime/teetime/blob/master/src/main/java/teetime/stage/Clock.java) which outputs the current timestamp as long as a certain condition holds. |