Skip to content
VirtualLP Virtual Learning Platform

Practical 9: AWS Flow Framework Hello-World Workflow

15 min read Topic 33 of 34 1 hands-on lab

Aim

Build a Hello World application with the AWS (Amazon Web Services) Flow Framework. Write the workflow and activity code. Start the worker programs, run the workflow and follow each step in the event history.

Theory

A workflow is a set of steps used to finish a job. An activity is one piece of work in that job. A worker is a program that picks up tasks and runs them.

Amazon Simple Workflow Service (SWF) keeps a record of the workflow. Workers ask for tasks from task lists. This repeated checking is called polling. The workflow code decides what should happen next.

The AWS Flow Framework for Java sits on top of SWF. You write normal Java interfaces and classes, and the framework does the polling, scheduling and history work for you.

restaurant order

The starter is the customer placing an order. SWF is the order board in the kitchen that remembers every order and its status. The workflow (decider) is the head chef who decides "first make the soup, then the main course". The activities are the actual cooking jobs, done by cooks (activity workers) who keep checking the board (polling the task list) for new jobs. Even if a cook goes on a break, the board still remembers the order.

flowchart LR
    ST[WorkflowStarter] -->|StartWorkflowExecution| SWF[(Amazon SWF<br/>domain: helloWorldWalkthrough)]
    SWF -->|decision task| WH["WorkflowHost<br/>WorkflowWorker<br/>HelloWorldWorkflowImpl"]
    WH -->|schedule activity printIt| SWF
    SWF -->|activity task| AH["ActivityHost<br/>ActivityWorker<br/>HelloWorldActivitiesImpl"]
    AH -->|"prints Hello World! → result"| SWF
    SWF -->|decision task| WH
    WH -->|complete workflow| SWF
Concept Meaning
Domain A named space that holds workflows, activities and their history (must be registered first)
Workflow The coordination logic (the decider) – decides which activities run and in what order
Activity One unit of work (for example print, charge a card, resize an image)
Task list A queue that workers keep checking (HelloWorldList)
Worker A program that polls a task list and runs the code
Starter A program that starts a workflow run
Event history The complete, ordered record of everything that happened in a run

Files in the Flow application

File Role
HelloWorldActivities Activity contract – @Activities, @ActivityRegistrationOptions
HelloWorldActivitiesImpl Activity implementation (prints the message)
HelloWorldWorkflow Workflow contract – @Workflow, @Execute
HelloWorldWorkflowImpl Coordination logic – calls the activity through the generated HelloWorldActivitiesClientImpl
ActivityHost Worker program: an ActivityWorker that hosts the activity implementation
WorkflowHost Worker program: a WorkflowWorker that hosts the workflow implementation
WorkflowStarter Starts a run through HelloWorldWorkflowClientExternalFactoryImpl

The annotation processor automatically generates the client classes (…ClientImpl, …ClientExternalFactoryImpl) from the annotated interfaces – you do not write them.

Key point

The workflow implementation never calls the activity directly. It calls a generated client, which asks SWF to schedule an activity task. SWF gives the task to whichever activity worker is polling that task list. This is what makes the application distributed and fault tolerant.

Requirements

JDK (Java Development Kit), Eclipse with the AWS Toolkit, the AWS SDK (Software Development Kit) for Java and the AWS Flow Framework for Java (with AspectJ / annotation processing), an AWS account with login details set up, and an SWF domain.

Program

@ActivityRegistrationOptions(defaultTaskScheduleToStartTimeoutSeconds = 300,
                             defaultTaskStartToCloseTimeoutSeconds = 10)
@Activities(version = "1.0")
public interface HelloWorldActivities {
    void printIt(String message);
}

public class HelloWorldActivitiesImpl implements HelloWorldActivities {
    @Override
    public void printIt(String message) {
        System.out.println(message);
    }
}

@Workflow
@WorkflowRegistrationOptions(defaultExecutionStartToCloseTimeoutSeconds = 3600)
public interface HelloWorldWorkflow {
    @Execute(version = "1.0")
    void helloWorld();
}

public class HelloWorldWorkflowImpl implements HelloWorldWorkflow {
    private HelloWorldActivitiesClient operations = new HelloWorldActivitiesClientImpl();

    @Override
    public void helloWorld() {
        operations.printIt("Hello World!");
    }
}

What the code does:

  • HelloWorldActivities – the activity contract: one job, printIt. The timeouts say: wait at most 300 s for a worker to pick it up, and 10 s to finish it.
  • HelloWorldActivitiesImpl – the real work: print the message.
  • HelloWorldWorkflow – the workflow contract; @Execute marks the method that starts the workflow (max run time 3600 s).
  • HelloWorldWorkflowImpl – the head chef: through the generated client it asks for printIt("Hello World!").

Worker programs and starter:

// ActivityHost.main
ActivityWorker aw = new ActivityWorker(service, "helloWorldWalkthrough", "HelloWorldList");
aw.addActivitiesImplementation(new HelloWorldActivitiesImpl());
aw.start();

// WorkflowHost.main
WorkflowWorker wfw = new WorkflowWorker(service, "helloWorldWalkthrough", "HelloWorldList");
wfw.addWorkflowImplementationType(HelloWorldWorkflowImpl.class);
wfw.start();

// WorkflowStarter.main
HelloWorldWorkflowClientExternalFactory factory =
        new HelloWorldWorkflowClientExternalFactoryImpl(service, "helloWorldWalkthrough");
HelloWorldWorkflowClientExternal workflow = factory.getClient("helloWorld-1");
workflow.helloWorld();
  • ActivityHost – a cook who keeps checking HelloWorldList for activity jobs.
  • WorkflowHost – the head chef who keeps checking the same list for decision jobs.
  • WorkflowStarter – the customer: starts a run with the ID helloWorld-1.

Procedure

Step 1 – Register the domain

aws swf register-domain --name helloWorldWalkthrough --workflow-execution-retention-period-in-days 1

Creates the space where our workflow lives (history is kept for 1 day).

Step 2 – Write the code Write the two contracts and two implementations, and complete the worker programs.

Step 3 – Start the workers Run ActivityHost, then WorkflowHost. Both keep running and poll HelloWorldList.

Step 4 – Start the workflow Run WorkflowStarter – the ActivityHost window prints Hello World!

Step 5 – Track the run Open the SWF console, or use:

aws swf list-closed-workflow-executions ...
aws swf get-workflow-execution-history ...

You will see every event of the run in order.

Virtual lab · Amazon Web Services · 35 min

Practical 9: AWS Flow Framework – Hello World workflow with activity and worker programs

Register an Amazon SWF domain, implement the HelloWorld activity, host the activity and workflow implementations in worker programs, start a workflow execution and trace it in the SWF event history.

Open lab

Output / Observations

Example output for the settings in this lesson:

ActivityHost   : Polling for activity tasks on task list 'HelloWorldList' …
                 Hello World!
WorkflowHost   : Polling for decision tasks on task list 'HelloWorldList' …
WorkflowStarter: Workflow execution started: helloWorld-1

Event history:

# Event What it means
1 WorkflowExecutionStarted The order was placed
2–4 DecisionTaskScheduled / Started / Completed The chef decided "run printIt"
5 ActivityTaskScheduled (printIt) The job was put on the board
6 ActivityTaskStarted A cook picked it up
7 ActivityTaskCompleted "Hello World!" was printed
8–10 DecisionTaskScheduled / Started / Completed The chef decided "all done"
11 WorkflowExecutionCompleted The order is finished

Conclusion

Check that the workers receive tasks and the activity prints Hello World!. Open the SWF event history and find the workflow start, activity scheduling, activity completion and workflow completion.

Viva questions

  1. What is the role of a decider? It holds the workflow logic and decides the next steps (schedule activities, complete the workflow).
  2. Why is a domain needed? It groups workflow/activity types and runs; workers and starters must use the same domain.
  3. What is a task list? A queue from which workers poll decision or activity tasks.
  4. What happens if the activity worker is not running? The activity task stays scheduled in SWF until a worker polls it or the schedule-to-start timeout expires.
  5. Which classes does the Flow annotation processor generate? Activity clients (…ClientImpl) and workflow clients/factories (…ClientExternalFactoryImpl).
  6. Where is the state of a workflow stored? In SWF's event history, not in the worker programs.

Practise this topic

Hands-on virtual lab

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.