19
19
20
20
import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
21
21
22
+ import com .google .common .base .Charsets ;
22
23
import com .uber .cadence .client .WorkflowClient ;
23
24
import com .uber .cadence .client .WorkflowOptions ;
24
25
import com .uber .cadence .worker .Worker ;
28
29
import java .time .Duration ;
29
30
import java .util .ArrayList ;
30
31
import java .util .List ;
32
+ import java .util .Random ;
31
33
32
34
/**
33
35
* Demonstrates asynchronous signalling of a workflow. Requires a local instance of Cadence server
@@ -51,7 +53,6 @@ public interface GreetingWorkflow {
51
53
@ SignalMethod
52
54
void waitForName (String name );
53
55
54
- /** Receives name through an external signal. */
55
56
@ SignalMethod
56
57
void exit ();
57
58
}
@@ -94,29 +95,41 @@ public static void main(String[] args) throws Exception {
94
95
worker .registerWorkflowImplementationTypes (GreetingWorkflowImpl .class );
95
96
factory .start ();
96
97
98
+ // In real applications use a business level ID like customerId or orderId
99
+ byte [] idBytes = new byte [10 ];
100
+ new Random ().nextBytes (idBytes );
101
+ String workflowId = new String (idBytes , Charsets .UTF_8 );
102
+
97
103
// Start a workflow execution. Usually this is done from another program.
98
104
WorkflowClient workflowClient = WorkflowClient .newInstance (DOMAIN );
99
105
// Get a workflow stub using the same task list the worker uses.
106
+ // The newly started workflow is going to have the workflowId generated above.
100
107
WorkflowOptions workflowOptions =
101
108
new WorkflowOptions .Builder ()
102
109
.setTaskList (TASK_LIST )
103
110
.setExecutionStartToCloseTimeout (Duration .ofSeconds (30 ))
111
+ .setWorkflowId (workflowId )
104
112
.build ();
105
113
GreetingWorkflow workflow =
106
114
workflowClient .newWorkflowStub (GreetingWorkflow .class , workflowOptions );
107
115
// Start workflow asynchronously to not use another thread to signal.
108
116
WorkflowClient .start (workflow ::getGreetings );
109
117
// After start for getGreeting returns, the workflow is guaranteed to be started.
110
- // So we can send a signal to it using workflow stub.
118
+ // So we can send a signal to it using the workflow stub.
111
119
// This workflow keeps receiving signals until exit is called
112
- workflow .waitForName ("World" );
113
- workflow .waitForName ("Universe" );
114
- workflow .exit ();
120
+ workflow .waitForName ("World" ); // sends waitForName signal
121
+
122
+ // Create a new stub using the workflowId.
123
+ // This is to demonstrate that to send a signal only the workflowId is required.
124
+ GreetingWorkflow workflowById =
125
+ workflowClient .newWorkflowStub (GreetingWorkflow .class , workflowId );
126
+ workflowById .waitForName ("Universe" ); // sends waitForName signal
127
+ workflowById .exit (); // sends exit signal
115
128
// Calling synchronous getGreeting after workflow has started reconnects to the existing
116
129
// workflow and blocks until a result is available. Note that this behavior assumes that
117
130
// WorkflowOptions are not configured with WorkflowIdReusePolicy.AllowDuplicate. In that case
118
131
// the call would fail with WorkflowExecutionAlreadyStartedException.
119
- List <String > greetings = workflow .getGreetings ();
132
+ List <String > greetings = workflowById .getGreetings ();
120
133
System .out .println (greetings );
121
134
System .exit (0 );
122
135
}
0 commit comments