Skip to content

Commit d269060

Browse files
authored
expand HelloSignal example to send multiple signals (#1)
* expand HelloSignal example to send multiple signals
1 parent 555887a commit d269060

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
import com.uber.cadence.workflow.Workflow;
2828
import com.uber.cadence.workflow.WorkflowMethod;
2929
import java.time.Duration;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.concurrent.ArrayBlockingQueue;
33+
import java.util.concurrent.BlockingDeque;
34+
import java.util.concurrent.BlockingQueue;
35+
import java.util.concurrent.CompletableFuture;
3036

3137
/**
3238
* Demonstrates asynchronous signalling of a workflow. Requires a local instance of Cadence server
@@ -39,32 +45,53 @@ public class HelloSignal {
3945

4046
/** Workflow interface must have a method annotated with @WorkflowMethod. */
4147
public interface GreetingWorkflow {
42-
/** @return greeting string */
48+
/** @return list of greeting strings that were received through the
49+
* waitForNameMethod. This method will block until the number of greetings
50+
* specified are received. */
4351
@WorkflowMethod
44-
String getGreeting();
52+
List<String> getGreetings();
4553

4654
/** Receives name through an external signal. */
4755
@SignalMethod
4856
void waitForName(String name);
57+
58+
/** Receives name through an external signal. */
59+
@SignalMethod
60+
void exit();
4961
}
5062

5163
/** GreetingWorkflow implementation that returns a greeting. */
5264
public static class GreetingWorkflowImpl implements GreetingWorkflow {
5365

54-
private final CompletablePromise<String> name = Workflow.newPromise();
66+
List<String> messageQueue = new ArrayList<>(10);
67+
boolean exit = false;
5568

5669
@Override
57-
public String getGreeting() {
58-
return "Hello " + name.get() + "!";
70+
public List<String> getGreetings() {
71+
List<String> receivedMessages = new ArrayList<>(10);
72+
73+
while (true) {
74+
Workflow.await(() -> !messageQueue.isEmpty() || exit);
75+
if(messageQueue.isEmpty() && exit){
76+
return receivedMessages;
77+
}
78+
String message = messageQueue.remove(0);
79+
receivedMessages.add(message);
80+
}
5981
}
6082

6183
@Override
6284
public void waitForName(String name) {
63-
this.name.complete(name);
85+
messageQueue.add("Hello " + name + "!");
86+
}
87+
88+
@Override
89+
public void exit() {
90+
exit = true;
6491
}
6592
}
6693

67-
public static void main(String[] args) {
94+
public static void main(String[] args) throws Exception {
6895
// Start a worker that hosts the workflow implementation.
6996
Worker worker = new Worker(DOMAIN, TASK_LIST);
7097
worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
@@ -80,17 +107,21 @@ public static void main(String[] args) {
80107
.build();
81108
GreetingWorkflow workflow =
82109
workflowClient.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
110+
workflow.wait();
83111
// Start workflow asynchronously to not use another thread to signal.
84-
WorkflowClient.start(workflow::getGreeting);
112+
WorkflowClient.start(workflow::getGreetings);
85113
// After start for getGreeting returns, the workflow is guaranteed to be started.
86114
// So we can send a signal to it using workflow stub.
115+
// This workflow keeps receiving signals until exit is called
87116
workflow.waitForName("World");
117+
workflow.waitForName("Universe");
118+
workflow.exit();
88119
// Calling synchronous getGreeting after workflow has started reconnects to the existing
89120
// workflow and blocks until a result is available. Note that this behavior assumes that
90121
// WorkflowOptions are not configured with WorkflowIdReusePolicy.AllowDuplicate. In that case
91122
// the call would fail with WorkflowExecutionAlreadyStartedException.
92-
String greeting = workflow.getGreeting();
93-
System.out.println(greeting);
123+
List<String> greetings = workflow.getGreetings();
124+
System.out.println(greetings);
94125
System.exit(0);
95126
}
96127
}

src/test/java/com/uber/cadence/samples/hello/HelloSignalTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import com.uber.cadence.testing.TestWorkflowEnvironment;
2626
import com.uber.cadence.worker.Worker;
2727
import java.time.Duration;
28+
import java.util.List;
29+
import java.util.concurrent.CompletableFuture;
30+
2831
import org.junit.After;
2932
import org.junit.Before;
3033
import org.junit.Rule;
@@ -68,8 +71,8 @@ public void tearDown() {
6871
testEnv.close();
6972
}
7073

71-
@Test(timeout = 5000)
72-
public void testSignal() {
74+
@Test//(timeout = 5000)
75+
public void testSignal() throws Exception {
7376
// Get a workflow stub using the same task list the worker uses.
7477
WorkflowOptions workflowOptions =
7578
new WorkflowOptions.Builder()
@@ -80,21 +83,25 @@ public void testSignal() {
8083
workflowClient.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
8184

8285
// Start workflow asynchronously to not use another thread to signal.
83-
WorkflowClient.start(workflow::getGreeting);
86+
WorkflowClient.start(workflow::getGreetings);
8487

8588
// After start for getGreeting returns, the workflow is guaranteed to be started.
8689
// So we can send a signal to it using workflow stub immediately.
8790
// But just to demonstrate the unit testing of a long running workflow adding a long sleep here.
8891
testEnv.sleep(Duration.ofDays(1));
92+
// This workflow keeps receiving signals until exit is called
8993
workflow.waitForName("World");
94+
workflow.waitForName("Universe");
95+
workflow.exit();
9096
// Calling synchronous getGreeting after workflow has started reconnects to the existing
9197
// workflow and
9298
// blocks until result is available. Note that this behavior assumes that WorkflowOptions are
9399
// not configured
94100
// with WorkflowIdReusePolicy.AllowDuplicate. In that case the call would fail with
95101
// WorkflowExecutionAlreadyStartedException.
96-
String greeting = workflow.getGreeting();
97-
98-
assertEquals("Hello World!", greeting);
102+
List<String> greetings = workflow.getGreetings();
103+
assertEquals(2, greetings.size());
104+
assertEquals("Hello World!", greetings.get(0));
105+
assertEquals("Hello Universe!", greetings.get(1));
99106
}
100107
}

0 commit comments

Comments
 (0)