Skip to content

Commit 50c6793

Browse files
authored
Merge pull request #78 from uber/signal-client
Add a new sample to start and interact with signal workflow directly via cadence client
2 parents d835352 + c285dca commit 50c6793

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.samples.clientsamples;
19+
20+
import com.uber.cadence.WorkflowExecution;
21+
import com.uber.cadence.client.WorkflowClient;
22+
import com.uber.cadence.client.WorkflowOptions;
23+
import com.uber.cadence.samples.spring.common.Constant;
24+
import com.uber.cadence.samples.spring.models.SampleMessage;
25+
import com.uber.cadence.samples.spring.workflows.SignalWorkflow;
26+
import java.io.File;
27+
import java.io.FileNotFoundException;
28+
import java.io.FileWriter;
29+
import java.io.IOException;
30+
import java.time.Duration;
31+
import java.util.Scanner;
32+
33+
// This class is a placeholder. Run the two binaries below to play with the
34+
// signal workflow sample.
35+
public class SignalSample {}
36+
37+
// This binary starts a signal workflow and store the workflow ID into a local file
38+
// for future usage. It must be run first.
39+
class SignalWorkflowStarter {
40+
public static void main(String[] args) throws IOException {
41+
WorkflowClient workflowClient = CadenceUtil.getWorkflowClient();
42+
WorkflowOptions workflowOptions =
43+
new WorkflowOptions.Builder()
44+
.setExecutionStartToCloseTimeout(Duration.ofSeconds(60))
45+
.setTaskList(Constant.TASK_LIST)
46+
.build();
47+
48+
// start a signal workflow using cadence client
49+
SignalWorkflow signalWorkflow =
50+
workflowClient.newWorkflowStub(SignalWorkflow.class, workflowOptions);
51+
WorkflowExecution execution =
52+
WorkflowClient.start(signalWorkflow::getGreeting, new SampleMessage("Uber"));
53+
String workflowID = execution.getWorkflowId();
54+
System.out.printf("WorkflowID: %s, RunID: %s", workflowID, execution.getRunId());
55+
56+
// store workflow ID for future use into a file. In prod, it may be persisted in a database
57+
File workflowIDFile = new File("workflow_id.txt");
58+
// always delete the file if it exists and create a new one.
59+
workflowIDFile.delete();
60+
workflowIDFile.createNewFile();
61+
FileWriter writer = new FileWriter(workflowIDFile);
62+
writer.write(workflowID);
63+
writer.close();
64+
}
65+
}
66+
67+
// This binary retrieves the stored signal workflow ID from local file created by
68+
// SignalWorkflowStarter and send a signal to this workflow.
69+
class SignalSender {
70+
public static void main(String[] args) throws FileNotFoundException {
71+
// Get stored workflowID
72+
File workflowIDFile = new File("workflow_id.txt");
73+
Scanner scanner = new Scanner(workflowIDFile);
74+
String workflowID = scanner.nextLine();
75+
scanner.close();
76+
77+
// create a new stub using the retrieved workflowID
78+
WorkflowClient workflowClient = CadenceUtil.getWorkflowClient();
79+
// To send a signal, only workflowID is needed.
80+
SignalWorkflow signalWorkflow =
81+
workflowClient.newWorkflowStub(SignalWorkflow.class, workflowID);
82+
// send signal to the workflow
83+
signalWorkflow.waitForGreeting("Hello");
84+
85+
// cancel workflow directly via a signal
86+
// signalWorkflow.cancel();
87+
}
88+
}

0 commit comments

Comments
 (0)