1919
2020import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
2121
22+ import com .google .common .base .Charsets ;
2223import com .uber .cadence .client .WorkflowClient ;
2324import com .uber .cadence .client .WorkflowOptions ;
2425import com .uber .cadence .worker .Worker ;
2829import java .time .Duration ;
2930import java .util .ArrayList ;
3031import java .util .List ;
32+ import java .util .Random ;
3133
3234/**
3335 * Demonstrates asynchronous signalling of a workflow. Requires a local instance of Cadence server
@@ -51,7 +53,6 @@ public interface GreetingWorkflow {
5153 @ SignalMethod
5254 void waitForName (String name );
5355
54- /** Receives name through an external signal. */
5556 @ SignalMethod
5657 void exit ();
5758 }
@@ -94,29 +95,41 @@ public static void main(String[] args) throws Exception {
9495 worker .registerWorkflowImplementationTypes (GreetingWorkflowImpl .class );
9596 factory .start ();
9697
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+
97103 // Start a workflow execution. Usually this is done from another program.
98104 WorkflowClient workflowClient = WorkflowClient .newInstance (DOMAIN );
99105 // 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.
100107 WorkflowOptions workflowOptions =
101108 new WorkflowOptions .Builder ()
102109 .setTaskList (TASK_LIST )
103110 .setExecutionStartToCloseTimeout (Duration .ofSeconds (30 ))
111+ .setWorkflowId (workflowId )
104112 .build ();
105113 GreetingWorkflow workflow =
106114 workflowClient .newWorkflowStub (GreetingWorkflow .class , workflowOptions );
107115 // Start workflow asynchronously to not use another thread to signal.
108116 WorkflowClient .start (workflow ::getGreetings );
109117 // 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.
111119 // 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
115128 // Calling synchronous getGreeting after workflow has started reconnects to the existing
116129 // workflow and blocks until a result is available. Note that this behavior assumes that
117130 // WorkflowOptions are not configured with WorkflowIdReusePolicy.AllowDuplicate. In that case
118131 // the call would fail with WorkflowExecutionAlreadyStartedException.
119- List <String > greetings = workflow .getGreetings ();
132+ List <String > greetings = workflowById .getGreetings ();
120133 System .out .println (greetings );
121134 System .exit (0 );
122135 }
0 commit comments