2828import java .time .Duration ;
2929import java .util .ArrayList ;
3030import java .util .List ;
31+ import org .apache .commons .lang .RandomStringUtils ;
3132
3233/**
3334 * Demonstrates asynchronous signalling of a workflow. Requires a local instance of Cadence server
@@ -51,7 +52,6 @@ public interface GreetingWorkflow {
5152 @ SignalMethod
5253 void waitForName (String name );
5354
54- /** Receives name through an external signal. */
5555 @ SignalMethod
5656 void exit ();
5757 }
@@ -94,29 +94,39 @@ public static void main(String[] args) throws Exception {
9494 worker .registerWorkflowImplementationTypes (GreetingWorkflowImpl .class );
9595 factory .start ();
9696
97+ // In a real application use a business ID like customer ID or order ID
98+ String workflowId = RandomStringUtils .randomAlphabetic (10 );
99+
97100 // Start a workflow execution. Usually this is done from another program.
98101 WorkflowClient workflowClient = WorkflowClient .newInstance (DOMAIN );
99102 // 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.
100104 WorkflowOptions workflowOptions =
101105 new WorkflowOptions .Builder ()
102106 .setTaskList (TASK_LIST )
103107 .setExecutionStartToCloseTimeout (Duration .ofSeconds (30 ))
108+ .setWorkflowId (workflowId )
104109 .build ();
105110 GreetingWorkflow workflow =
106111 workflowClient .newWorkflowStub (GreetingWorkflow .class , workflowOptions );
107112 // Start workflow asynchronously to not use another thread to signal.
108113 WorkflowClient .start (workflow ::getGreetings );
109114 // 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.
111116 // 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
115125 // Calling synchronous getGreeting after workflow has started reconnects to the existing
116126 // workflow and blocks until a result is available. Note that this behavior assumes that
117127 // WorkflowOptions are not configured with WorkflowIdReusePolicy.AllowDuplicate. In that case
118128 // the call would fail with WorkflowExecutionAlreadyStartedException.
119- List <String > greetings = workflow .getGreetings ();
129+ List <String > greetings = workflowById .getGreetings ();
120130 System .out .println (greetings );
121131 System .exit (0 );
122132 }
0 commit comments