Skip to content

Commit 9c05b9d

Browse files
committed
Add sample for local activity
1 parent 6467b49 commit 9c05b9d

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.hello;
19+
20+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
21+
22+
import com.uber.cadence.activity.ActivityMethod;
23+
import com.uber.cadence.client.WorkflowClient;
24+
import com.uber.cadence.worker.Worker;
25+
import com.uber.cadence.workflow.Workflow;
26+
import com.uber.cadence.workflow.WorkflowMethod;
27+
28+
/**
29+
* Hello World Cadence workflow that executes a single activity. Requires a local instance the
30+
* Cadence service to be running.
31+
*/
32+
public class HelloLocalActivity {
33+
34+
static final String TASK_LIST = "HelloActivity";
35+
36+
/** Workflow interface has to have at least one method annotated with @WorkflowMethod. */
37+
public interface GreetingWorkflow {
38+
/** @return greeting string */
39+
@WorkflowMethod(executionStartToCloseTimeoutSeconds = 10, taskList = TASK_LIST)
40+
String getGreeting(String name);
41+
}
42+
43+
/** Activity interface is just a POJI. */
44+
public interface GreetingActivities {
45+
@ActivityMethod(scheduleToCloseTimeoutSeconds = 2)
46+
String composeGreeting(String greeting, String name);
47+
}
48+
49+
/** GreetingWorkflow implementation that calls GreetingsActivities#composeGreeting. */
50+
public static class GreetingWorkflowImpl implements GreetingWorkflow {
51+
52+
/**
53+
* Activity stub implements activity interface and proxies calls to it to Cadence activity
54+
* invocations. Because activities are reentrant, only a single stub can be used for multiple
55+
* activity invocations.
56+
*/
57+
private final GreetingActivities activities =
58+
Workflow.newLocalActivityStub(GreetingActivities.class);
59+
60+
@Override
61+
public String getGreeting(String name) {
62+
// This is a blocking call that returns only after the activity has completed.
63+
return activities.composeGreeting("Hello", name);
64+
}
65+
}
66+
67+
static class GreetingActivitiesImpl implements GreetingActivities {
68+
@Override
69+
public String composeGreeting(String greeting, String name) {
70+
return greeting + " " + name + "!";
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
// Start a worker that hosts both workflow and activity implementations.
76+
Worker.Factory factory = new Worker.Factory(DOMAIN);
77+
Worker worker = factory.newWorker(TASK_LIST);
78+
// Workflows are stateful. So you need a type to create instances.
79+
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
80+
// Activities are stateless and thread safe. So a shared instance is used.
81+
worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
82+
// Start listening to the workflow and activity task lists.
83+
factory.start();
84+
85+
// Start a workflow execution. Usually this is done from another program.
86+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
87+
// Get a workflow stub using the same task list the worker uses.
88+
GreetingWorkflow workflow = workflowClient.newWorkflowStub(GreetingWorkflow.class);
89+
// Execute a workflow waiting for it to complete.
90+
String greeting = workflow.getGreeting("World");
91+
System.out.println(greeting);
92+
System.exit(0);
93+
}
94+
}

0 commit comments

Comments
 (0)