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<String> name = operations.getName();
     Promise<String> 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<String> 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<T> 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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,463评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,868评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,213评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,666评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,759评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,725评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,716评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,484评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,928评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,233评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,393评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,073评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,718评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,308评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,538评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,338评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,260评论 2 352

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,460评论 0 23
  • 演讲者Tim Urban,读政府专业,拥有写不完的论文,别人都是刚开始就把第一时间写完,而他总在最后一刻惊慌怪兽出...
    karmen123阅读 319评论 0 0
  • 吕可乐很清楚的知道。淘淘和飞鱼的恋情,因为她最不愿知道的事。飞鱼每次都会喋喋不休的在她耳边说起。每当这个时候。吕可...
    uiu阅读 4,515评论 0 1
  • 整整一天,全世界都在刷奥斯卡,几天前很多媒体发出“百年奥斯卡如何迎来第二春”之问,今天奥斯卡就用一个大乌龙回答这个...
    龙林屿阅读 348评论 1 2
  • #田生万物#成长日记第21天 21天,很神奇的数字,估计大家跟我有着相同的感受,从刚开始的担心、害怕、焦虑,到慢慢...
    珊瑚_54b6阅读 227评论 0 0