#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.
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
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
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
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.
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.