-
Notifications
You must be signed in to change notification settings - Fork 64
Add a new sample to start and interact with signal workflow directly via cadence client #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/com/uber/cadence/samples/clientsamples/SignalSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.uber.cadence.samples.clientsamples; | ||
|
||
import com.uber.cadence.WorkflowExecution; | ||
import com.uber.cadence.client.WorkflowClient; | ||
import com.uber.cadence.client.WorkflowOptions; | ||
import com.uber.cadence.samples.spring.common.Constant; | ||
import com.uber.cadence.samples.spring.models.SampleMessage; | ||
import com.uber.cadence.samples.spring.workflows.SignalWorkflow; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.Scanner; | ||
|
||
// This class is a placeholder. Run the two binaries below to play with the | ||
// signal workflow sample. | ||
public class SignalSample {} | ||
|
||
// This binary starts a signal workflow and store the workflow ID into a local file | ||
// for future usage. It must be run first. | ||
class SignalWorkflowStarter { | ||
public static void main(String[] args) throws IOException { | ||
WorkflowClient workflowClient = CadenceUtil.getWorkflowClient(); | ||
WorkflowOptions workflowOptions = | ||
new WorkflowOptions.Builder() | ||
.setExecutionStartToCloseTimeout(Duration.ofSeconds(60)) | ||
.setTaskList(Constant.TASK_LIST) | ||
.build(); | ||
|
||
// start a signal workflow using cadence client | ||
SignalWorkflow signalWorkflow = | ||
workflowClient.newWorkflowStub(SignalWorkflow.class, workflowOptions); | ||
WorkflowExecution execution = | ||
WorkflowClient.start(signalWorkflow::getGreeting, new SampleMessage("Uber")); | ||
String workflowID = execution.getWorkflowId(); | ||
System.out.printf("WorkflowID: %s, RunID: %s", workflowID, execution.getRunId()); | ||
|
||
// store workflow ID for future use into a file. In prod, it may be persisted in a database | ||
File workflowIDFile = new File("workflow_id.txt"); | ||
// always delete the file if it exists and create a new one. | ||
workflowIDFile.delete(); | ||
workflowIDFile.createNewFile(); | ||
FileWriter writer = new FileWriter(workflowIDFile); | ||
writer.write(workflowID); | ||
writer.close(); | ||
Comment on lines
+62
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's check the errors in case there's permission issues etc. to create/read/write/delete file |
||
} | ||
} | ||
|
||
// This binary retrieves the stored signal workflow ID from local file created by | ||
// SignalWorkflowStarter and send a signal to this workflow. | ||
class SignalSender { | ||
public static void main(String[] args) throws FileNotFoundException { | ||
// Get stored workflowID | ||
File workflowIDFile = new File("workflow_id.txt"); | ||
Scanner scanner = new Scanner(workflowIDFile); | ||
String workflowID = scanner.nextLine(); | ||
scanner.close(); | ||
|
||
// create a new stub using the retrieved workflowID | ||
WorkflowClient workflowClient = CadenceUtil.getWorkflowClient(); | ||
// To send a signal, only workflowID is needed. | ||
SignalWorkflow signalWorkflow = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add some logs "signallng workflow %s" |
||
workflowClient.newWorkflowStub(SignalWorkflow.class, workflowID); | ||
// send signal to the workflow | ||
signalWorkflow.waitForGreeting("Hello"); | ||
|
||
// cancel workflow directly via a signal | ||
// signalWorkflow.cancel(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there going to be a corresponding README to show how to run this scenario?