如何编写swf启动程序

时间:2016-11-24 05:57:59

标签: java amazon-web-services aws-sdk amazon-swf

我是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控制台中查看我的活动和工作流程,但是如何触发我无法弄清楚的工作流程。

有人可以帮我。

1 个答案:

答案 0 :(得分:0)

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

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

我建议浏览AWS Flow Framework ExamplesRecipes,因为它们包含各种方案的工作端到端示例。

在您的代码中,您有一个返回String的@Asynchronous方法。不允许这样做,因为这种方法必须返回void或Promise。您的工作流代码不应该直接引用活动实现,而只能通过生成的客户端引用。

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