The series so far:

1.   Hello World exam­ple

3.   Par­al­leliz­ing activities

 

In this post we’re going to go beyond the pre­vi­ous Hello World exam­ple and show you how to use the SWF exten­sions library to model work­flows with mul­ti­ple steps and allow data to flow nat­u­rally from one step to the next.

When using the exten­sion library, the input to a work­flow exe­cu­tion is passed onto the first activ­ity of a work­flow as input by default (as per the pre­vi­ous Hello World exam­ple), and the result of that activ­ity is passed onto the next activ­ity as input and so on. The result of the last activ­ity is then used as the result of the whole workflow :

image

When you model an activ­ity using the Activ­ity type you need to pass in a func­tion with the sig­na­ture of string –> string. This func­tion is called against the input when the gen­er­ated activ­ity worker receives a task, and will use the return value from the func­tion as the result of the activity.

What you might not real­ize is that the Activ­ity type is actu­ally a spe­cial­ized form of the generic Activity<TInput, TOut­put> type which allows you to sup­ply func­tions with arbi­trary input and out­put types and sim­ply uses the ServiceStack.Text JSON seri­al­izer to mar­shal data to and from string. I had decided to use the ServiceStack.Text seri­al­izer because it’s the fastest JSON seri­al­izer around based on my bench­marks.

Exam­ple : Sum Web Page Lengths

Sup­pose you want to count and sum the size of the HTML pages given a num­ber of URLs and return the sum as the result:

image

To imple­ment this work­flow you need to attach two activ­i­ties to the work­flow, the first requir­ing a func­tion that turns a string array into an int array and the sec­ond aggre­gates the int array into a sin­gle inte­ger value, some­thing along the lines of:

The main things to take away from this exam­ple is that:

  1. you can attach mul­ti­ple activ­i­ties to a work­flow by chain­ing them up with the ++> oper­a­tor
  2. han­dler func­tions for activ­i­ties do not have to have string –> string sig­na­ture

 

Let’s take a closer look at the two activities:

image

image

image

As you can see, given the input JSON string to the workflow:

[ “http://www.google.com”, “http://www.yahoo.com”, “http://www.bing.com” ]

the activ­i­ties did what they were sup­posed to and first trans­lated the input into an int array before sum­ming them to give a total for the length of the land­ing pages for Google, Yahoo and Bing, and lit­tle sur­prise that Yahoo’s land­ing page is nearly an order of mag­ni­tude big­ger than the rest!

 

Part­ing Thoughts

In this post I demon­strated how to model a work­flow with mul­ti­ple steps which accept and return arbi­trary types as input and out­put. In the next post I’ll demon­strate how to sched­ule mul­ti­ple activ­i­ties to be per­formed in par­al­lel as a sin­gle step of a workflow.

Share

3 Responses to “Introduction to AWS SimpleWorkflow Extensions Part 2 – Beyond Hello World”

  1. […] Yan Cui pre­sented “Intro­duc­tion to AWS Sim­ple­Work­flow Exten­sions Part 2 – Beyond Hello World“. […]

Leave a Reply