The Stage

On this page, we will create a stage (not a producing stage) that is capable of reading from a HelloWorldSchema pipe and do something with that information.

Creating the Stage

Let's assume our person being greeted is called a guest. After all, they're being warmly welcomed into the Pronghorn ecosystem!

Create a new file named GuestStage.java under src/main/java/com.ociweb with the following contents:

package com.ociweb;

import com.ociweb.pronghorn.pipe.Pipe;
import com.ociweb.pronghorn.pipe.PipeReader;
import com.ociweb.pronghorn.stage.PronghornStage;
import com.ociweb.pronghorn.stage.scheduling.GraphManager;

public class GuestStage extends PronghornStage {

    private Pipe<HelloWorldSchema> input;

    public GuestStage(GraphManager graphManager, Pipe<HelloWorldSchema> input) {
        super(graphManager, input, NONE);

        this.input = input;
    }

    @Override
    public void startup() {

    }

    @Override
    public void run() {

    }

    @Override
    public void shutdown() {

    }
}

As you will notice, this looks very similar to our producer. However, there are a few differences:

  • Instead of having an output pipe, we obviously have an input pipe, because we are reading, not writing. This is reflected in our constructor.

  • Instead of calling super(graphManager, NONE, output), we call super(graphManager, input, NONE), letting Pronghorn know that we are a consumer of our pipe and do not produce any output.

Let's see how to read from a pipe:

@Override
public void run() {
    if(PipeReader.tryReadFragment(input)) {
        StringBuilder name = new StringBuilder();
        PipeReader.readUTF8(input, HelloWorldSchema.MSG_HELLOWORLDMESSAGE_1_FIELD_GREETINGNAME_100, name);

        System.out.printf("Hello World, %s!", name);
        
        requestShutdown();
    }
}

What's actually going on here?

  • Once again, this API looks very similar to the writing API. It should be noted that there is a second way to read and write from the API, which we call the low-level API. However, it is not required for such a simple example, and the high-level API will do.

  • On line 3, we check if we can read from the input pipe.

  • On line 4-5, we create a StringBuilder that will contain the read name. Then, we use the PipeReader to read the UTF-8 encoded field from the pipe and append it to the StringBuilder.

  • On line 9, we shut down the stage since there is not much more to do.

That wasn't to bad, was it?

On the next page, we will learn how to finally connect all these pieces and run our very simple Pronghorn app.

Last updated