Connecting the Dots

Last updated 6 months ago

On this page, we will learn how to connect all the pieces we wrote before in this tutorial.

Going back to the Beginning

Let's open up our original HelloWorldPronghorn.java file that contains our main(). If you remember, it looked something like this:

package com.ociweb;
import com.ociweb.pronghorn.pipe.Pipe;
import com.ociweb.pronghorn.stage.scheduling.GraphManager;
import com.ociweb.pronghorn.stage.scheduling.StageScheduler;
public class HelloWorldPronghorn {
public static void main(String[] args) {
//todo: code here
}
}

First, let's create two new methods named populateGraph and start:

private static void populateGraph(GraphManager gm) {
}
private static void start(GraphManager gm) {
}
  • populateGraph takes a GraphManager and will be responsible for adding stages and pipes.

  • start also takes a GraphManager and will be responsible for starting the scheduler and enabling telemetry if needed.

Although you could technically write all of this in the body of your main, it is a good idea to start separating your graph creation (adding pipes and stages) and your graph operations (starting the scheduler, shut down, telemetry) logic.

This will let you keep things organized once you start expanding your codebase.

Let's take a closer look at our populateGraph method.

We want to create a general-purpose pipe that contains the name of our person being greeted. We also want to create new instances of our GreeterStage that takes the general-purpose pipe as an output and a GuestStage that will take it as an input:

private static void populateGraph(GraphManager gm) {
Pipe<HelloWorldSchema> messagePipe = HelloWorldSchema.instance.newPipe(10, 10_000);
new GreeterStage(gm, "Jon Snow", messagePipe);
new GuestStage(gm, messagePipe);
}
  • On line 2, we initialize our HelloWorldSchema pipe using the generated newPipe method. It takes the minimumFragments on the pipe (which we set to 10), and lets us also set the maximum length of a variable on the pipe. In your own project, you will need to fine-tune these numbers depending on the known size of fragments, expected usage, and average volume.

  • On line 3, we create our GreeterStage. Note that it isn't being passed into anything. This is because most of Pronghorn is statically typed. We pass in our messagePipe that will be written to.

    • If this syntax is strange to you, you can create a static method on GreeterStage named newInstance that simply creates a new object. This is Pronghorn convention but not required.

  • On line 4, the same concept is repeated, but with the GuestStage. This stage takes the message and will print it to the console. Ideally, we could let our users pass in an input stream (such as a PrintWriter or System.out) to determine where the name gets written to, but this isn't required.

This sums up the populateGraph method. Our graph should now contain two stages, connected by one pipe. However, this is useless if we don't start our scheduler:

A scheduler in Pronghorn is responsible for determining when and where stages should be executed. It determines what threads stages should be put on depending on system settings, and allots more resources to specific stages. It also provides warning messages when a stage is taking up too many resources.

By default, always use the defaultScheduler which is optimized for multi-core performance.

private static void start(GraphManager gm) {
StageScheduler.defaultScheduler(gm).startup();
}

The one thing remaining is creating a GraphManager and calling our new methods:

public static void main(String[] args) {
GraphManager gm = new GraphManager();
populateGraph(gm);
start(gm);
}

...and we're done!

Run our new Program

Running our new HelloWorld app yields the following output:

[main] INFO com.ociweb.pronghorn.stage.scheduling.StageScheduler - Targeted threads in use 8, fixed limit with fixed script. NOTE: More threads may be used use to graph complexity and telemetry usage.
[main] INFO com.ociweb.pronghorn.stage.scheduling.ScriptedFixedThreadsScheduler - actual thread count 1
Hello World, Jon Snow!

Not very exciting, is it? However, now you know how to start writing very performant Pronghorn apps. Check the next page for some information about telemetry that Pronghorn provides.