AWS SWF Java Workflow

AWS Flow Framework for Java

  • By using the AWS Flow Framework, you can focus on implementing your workflow logic, while leaving the details of communication and coordination with Amazon SWF to the framework.
  • Behind the scenes, the framework uses Amazon SWF to manage your workflow's execution and make it scalable, reliable, and auditable.
  • AWS Flow Framework-based workflows are highly concurrent, and workflows can be distributed across multiple components—each of which can run as separate processes on separate computers and can be scaled independently.
  • The workflow will continue to run if any of its components are running, making it highly fault tolerant.

A workflow application consists of three basic components:

  • An activities worker supports a set of activities, each of which is a method that executes independently to perform a particular task.
  • A workflow worker orchestrates the activities' execution and manages data flow. It is a programmatic realization of a workflow topology, which is basically a flow chart that defines when the various activities execute, whether they execute sequentially or concurrently, and so on.
  • A workflow starter starts a workflow instance, called an execution, and can interact with it during execution.

Example that compare the local version of HelloWorld and Workflow version of HelloWorld

http://docs.aws.amazon.com/amazonswf/latest/awsflowguide/getting-started-example-helloworldworkflow.html

Six entities

  • activity method class - which perform the actual tasks—are defined in an interface and implemented in a related class.
  • activity worker class
  • workflow implementation - which perform the actual workflow tasks—are defined in an interface and implemented in a related class.
  • workflow worker class
  • activity host application
  • activity client class (we don't implemented it)

Activities worker

HelloWorld implemented its activities worker as a single class. An AWS Flow Framework for Java activities worker has three basic components:

  • The activity methods—which perform the actual tasks—are defined in an interface and implemented in a related class.
  • An ActivityWorker class manages the interaction between the activity methods and Amazon SWF.
  • An activities host application registers and starts the activities worker, and handles cleanup.

workflow worker

An Amazon SWF workflow worker has three basic components.

  • A workflow implementation, which is a class that performs the workflow-related tasks.
  • An activities client class, which is basically a proxy for the activities class and is used by a workflow implementation to execute activity methods asynchronously.
  • A WorkflowWorker class, which manages the interaction between the workflow and Amazon SWF.

the real difference starts here

HelloWorldWorkflow implements the workflow in GreeterWorkflowImpl
, as follows:

import com.amazonaws.services.simpleworkflow.flow.core.Promise;

public class GreeterWorkflowImpl implements GreeterWorkflow {
   private GreeterActivitiesClient operations = new GreeterActivitiesClientImpl();

   public void greet() {
     Promise name = operations.getName();
     Promise greeting = operations.getGreeting(name);
     operations.say(greeting);
   }
}

The code is similar to HelloWorld, but with two important differences.

GreeterWorkflowImpl creates an instance of GreeterActivitiesClientImpl, the activities client, instead of GreeterActivitiesImpl, and executes activities by calling methods on the client object.

The name and greeting activities return Promise objects instead of String objects.

HelloWorld is a standard Java application that runs locally as a single process, so GreeterWorkflowImpl can implement the workflow topology by simply creating an instance of GreeterActivitiesImpl, calling the methods in order, and passing the return values from one activity to the next. With an Amazon SWF workflow, an activity's task is still performed by an activity method from GreeterActivitiesImpl. However, the method doesn't necessarily run in the same process as the workflow—it might not even run on the same system—and the workflow needs to execute the activity asynchronously. These requirements raise the following issues:

  • How to execute an activity method that might be running in a different process, perhaps on a different system.
  • How to execute an activity method asynchronously.
  • How to manage activities' input and return values. For example, if the Activity A return value is an input to Activity B, you must ensure that Activity B doesn't execute until Activity A is complete.

Activities Client

GreeterActivitiesClientImpl is basically a proxy for GreeterActivitiesImpl that allows a workflow implementation to execute the GreeterActivitiesImpl methods asynchronously.

You don't implement the activities client directly. The AWS Flow Framework for Java annotation processor uses the annotations and code from the GreeterActivities interface to generate the GreeterActivitiesClient interface and the GreeterActivitiesClientImpl class.

A workflow worker executes an activity by calling the corresponding client method. The method is asynchronous and immediately returns a Promise object, where T is the activity's return type.

** check the webpage for detail explanation in this section **

HelloWorldWorkflow Workflow and Activities Implementation

The workflow and activities implementations have associated worker classes, ActivityWorker and WorkflowWorker. They handle communication between Amazon SWF and the activities and workflow implementations by polling the appropriate Amazon SWF task list for tasks, executing the appropriate method for each task, and managing the data flow.

To associate the activity and workflow implementations with the corresponding worker objects, you implement one or more worker applications which:

  • Register workflows or activities with Amazon SWF.
  • Create worker objects and associate them with the workflow or activity worker implementations.
  • Direct the worker objects to start communicating with Amazon SWF.
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflow;
import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient;
import com.amazonaws.services.simpleworkflow.flow.ActivityWorker;
import com.amazonaws.services.simpleworkflow.flow.WorkflowWorker;

public class GreeterWorker  {
   public static void main(String[] args) throws Exception {
     ClientConfiguration config = new ClientConfiguration().withSocketTimeout(70*1000);

     String swfAccessId = System.getenv("AWS_ACCESS_KEY_ID");
     String swfSecretKey = System.getenv("AWS_SECRET_KEY");
     AWSCredentials awsCredentials = new BasicAWSCredentials(swfAccessId, swfSecretKey);

     AmazonSimpleWorkflow service = new AmazonSimpleWorkflowClient(awsCredentials, config);
     service.setEndpoint("https://swf.us-east-1.amazonaws.com");

     String domain = "helloWorldWalkthrough";
     String taskListToPoll = "HelloWorldList";

// key part:
     ActivityWorker aw = new ActivityWorker(service, domain, taskListToPoll);
     aw.addActivitiesImplementation(new GreeterActivitiesImpl());
     aw.start();

     WorkflowWorker wfw = new WorkflowWorker(service, domain, taskListToPoll);
     wfw.addWorkflowImplementationType(GreeterWorkflowImpl.class);
     wfw.start();
   }
}

The first step is to create and configure an AmazonSimpleWorkflowClient object, which invokes the underlying Amazon SWF service methods.

For convenience, GreeterWorker defines two string constants.

  • domain is the workflow's Amazon SWF domain name, which you created when you set up your Amazon SWF account. HelloWorldWorkflow assumes that you are running the workflow in the "helloWorldWalkthrough" domain.
  • taskListToPoll is the name of the task lists that Amazon SWF uses to manage communication between the workflow and activities workers. You can set the name to any convenient string. HelloWorldWorkflow uses "HelloWorldList" for both workflow and activity task lists. Behind the scenes, the names end up in different namespaces, so the two task lists are distinct.

你可能感兴趣的:(AWS SWF Java Workflow)