Rohan Prabhu
Rohan Prabhu

Reputation: 7302

Recursing using AWS Simple Workflow Service

I have a simple asynchronous method:

@Asynchronous
public void doSomething(Promise<int> something) {
    if(something == 0) {
        return;
    }

    ActivityHolder.someActivity();
    System.out.println("Current value: " + Integer.toString(something));
    doSomething(something--);
}

This is what I'm doing to see if a certain requirement of mine is feasible or not. I essentially, want certain actions to be performed in batches, where members of each batch are run parallely. I essentially have another activity (in another class):

@Activity
public void someActivity() {
    // Some stuff
}

The output i get is (I call doSomething with 100):

Current value: 100

After that, the workflow execution fails and gives me an error stating that the activity was not found. Why was it not found? How was it found in the first execution?

Upvotes: 1

Views: 1027

Answers (2)

instanceOfObject
instanceOfObject

Reputation: 2984

Couple of things:

  1. When you pass a promise variable, you call variable.get() on it to extract its value.

  2. If you are getting activity not found error, Did you check if this activity is actually registered? Run your activity classes first and see if they are running from the logs. Ensure that you have registered the activities successfully.

  3. Code should have been something like this (using Promise.asPromise() and promise.get()):

    @Asynchronous
    public void doSomething(Promise<int> something) {
        if(something.get() == 0) {
            return;
        }
    
        ActivityHolder.someActivity();
        System.out.println("Current value: " + Integer.toString(something.get()));
        doSomething(Promise.asPromise(something.get()--));
    }
    
  4. What is ActivityHolder here? Is it client implementation of the class containing someActivity()?

Upvotes: 2

vivek garg
vivek garg

Reputation: 283

Does the above code compiles?

Promise<int> should be Promise<Integer> and something-- will give compilation error, you can not convert from Promise<Integer> to int.

Anyways, using recursion to achieve parallel activity execution does not looks right. You can easily use for loop here -

List<Promise<Void>> promiseList = new ArrayList<Promise<Void>>();
for (int i=0; i<100; i++) {
    Promise<Void> promise = ActivityHolder.someActivity();
    promiseList.add(promise);
}

callSomeOtherAsynchronousMethod(arg1, arg2, promiseList);

Upvotes: 1

Related Questions