Skip to content

Commit 6a2d187

Browse files
author
Maxim Fateev
committed
Added stub creation by id to HelloSignal
1 parent 4b6dd55 commit 6a2d187

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/main/java/com/uber/cadence/samples/hello/HelloSignal.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2121

22+
import com.google.common.base.Charsets;
2223
import com.uber.cadence.client.WorkflowClient;
2324
import com.uber.cadence.client.WorkflowOptions;
2425
import com.uber.cadence.worker.Worker;
@@ -28,6 +29,7 @@
2829
import java.time.Duration;
2930
import java.util.ArrayList;
3031
import 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

Comments
 (0)