Skip to content

Commit 2fbc84a

Browse files
Add abstract all-in-one workflow to demonstrate basic elements (#38)
1 parent 396c46c commit 2fbc84a

File tree

8 files changed

+394
-1
lines changed

8 files changed

+394
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
public interface Activities {
21+
22+
/**
23+
* @param a
24+
* @param b
25+
* @return a*b
26+
*/
27+
long multiple(long a, long b);
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
@SuppressWarnings("ALL")
21+
public class ActivitiesImpl implements Activities {
22+
23+
@Override
24+
public long multiple(final long a, final long b) {
25+
long c = (a * b);
26+
27+
try {
28+
Thread.sleep(5 * 1000);
29+
} catch (InterruptedException e) {
30+
System.out.println("thread.Sleep exception:" + e.getMessage());
31+
}
32+
System.out.println(
33+
"After an expensive multiplication calculation... " + a + " * " + b + " = " + c);
34+
return c;
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
import static com.uber.cadence.samples.calculation.WorkflowWorker.DEFAULT_TASK_LIST;
21+
22+
import com.uber.cadence.workflow.QueryMethod;
23+
import com.uber.cadence.workflow.SignalMethod;
24+
import com.uber.cadence.workflow.WorkflowMethod;
25+
26+
public interface WorkflowMethods {
27+
28+
@WorkflowMethod(executionStartToCloseTimeoutSeconds = 3600, taskList = DEFAULT_TASK_LIST)
29+
long calculate(long a, long b, long c);
30+
31+
/** Receives n for g(n) through an external signal. */
32+
@SignalMethod
33+
void factorForGn(long n);
34+
35+
/** Returns factorForGn as a query value. */
36+
@QueryMethod
37+
long factorForGn();
38+
39+
/** Returns a*b+a*c+b*c as a query value. */
40+
@QueryMethod
41+
long abPlusAcPlusBc();
42+
43+
@QueryMethod
44+
long currentG();
45+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
import com.uber.cadence.activity.ActivityOptions;
21+
import com.uber.cadence.workflow.Async;
22+
import com.uber.cadence.workflow.Promise;
23+
import com.uber.cadence.workflow.Workflow;
24+
import java.time.Duration;
25+
import org.slf4j.Logger;
26+
27+
public class WorkflowMethodsImpl implements WorkflowMethods {
28+
29+
private static Logger LOGGER = Workflow.getLogger(WorkflowMethodsImpl.class);
30+
31+
private final ActivityOptions options =
32+
new ActivityOptions.Builder().setScheduleToCloseTimeout(Duration.ofHours(1)).build();
33+
34+
private final Activities activities = Workflow.newActivityStub(Activities.class, options);
35+
36+
private long factorForGn = -1;
37+
private long abPlusAcPlusBc = -1;
38+
private long currentG = -1;
39+
40+
@Override
41+
public long calculate(long a, long b, long c) {
42+
LOGGER.info("workflow start...");
43+
44+
// Async.invoke takes method reference and activity parameters and returns Promise.
45+
Promise<Long> ab = Async.function(activities::multiple, a, b);
46+
Promise<Long> ac = Async.function(activities::multiple, a, c);
47+
Promise<Long> bc = Async.function(activities::multiple, b, c);
48+
49+
// Promise#get blocks until result is ready.
50+
this.abPlusAcPlusBc = ab.get() + ac.get() + bc.get();
51+
52+
// waiting 30s for a human input to decide the factor N for g(n), based on a*b+a*c+b*c
53+
// the waiting timer is durable, independent of workers' liveness
54+
final boolean received = Workflow.await(Duration.ofMinutes(2), () -> this.factorForGn > 1);
55+
if (!received) {
56+
this.factorForGn = 10;
57+
}
58+
59+
long fi_1 = 0; // f(0)
60+
long fi_2 = 1; // f(1)
61+
this.currentG = 1; // current g = f(0)*f(0) + f(1)*f(1)
62+
long i = 2;
63+
64+
for (; i < this.factorForGn; i++) {
65+
// get next fibonacci number
66+
long fi = fi_1 + fi_2;
67+
fi_2 = fi_1;
68+
fi_1 = fi;
69+
70+
this.currentG += activities.multiple(fi, fi);
71+
}
72+
73+
return this.abPlusAcPlusBc + this.currentG;
74+
}
75+
76+
@Override
77+
public void factorForGn(final long n) {
78+
if (n < 2) {
79+
LOGGER.warn("receive invalid factor, " + n + ", it must be greater than 1");
80+
}
81+
if (this.factorForGn > 1) {
82+
LOGGER.warn(
83+
"factor N for g(n) is has been set to " + this.factorForGn + " and cannot be changed");
84+
}
85+
this.factorForGn = n;
86+
LOGGER.info("receive factor, " + n + " and set as factor N for g(n)");
87+
}
88+
89+
@Override
90+
public long factorForGn() {
91+
return this.factorForGn;
92+
}
93+
94+
@Override
95+
public long abPlusAcPlusBc() {
96+
return this.abPlusAcPlusBc;
97+
}
98+
99+
@Override
100+
public long currentG() {
101+
return this.currentG;
102+
}
103+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
21+
22+
import com.uber.cadence.WorkflowExecution;
23+
import com.uber.cadence.WorkflowIdReusePolicy;
24+
import com.uber.cadence.client.WorkflowClient;
25+
import com.uber.cadence.client.WorkflowClientOptions;
26+
import com.uber.cadence.client.WorkflowOptions;
27+
import com.uber.cadence.serviceclient.ClientOptions;
28+
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
29+
30+
public class WorkflowStarter {
31+
32+
@SuppressWarnings("CatchAndPrintStackTrace")
33+
public static void main(String[] args) {
34+
final WorkflowClient workflowClient =
35+
WorkflowClient.newInstance(
36+
new WorkflowServiceTChannel(ClientOptions.defaultInstance()),
37+
WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
38+
39+
WorkflowOptions workflowOptions =
40+
new WorkflowOptions.Builder()
41+
.setTaskList(WorkflowWorker.DEFAULT_TASK_LIST)
42+
.setWorkflowId("test-workflow-id")
43+
.setWorkflowIdReusePolicy(WorkflowIdReusePolicy.AllowDuplicate)
44+
.build();
45+
WorkflowMethods calculation =
46+
workflowClient.newWorkflowStub(WorkflowMethods.class, workflowOptions);
47+
48+
WorkflowExecution execution = WorkflowClient.start(calculation::calculate, 4L, 5L, 6L);
49+
System.out.println("started workflow execution" + execution);
50+
System.exit(0);
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
18+
package com.uber.cadence.samples.calculation;
19+
20+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
21+
22+
import com.uber.cadence.client.WorkflowClient;
23+
import com.uber.cadence.client.WorkflowClientOptions;
24+
import com.uber.cadence.serviceclient.ClientOptions;
25+
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
26+
import com.uber.cadence.worker.Worker;
27+
import com.uber.cadence.worker.WorkerFactory;
28+
29+
public class WorkflowWorker {
30+
31+
static final String DEFAULT_TASK_LIST = "calculation-default-tasklist";
32+
33+
@SuppressWarnings("CatchAndPrintStackTrace")
34+
public static void main(String[] args) {
35+
// Get a new client
36+
WorkflowClient workflowClient =
37+
WorkflowClient.newInstance(
38+
new WorkflowServiceTChannel(ClientOptions.defaultInstance()),
39+
WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
40+
41+
// Get worker to poll the common task list.
42+
WorkerFactory factory = WorkerFactory.newInstance(workflowClient);
43+
final Worker workerForCommonTaskList = factory.newWorker(DEFAULT_TASK_LIST);
44+
workerForCommonTaskList.registerWorkflowImplementationTypes(WorkflowMethodsImpl.class);
45+
Activities activities = new ActivitiesImpl();
46+
workerForCommonTaskList.registerActivitiesImplementations(activities);
47+
48+
// Start all workers created by this factory.
49+
factory.start();
50+
System.out.println("Worker started for task list: " + DEFAULT_TASK_LIST);
51+
}
52+
}

src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</encoder>
2525
</appender>
2626
<logger name="io.netty" level="INFO"/>
27-
<root level="WARN">
27+
<root level="INFO">
2828
<appender-ref ref="STDOUT" />
2929
</root>
3030
</configuration>

0 commit comments

Comments
 (0)