Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Initializing Instance Fields

There are three ways to initialize instance fields used in flowlets:

  1. Using the default constructor;

  2. Using the initialize() method of the flowlets; and

  3. Using @Property annotations.

To initialize using a property annotation, simply annotate the field definition with @Property.

The following example demonstrates the convenience of using @Property in a WordFilter flowlet that filters out specific words:

Code Block
public static class WordFilter extends AbstractFlowlet {

  private OutputEmitter<String> out;

  @Property
  private final String toFilterOut;

  public WordFilter(String toFilterOut) {
    this.toFilterOut = toFilterOut;
  }

  @ProcessInput()
  public void process(String word) {
    if (!toFilterOut.equals(word)) {
      out.emit(word);
    }
  }
}

The flowlet constructor is called with the parameter when the flow is configured:

Code Block
public static class WordCountFlow extends AbstractFlow {

  @Override
  public void configure() {
    setName("WordCountFlow");
    setDescription("Flow for counting words");
    addFlowlet("Tokenizer", new Tokenizer());
    addFlowlet("WordsFilter", new WordsFilter("the"));
    addFlowlet("WordsCounter", new WordsCounter());
    connectStream("text", "Tokenizer");
    connect("Tokenizer", "WordsFilter");
    connect("WordsFilter", "WordsCounter");
  }
}

At run-time, when the flowlet is started, a value is injected into the toFilterOut field.

Note: Field types that are supported using the @Property annotation are primitives, boxed types (such as an Integer), String, and enum.

Scanning Over Tables with Byte Array Keys

...