Search code examples
javaamazon-web-servicesaws-sdkamazon-swf

How to write a swf starter program


I am new to AWS SWF and trying to create some activities, workflow and workflow starter. I found many examples and with the help of them I am now able to register a domain, create some activities and create a workflow but I am not able to write a program which will start my workflow execution.

Can someone help me here. Below are my code till now:

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();
    }

}

Aftere running Host.java , I am able to see my activities and workflow in aws console but how to trigger my workflow that I am not able to figure out.

Can someone please help me here.


Solution

  • Use the generated external client to start workflow executions:

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

    I recommend going through AWS Flow Framework Examples and Recipes as they contain working end to end examples for various scenarios.

    In your code you have an @Asynchronous method that returns a String. It is not allowed as such method must either return void or Promise. Your workflow code should never reference an activity implementation directly, but only through the generated client.

    Edit: The factory and the clients are generated by an annotation processor. Follow instructions from the "Setting up the AWS Flow Framework for Java" document on enabling code generation.