Skip to content

Commit 845afa3

Browse files
authored
Merge pull request #24 from mfateev/signal
Added stub creation by id to HelloSignal
2 parents 4b6dd55 + b9007a1 commit 845afa3

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/com/uber/cadence/samples/bookingsaga/TripBookingSaga.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class TripBookingSaga {
2727

2828
static final String TASK_LIST = "TripBooking";
2929

30+
@SuppressWarnings("CatchAndPrintStackTrace")
3031
public static void main(String[] args) {
31-
3232
// Get worker to poll the common task list.
3333
Worker.Factory factory = new Worker.Factory(DOMAIN);
3434
final Worker workerForCommonTaskList = factory.newWorker(TASK_LIST);

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.time.Duration;
2929
import java.util.ArrayList;
3030
import 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

Comments
 (0)