如何编写 swf 启动程序

How to write a swf starter program

我是 AWS SWF 的新手,正在尝试创建一些活动、工作流和工作流启动器。 我找到了许多示例,在它们的帮助下,我现在能够注册一个域、创建一些活动并创建一个工作流,但我无法编写一个程序来启动我的工作流执行。

有人可以帮我吗?下面是我的代码:

ActivityWorker.java:

@Activities
@ActivityRegistrationOptions(
    defaultTaskHeartbeatTimeoutSeconds = FlowConstants.NONE, 
    defaultTaskScheduleToCloseTimeoutSeconds = 300, 
    defaultTaskScheduleToStartTimeoutSeconds = FlowConstants.NONE, 
    defaultTaskStartToCloseTimeoutSeconds = 300)
public interface ActivityWorker {
@Activity(name = "swftest1", version = "1.0")
@ExponentialRetry(
        initialRetryIntervalSeconds=10,
        backoffCoefficient=1,
        maximumAttempts=5)
public String print() throws IOException;


@Activity(name = "swftest2", version = "1.0")
@ExponentialRetry(
        initialRetryIntervalSeconds=10,
        backoffCoefficient=1,
        maximumAttempts=5)
public String print2() throws IOException;

}

ActivityWorkerImpl.java

public class ActivityWorkerImpl implements ActivityWorker{

@Override
public String print() throws IOException {
    System.out.println("Hello..");
    return "Printing..";
}

@Override
public String print2() throws IOException {
    System.out.println("Hello..");
    return "Printing2..";
}
}

Worker.java

@Workflow
@WorkflowRegistrationOptions(
    defaultExecutionStartToCloseTimeoutSeconds = 600, 
    defaultTaskStartToCloseTimeoutSeconds = 300)
public interface Worker {
@Execute(version = "1.0", name="worker1")
public void greet();

}

WorkerImpl.java

public class WorkerImpl implements Worker{

private final ActivityWorkerImpl actWorkerImpl = new ActivityWorkerImpl();

@Override
public void greet() {
    new TryCatchFinally() {
        @Override
        protected void doTry() throws Throwable {
            String res= downloadFromS3();
            System.out.println("res.."+res);
        }

        @Override
        protected void doCatch(Throwable e) throws Throwable {
            throw e;
        }

        @Override
        protected void doFinally() throws Throwable {
            // noop
        }
    };
}


@Asynchronous
private String downloadFromS3() throws IOException {
    return this.actWorkerImpl.print();
}

}

Host.java

public class ActivityHost {

    public static void main(String[] args) throws Exception {

          String domain = "test1";
          String taskListToPoll = "HelloWorldList";

         ClientConfiguration config = new ClientConfiguration().withSocketTimeout(70*1000);

         String swfAccessId = "myid";
         String swfSecretKey = "mysecretekey";
         AWSCredentials awsCredentials = new BasicAWSCredentials(swfAccessId, swfSecretKey);

       AmazonSimpleWorkflow swf = new AmazonSimpleWorkflowClient(awsCredentials, config);

        ActivityWorker activityworker = new ActivityWorker(swf, domain, taskListToPoll);
        //adding activity
        activityworker.addActivitiesImplementation(new ActivityWorkerImpl());

        activityworker.start();

        //adding workflow
        WorkflowWorker worker = new WorkflowWorker(swf,domain, taskListToPoll);

        worker.addWorkflowImplementationType(WorkerImpl.class);

        worker.start();
    }

}

在 运行 Host.java 之后,我可以在 aws 控制台中看到我的活动和工作流程,但是我无法弄清楚如何触发我的工作流程。

有人可以帮我吗。

使用生成的外部客户端启动工作流执行:

WorkerClientExternalFactory f = new WorkerClientExternalFActory (swf, domain);
WorkerClientExternal w = f.getClient();
w.greet();

我建议使用 AWS Flow Framework Examples and Recipes,因为它们包含适用于各种场景的端到端工作示例。

在您的代码中,您有一个 return 字符串的 @Asynchronous 方法。这是不允许的,因为这种方法必须 return void 或 Promise。您的工作流代码不应直接引用 activity 实现,而只能通过生成的客户端引用。

编辑: 工厂和客户端由注解处理器生成。按照 "Setting up the AWS Flow Framework for Java" 文档中有关启用代码生成的说明进行操作。