Amazon Simple Workflow Service
开发人员指南 (API 版本 2012-01-25)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用 Amazon Simple Workflow Service 实施排他选择

在某些应用场景中,建议您根据前一个活动的结果排定不同组活动。您可以通过排他选择模式创建灵活的工作流程,以满足应用程序的复杂要求。

Amazon Simple Workflow Service (Amazon SWF) 没有特定的排他选择操作。要使用排他选择,您只需写入您的决策程序逻辑以根据前一个活动的结果作出不同决策。以下为适用于排他选择的某些应用程序:

  • 如果前一个活动的结果不成功,执行清除活动

  • 根据客户购买的是基本还是高级计划来排定不同的活动

  • 根据客户订单历史执行不同的客户身份验证活动

在电子商务示例中,您可以使用排他选择根据信用卡付费结果给订单发货或取消订单。在下图中,如果信用卡付费成功,决策程序将排定 Ship Order 和 Record Completion 活动任务。否则,它将排定 Cancel Order 和 Email Customer 活动任务。

 客户订单工作流程图

如果信用卡成功付费,决策程序将排定 ShipOrder 活动。否则,决策程序将排定 CancelOrder 活动。

在这种情况下,给决策程序编程以解析历史记录并决定信用卡是否付费成功。要实现此操作,您要有与下列内容相类似的逻辑

IF lastEvent = "WorkflowExecutionStarted" addToDecisions ScheduleActivityTask(ActivityType = "VerifyOrderActivity") ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "VerifyOrderActivity" addToDecisions ScheduleActivityTask(ActivityType = "ChargeCreditCardActivity") #Successful Credit Card Charge Activities ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "ChargeCreditCardActivity" addToDecisions ScheduleActivityTask(ActivityType = "ShipOrderActivity") ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "ShipOrderActivity" addToDecisions ScheduleActivityTask(ActivityType = "RecordOrderCompletionActivity") ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "RecordOrderCompletionActivity" addToDecisions CompleteWorkflowExecution #Unsuccessful Credit Card Charge Activities ELSIF lastEvent = "ActivityTaskFailed" AND ActivityType = "ChargeCreditCardActivity" addToDecisions ScheduleActivityTask(ActivityType = "CancelOrderActivity") ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "CancelOrderActivity" addToDecisions ScheduleActivityTask(ActivityType = "EmailCustomerActivity") ELSIF lastEvent = "ActivityTaskCompleted" AND ActivityType = "EmailCustomerActivity" addToDecisions CompleteWorkflowExecution ENDIF

如果信用卡付费成功,决策程序应响应 RespondDecisionTaskCompleted 以排定 ShipOrder 活动。

https://swf.us-east-1.amazonaws.com RespondDecisionTaskCompleted { "taskToken": "12342e17-80f6-FAKE-TASK-TOKEN32f0223", "decisions":[ { "decisionType":"ScheduleActivityTask", "scheduleActivityTaskDecisionAttributes":{ "control":"OPTIONAL_DATA_FOR_DECIDER", "activityType":{ "name":"ShipOrder", "version":"2.4" }, "activityId":"3e2e6e55-e7c4-fee-deed-aa815722b7be", "scheduleToCloseTimeout":"3600", "taskList":{ "name":"SHIPPING" }, "scheduleToStartTimeout":"600", "startToCloseTimeout":"3600", "heartbeatTimeout":"300", "input": "123 Main Street, Anytown, United States" } } ] }

如果信用卡未付费成功,决策程序应响应 RespondDecisionTaskCompleted 以排定 CancelOrder 活动。

https://swf.us-east-1.amazonaws.com RespondDecisionTaskCompleted { "taskToken": "12342e17-80f6-FAKE-TASK-TOKEN32f0223", "decisions":[ { "decisionType":"ScheduleActivityTask", "scheduleActivityTaskDecisionAttributes":{ "control":"OPTIONAL_DATA_FOR_DECIDER", "activityType":{ "name":"CancelOrder", "version":"2.4" }, "activityId":"3e2e6e55-e7c4-fee-deed-aa815722b7be", "scheduleToCloseTimeout":"3600", "taskList":{ "name":"CANCELLATIONS" }, "scheduleToStartTimeout":"600", "startToCloseTimeout":"3600", "heartbeatTimeout":"300", "input": "Out of Stock" } } ] }

如果 Amazon SWF 能够通过 RespondDecisionTaskCompleted 操作验证数据,则 Amazon SWF 会返回与下面类似的成功 HTTP 响应。

HTTP/1.1 200 OK Content-Length: 11 Content-Type: application/json x-amzn-RequestId: 93cec6f7-0747-11e1-b533-79b402604df1