Skip to content

Commit 6560530

Browse files
committed
Simple sideeffect workflow
Can be used to check result in cadence-web
1 parent a8ddba2 commit 6560530

File tree

1 file changed

+73
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)