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