Skip to content

Commit 38c3fdc

Browse files
author
Maxim Fateev
committed
HelloAsyncActivityCompletion added
1 parent 1dc9c67 commit 38c3fdc

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.activity.Activity;
20+
import com.uber.cadence.activity.DoNotCompleteOnReturn;
21+
import com.uber.cadence.client.ActivityCompletionClient;
22+
import com.uber.cadence.client.WorkflowClient;
23+
import com.uber.cadence.client.WorkflowOptions;
24+
import com.uber.cadence.worker.Worker;
25+
import com.uber.cadence.workflow.ActivityOptions;
26+
import com.uber.cadence.workflow.Workflow;
27+
import com.uber.cadence.workflow.WorkflowMethod;
28+
29+
import java.util.concurrent.ForkJoinPool;
30+
31+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
32+
33+
/**
34+
* Hello World Cadence workflow that executes a single activity.
35+
* Requires a local instance of Cadence server running.
36+
*/
37+
public class HelloAsyncActivityCompletion {
38+
39+
private static final String TASK_LIST = "HelloActivity";
40+
41+
/**
42+
* Workflow interface has to have at least one method annotated with @WorkflowMethod.
43+
*/
44+
public interface GreetingWorkflow {
45+
/**
46+
* @return greeting string
47+
*/
48+
@WorkflowMethod
49+
String getGreeting(String name);
50+
}
51+
52+
/**
53+
* Activity interface is just a POJI
54+
*/
55+
public interface GreetingActivities {
56+
String composeGreeting(String greeting, String name);
57+
}
58+
59+
/**
60+
* GreetingWorkflow implementation that calls GreetingsActivities#printIt.
61+
*/
62+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
63+
64+
/**
65+
* Activity stub implements activity interface and proxies calls to it to Cadence activity invocations.
66+
* As activities are reentrant only a single stub can be used for multiple activity invocations.
67+
*/
68+
private final GreetingActivities activities = Workflow.newActivityStub(
69+
GreetingActivities.class,
70+
new ActivityOptions.Builder().setScheduleToCloseTimeoutSeconds(10).build());
71+
72+
@Override
73+
public String getGreeting(String name) {
74+
// This is blocking call that returns only after activity is completed.
75+
return activities.composeGreeting("Hello", name);
76+
}
77+
}
78+
79+
private static class GreetingActivitiesImpl implements GreetingActivities {
80+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
81+
82+
/**
83+
* Demonstrates how implement an activity asynchronously.
84+
* When @DoNotCompleteOnReturn is present activity implementation function returning doesn't complete
85+
* the activity.
86+
*/
87+
@DoNotCompleteOnReturn
88+
@Override
89+
public String composeGreeting(String greeting, String name) {
90+
// TaskToken is a correlation token used to match activity task with completion
91+
byte[] taskToken = Activity.getTaskToken();
92+
// In real life this request can be executed anywhere. By external service for example.
93+
ForkJoinPool.commonPool().execute(() -> composeGreetingAsync(taskToken, greeting, name));
94+
// When @DoNotCompleteOnReturn is specified the return value is ignored.
95+
return "ignored";
96+
}
97+
98+
private void composeGreetingAsync(byte[] taskToken, String greeting, String name) {
99+
// To complete activity from a different thread or process use ActivityCompletionClient.
100+
ActivityCompletionClient completionClient = workflowClient.newActivityCompletionClient();
101+
String result = greeting + " " + name + "!";
102+
completionClient.complete(taskToken, result);
103+
}
104+
}
105+
106+
public static void main(String[] args) {
107+
// Start a worker that hosts both workflow and activity implementation
108+
Worker worker = new Worker(DOMAIN, TASK_LIST);
109+
// Workflows are stateful. So need a type to create instances.
110+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
111+
// Activities are stateless and thread safe. So a shared instance is used.
112+
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
113+
// Start listening to the workflow and activity task lists.
114+
worker.start();
115+
116+
// Start a workflow execution. Usually it is done from another program.
117+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
118+
// Get a workflow stub using the same task list the worker uses.
119+
WorkflowOptions workflowOptions = new WorkflowOptions.Builder()
120+
.setTaskList(TASK_LIST)
121+
.setExecutionStartToCloseTimeoutSeconds(30)
122+
.build();
123+
GreetingWorkflow workflow = workflowClient.newWorkflowStub(GreetingWorkflow.class,
124+
workflowOptions);
125+
// Execute a workflow waiting for it complete.
126+
String greeting = workflow.getGreeting("World");
127+
System.out.println(greeting);
128+
System.exit(0);
129+
}
130+
}

0 commit comments

Comments
 (0)