Skip to content

Instantly share code, notes, and snippets.

@mjg123

mjg123/blog.md Secret

Created October 16, 2017 09:20
Show Gist options
  • Save mjg123/2cd92aa1e92730e2430cd01c64bde6f9 to your computer and use it in GitHub Desktop.
Save mjg123/2cd92aa1e92730e2430cd01c64bde6f9 to your computer and use it in GitHub Desktop.

#Flow 102

If you haven't read Flow 101 yet, I encourage you to start there and get grounded in what Flow is, what it's for and how it works.

In this post I'll go through how to build a more complex Flow with parallelism and asynchronous chaining. I'll assume you have set up the services as described in the Flow 101.

The demo Flow

An app which:

  • reads some text
  • greps for a given keyword
  • counts the matching lines
  • prints the count
  • prints the file header

In your shell, it might look something like:

https://gist.github.com/23ae93871337f319b158dc7367bdd4c4

Installing helper functions

One of the cool things about Fn is that because it's based on Docker, functions can be written in any language - even Bash!

Clone this repo of simple Bash functions and deploy them all:

https://gist.github.com/5fd64df7f3e958c12f6c07781984cd01

You can test all of these individually, for example:

https://gist.github.com/5fd56a0d81a808c3ac3985e906d94baf

Creating our Flow function

In a new directory called word-flow:

https://gist.github.com/b237a894a9c582ab5ed38c93ac96759f

And, make HelloFunction.java look like this:

https://gist.github.com/13c8b1a285e5901c859e21ac97f82711

It's worth reading this code carefully, remembering that anything returning a FlowFuture object is an asynchronous call, which can be chained with thenApply, thenCompose and the other Flow API methods.

We'll want some test data:

https://gist.github.com/a3ec7e121dbbffc2c6f48fd73df4e525

Deploy the function, and remember to configure the app with the location of the completer:

https://gist.github.com/be5ad5810c1c276d44ea9157b42e4da9

And... send in the Shakespeare:

https://gist.github.com/bcce9f5e7f85ecd2ede1e4416d050e11

Visualising the Flow

Check the UI on http://localhost:3002 and you should see something like this:

![flow-ui]({{ "/assets/word-flow.png" | relative_url }})

As you could see from the code above, the head and grep are executed in parallel, the linecount has to wait for the grep, and the main has to wait till everything else is finished.

Further reading

For a more thorough treatment of the different operations you can use to create Flows, see the Fn Flow User Guide. If you're at the top of the class, you can have a look at the Flow - Advanced Topics page. And a real example can be found in the Asynchronous Thumbnails project.

Finally, there is an explanation of testing Fn Java Functions and Flows

Any questions or comments? There is #fn-flow on the FnProject slack, and our github. Or hit me up on Twitter as @MaximumGilliard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment