Skip to content

Commit d748dd1

Browse files
authored
Merge pull request #41 from RaymondKroon/sideeffect-example
Simple side effect workflow
2 parents a8ddba2 + 8cf9ed5 commit d748dd1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.uber.cadence.samples.hello;
2+
3+
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
4+
5+
import com.uber.cadence.client.WorkflowClient;
6+
import com.uber.cadence.worker.Worker;
7+
import com.uber.cadence.workflow.QueryMethod;
8+
import com.uber.cadence.workflow.Workflow;
9+
import com.uber.cadence.workflow.WorkflowMethod;
10+
import java.util.UUID;
11+
12+
/**
13+
* Hello SideEffect Cadence workflow that sets a SideEffect. The set value can be queried Requires a
14+
* local instance the Cadence service to be running.
15+
*/
16+
public class HelloSideEffect {
17+
18+
static final String TASK_LIST = "HelloSideEffect";
19+
20+
/** Workflow interface has to have at least one method annotated with @WorkflowMethod. */
21+
public interface SideEffectWorkflow {
22+
@WorkflowMethod(executionStartToCloseTimeoutSeconds = 10, taskList = TASK_LIST)
23+
void start();
24+
25+
/** @return set value */
26+
@QueryMethod
27+
String get();
28+
}
29+
30+
public static class SideEffectWorkflowImpl implements SideEffectWorkflow {
31+
32+
private String value = "";
33+
34+
@Override
35+
public void start() {
36+
this.value =
37+
Workflow.sideEffect(
38+
String.class,
39+
() -> {
40+
return UUID.randomUUID().toString();
41+
});
42+
}
43+
44+
@Override
45+
public String get() {
46+
return this.value;
47+
}
48+
}
49+
50+
public static void main(String[] args) {
51+
// Start a worker that hosts both workflow and activity implementations.
52+
Worker.Factory factory = new Worker.Factory(DOMAIN);
53+
Worker worker = factory.newWorker(TASK_LIST);
54+
// Workflows are stateful. So you need a type to create instances.
55+
worker.registerWorkflowImplementationTypes(SideEffectWorkflowImpl.class);
56+
// Start listening to the workflow and activity task lists.
57+
factory.start();
58+
59+
// Start a workflow execution. Usually this is done from another program.
60+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
61+
// Get a workflow stub using the same task list the worker uses.
62+
SideEffectWorkflow workflow = workflowClient.newWorkflowStub(SideEffectWorkflow.class);
63+
// Execute a workflow waiting for it to complete.
64+
workflow.start();
65+
// Query and print the set value
66+
System.out.println(workflow.get());
67+
System.exit(0);
68+
}
69+
}

0 commit comments

Comments
 (0)