Skip to content

Commit 503b457

Browse files
authored
Added HelloActivityRetry (#3)
* Added HelloActivityRetry * Updated comments
1 parent 0ab9964 commit 503b457

File tree

9 files changed

+135
-15
lines changed

9 files changed

+135
-15
lines changed

src/main/java/com/uber/cadence/samples/hello/HelloActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface GreetingActivities {
5252
}
5353

5454
/**
55-
* GreetingWorkflow implementation that calls GreetingsActivities#printIt.
55+
* GreetingWorkflow implementation that calls GreetingsActivities#composeGreeting.
5656
*/
5757
public static class GreetingWorkflowImpl implements GreetingWorkflow {
5858

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
package com.uber.cadence.samples.hello;
18+
19+
import com.uber.cadence.client.WorkflowClient;
20+
import com.uber.cadence.client.WorkflowOptions;
21+
import com.uber.cadence.worker.Worker;
22+
import com.uber.cadence.workflow.ActivityOptions;
23+
import com.uber.cadence.workflow.Functions;
24+
import com.uber.cadence.workflow.RetryOptions;
25+
import com.uber.cadence.workflow.Workflow;
26+
import com.uber.cadence.workflow.WorkflowMethod;
27+
28+
import java.time.Duration;
29+
30+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
31+
32+
/**
33+
* Demonstrates activity retries using exponential backoff algorithm.
34+
* Requires a local instance of Cadence server running.
35+
*/
36+
public class HelloActivityRetry {
37+
38+
private static final String TASK_LIST = "HelloActivity";
39+
40+
public interface GreetingWorkflow {
41+
/**
42+
* @return greeting string
43+
*/
44+
@WorkflowMethod
45+
String getGreeting(String name);
46+
}
47+
48+
public interface GreetingActivities {
49+
String composeGreeting(String greeting, String name);
50+
}
51+
52+
/**
53+
* GreetingWorkflow implementation that demonstrates activity stub configured with
54+
* {@link RetryOptions}.
55+
*/
56+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
57+
58+
/**
59+
* To enable activity retry set {@link RetryOptions} on {@link ActivityOptions}.
60+
* It also works for activities invoked through
61+
* {@link com.uber.cadence.workflow.Async#invoke(Functions.Proc)} and for child workflows.
62+
*/
63+
private final GreetingActivities activities = Workflow.newActivityStub(
64+
GreetingActivities.class,
65+
new ActivityOptions.Builder()
66+
.setScheduleToCloseTimeoutSeconds(10)
67+
.setRetryOptions(new RetryOptions.Builder()
68+
.setInitialInterval(Duration.ofSeconds(1))
69+
.setExpiration(Duration.ofMinutes(1))
70+
.build())
71+
.build());
72+
73+
@Override
74+
public String getGreeting(String name) {
75+
// This is blocking call that returns only after activity is completed.
76+
return activities.composeGreeting("Hello", name);
77+
}
78+
}
79+
80+
private static class GreetingActivitiesImpl implements GreetingActivities {
81+
private int callCount;
82+
private long lastInvocationTime;
83+
84+
@Override
85+
public synchronized String composeGreeting(String greeting, String name) {
86+
if (lastInvocationTime != 0) {
87+
long timeSinceLastInvocation = System.currentTimeMillis() - lastInvocationTime;
88+
System.out.print(timeSinceLastInvocation + " milliseconds since last invocation. ");
89+
}
90+
lastInvocationTime = System.currentTimeMillis();
91+
if (++callCount < 4) {
92+
System.out.println("composeGreeting activity is going to fail");
93+
throw new IllegalStateException("not yet");
94+
}
95+
System.out.println("composeGreeting activity is going to complete");
96+
return greeting + " " + name + "!";
97+
}
98+
}
99+
100+
public static void main(String[] args) {
101+
// Start a worker that hosts both workflow and activity implementation
102+
Worker worker = new Worker(DOMAIN, TASK_LIST);
103+
// Workflows are stateful. So need a type to create instances.
104+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
105+
// Activities are stateless and thread safe. So a shared instance is used.
106+
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
107+
// Start listening to the workflow and activity task lists.
108+
worker.start();
109+
110+
// Start a workflow execution. Usually it is done from another program.
111+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
112+
// Get a workflow stub using the same task list the worker uses.
113+
WorkflowOptions workflowOptions = new WorkflowOptions.Builder()
114+
.setTaskList(TASK_LIST)
115+
.setExecutionStartToCloseTimeoutSeconds(30)
116+
.build();
117+
GreetingWorkflow workflow = workflowClient.newWorkflowStub(GreetingWorkflow.class,
118+
workflowOptions);
119+
// Execute a workflow waiting for it complete.
120+
String greeting = workflow.getGreeting("World");
121+
System.out.println(greeting);
122+
System.exit(0);
123+
}
124+
}

src/main/java/com/uber/cadence/samples/hello/HelloAsync.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import com.uber.cadence.worker.Worker;
2222
import com.uber.cadence.workflow.ActivityOptions;
2323
import com.uber.cadence.workflow.Async;
24+
import com.uber.cadence.workflow.Functions;
2425
import com.uber.cadence.workflow.Promise;
2526
import com.uber.cadence.workflow.Workflow;
2627
import com.uber.cadence.workflow.WorkflowMethod;
2728

2829
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2930

3031
/**
31-
* Hello World Cadence workflow that executes a couple activities in parallel.
32+
* Demonstrates asynchronous activity invocation.
3233
* Requires a local instance of Cadence server running.
3334
*/
3435
public class HelloAsync {
@@ -40,15 +41,13 @@ public interface GreetingWorkflow {
4041
String getGreeting(String name);
4142
}
4243

43-
/**
44-
* Activity interface is just a POJI
45-
*/
4644
public interface GreetingActivities {
4745
String composeGreeting(String greeting, String name);
4846
}
4947

5048
/**
51-
* GreetingWorkflow implementation that calls GreetingsActivities#printIt.
49+
* GreetingWorkflow implementation that calls GreetingsActivities#composeGreeting
50+
* using {@link Async#invoke(Functions.Proc)}.
5251
*/
5352
public static class GreetingWorkflowImpl implements GreetingWorkflow {
5453

src/main/java/com/uber/cadence/samples/hello/HelloAsyncActivityCompletion.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@
3131
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
3232

3333
/**
34-
* Hello World Cadence workflow that executes a single activity.
34+
* Demonstrates an asynchronous activity implementation.
3535
* Requires a local instance of Cadence server running.
3636
*/
3737
public class HelloAsyncActivityCompletion {
3838

3939
private static final String TASK_LIST = "HelloActivity";
4040

41-
/**
42-
* Workflow interface has to have at least one method annotated with @WorkflowMethod.
43-
*/
4441
public interface GreetingWorkflow {
4542
/**
4643
* @return greeting string

src/main/java/com/uber/cadence/samples/hello/HelloAsyncLambda.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
3030

3131
/**
32-
* Hello World Cadence workflow that executes two sequences of activities in parallel.
32+
* Demonstrates async invocation of an entire sequence of activities.
3333
* Requires a local instance of Cadence server running.
3434
*/
3535
public class HelloAsyncLambda {

src/main/java/com/uber/cadence/samples/hello/HelloChild.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
3131

3232
/**
33-
* Hello World Cadence workflow that executes a single child workflow.
33+
* Demonstrates child workflow.
3434
* Requires a local instance of Cadence server running.
3535
*/
3636
public class HelloChild {

src/main/java/com/uber/cadence/samples/hello/HelloPeriodic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
3333

3434
/**
35-
* Hello World Cadence workflow that executes a single activity periodically.
35+
* Demonstrates a "cron" workflow that executes activity periodically.
3636
* Requires a local instance of Cadence server running.
3737
*/
3838
public class HelloPeriodic {

src/main/java/com/uber/cadence/samples/hello/HelloQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2929

3030
/**
31-
* Hello World Cadence workflow that returns greeting when queried.
31+
* Demonstrates query capability.
3232
* Requires a local instance of Cadence server running.
3333
*/
3434
public class HelloQuery {

src/main/java/com/uber/cadence/samples/hello/HelloSignal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2828

2929
/**
30-
* Hello World Cadence workflow that blocks until a signal is received.
30+
* Demonstrates asynchronous signalling of a workflow.
3131
* Requires a local instance of Cadence server running.
3232
*/
3333
@SuppressWarnings("ALL")

0 commit comments

Comments
 (0)