-
Notifications
You must be signed in to change notification settings - Fork 227
Initial Workflow SDK #857
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
Initial Workflow SDK #857
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ccd29fc
Init for workflows
bderusha 22aacbd
Updating some javadocs and Years.
cf3f60f
Add missing Header
c6e3b5e
respond to PR feedback
bderusha 3cfd8c1
Update workflow example README
bderusha a6cfda9
Address PR feedback
bderusha 1ed1940
fixup deprecated pom.xml variable
bderusha 452a4d9
Updates based on PR feedback
bderusha 9b2881b
Update pom files per feedback
bderusha 522f1ef
Merge branch 'dapr:master' into master
bderusha edecaf6
Add unit testing example
bderusha 42b61ef
Merge branch 'dapr:master' into master
bderusha 234ab44
update pom
bderusha 2fbc805
Fix dependency conflict
bderusha 888b6e5
Merge branch 'dapr:master' into master
bderusha 28898e4
Merge branch 'master' into master
bderusha 1cfae5e
Use Mono and refactor GRPC managed channel builder
bderusha c9f6d1d
Fix example unit tests
bderusha 2aba320
Implement PR feedback
bderusha 6af50b5
PR Feedback: Revert Mono usage + pom.xml changes
bderusha 4a53c4e
pom.xml version fixes
bderusha 904e41e
Remove Mono entirely from workflows
bderusha 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
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,14 @@ | ||
| apiVersion: dapr.io/v1alpha1 | ||
| kind: Component | ||
| metadata: | ||
| name: statestore | ||
| spec: | ||
| type: state.redis | ||
| version: v1 | ||
| metadata: | ||
| - name: redisHost | ||
| value: localhost:6379 | ||
| - name: redisPassword | ||
| value: "" | ||
| - name: actorStateStore | ||
| value: "true" |
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
42 changes: 42 additions & 0 deletions
42
examples/src/main/java/io/dapr/examples/workflows/DemoWorkflow.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,42 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 io.dapr.examples.workflows; | ||
|
|
||
| import com.microsoft.durabletask.TaskCanceledException; | ||
| import io.dapr.workflows.runtime.Workflow; | ||
| import io.dapr.workflows.runtime.WorkflowContext; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| /** | ||
| * Implementation of the DemoWorkflow for the server side. | ||
| */ | ||
| public class DemoWorkflow extends Workflow { | ||
|
|
||
| @Override | ||
| public void run(WorkflowContext ctx) { | ||
| ctx.getLogger().info("Starting Workflow: " + ctx.getName()); | ||
| ctx.getLogger().info("Instance ID: " + ctx.getInstanceId()); | ||
| ctx.getLogger().info("Waiting for event: 'myEvent'..."); | ||
| try { | ||
| ctx.waitForExternalEvent("myEvent", Duration.ofSeconds(10)).await(); | ||
| ctx.getLogger().info("Received!"); | ||
| } catch (TaskCanceledException e) { | ||
| ctx.getLogger().warn("Timed out"); | ||
| ctx.getLogger().warn(e.getMessage()); | ||
| } | ||
| ctx.complete("finished"); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowClient.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,55 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 io.dapr.examples.workflows; | ||
|
|
||
| import io.dapr.workflows.client.DaprWorkflowClient; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * For setup instructions, see the README. | ||
| */ | ||
| public class DemoWorkflowClient { | ||
|
|
||
| /** | ||
| * The main method. | ||
| * @param args Input arguments (unused). | ||
| * @throws InterruptedException If program has been interrupted. | ||
| */ | ||
| public static void main(String[] args) throws InterruptedException { | ||
| DaprWorkflowClient client = new DaprWorkflowClient(); | ||
|
|
||
| try (client) { | ||
| System.out.println("*****"); | ||
|
bderusha marked this conversation as resolved.
|
||
| String instanceId = client.scheduleNewWorkflow(DemoWorkflow.class); | ||
| System.out.printf("Started new workflow instance with random ID: %s%n", instanceId); | ||
|
|
||
| System.out.println("Sleep and allow this workflow instance to timeout..."); | ||
| TimeUnit.SECONDS.sleep(10); | ||
|
|
||
| System.out.println("*****"); | ||
| String instanceToTerminateId = "terminateMe"; | ||
| client.scheduleNewWorkflow(DemoWorkflow.class, null, instanceToTerminateId); | ||
| System.out.printf("Started new workflow instance with specified ID: %s%n", instanceToTerminateId); | ||
|
|
||
| TimeUnit.SECONDS.sleep(5); | ||
| System.out.println("Terminate this workflow instance manually before the timeout is reached"); | ||
| client.terminateWorkflow(instanceToTerminateId, null); | ||
| System.out.println("*****"); | ||
| } | ||
|
|
||
| System.out.println("Exiting DemoWorkflowClient."); | ||
| System.exit(0); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowWorker.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,36 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 io.dapr.examples.workflows; | ||
|
|
||
| import io.dapr.workflows.runtime.WorkflowRuntime; | ||
|
|
||
| /** | ||
| * For setup instructions, see the README. | ||
| */ | ||
| public class DemoWorkflowWorker { | ||
|
|
||
| /** | ||
| * The main method of this app. | ||
| * | ||
| * @param args The port the app will listen on. | ||
| * @throws Exception An Exception. | ||
| */ | ||
| public static void main(String[] args) throws Exception { | ||
| // Register the Workflow with the runtime. | ||
| WorkflowRuntime.getInstance().registerWorkflow(DemoWorkflow.class); | ||
| System.out.println("Start workflow runtime"); | ||
| WorkflowRuntime.getInstance().startAndBlock(); | ||
| System.exit(0); | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
examples/src/main/java/io/dapr/examples/workflows/README.md
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,78 @@ | ||
| # Dapr Workflow Sample | ||
|
|
||
| In this example, we'll use Dapr to test workflow features. | ||
|
|
||
| Visit [the Workflow documentation landing page](https://docs.dapr.io/developing-applications/building-blocks/workflow) for more information. | ||
|
|
||
| This example contains the follow classes: | ||
|
|
||
| * DemoWorkflow: An example of a Dapr Workflow. | ||
| * DemoWorkflowClient: This application will start workflows using Dapr. | ||
| * DemoWorkflowWorker: An application that registers a workflow to the Dapr workflow runtime engine. It also executes the workflow instance. | ||
|
|
||
| ## Pre-requisites | ||
|
|
||
| * [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/). | ||
| * Run `dapr init`. | ||
| * Java JDK 11 (or greater): | ||
| * [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
| * [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
| * [OpenJDK 11](https://jdk.java.net/11/) | ||
| * [Apache Maven](https://maven.apache.org/install.html) version 3.x. | ||
|
|
||
| ### Checking out the code | ||
|
|
||
| Clone this repository: | ||
|
|
||
| ```sh | ||
| git clone https://github.com/dapr/java-sdk.git | ||
| cd java-sdk | ||
| ``` | ||
|
|
||
| Then build the Maven project: | ||
|
|
||
| ```sh | ||
| # make sure you are in the `java-sdk` directory. | ||
| mvn install | ||
| ``` | ||
|
|
||
| Get into the `examples` directory. | ||
| ```sh | ||
| cd examples | ||
| ``` | ||
|
|
||
| ### Running the demo Workflow worker | ||
|
|
||
| The first Java class to consider is `DemoWorkflowWorker`. Its job is to register an implementation of `DemoWorkflow` in the Dapr's workflow runtime engine. In `DemoWorkflowWorker.java` file, you will find the `DemoWorkflowWorker` class and the `main` method. See the code snippet below: | ||
|
|
||
| ```java | ||
| public class DemoWorkflowWorker { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| // Register the Workflow with the runtime. | ||
| WorkflowRuntime.getInstance().registerWorkflow(DemoWorkflow.class); | ||
| System.out.println("Start workflow runtime"); | ||
| WorkflowRuntime.getInstance().startAndBlock(); | ||
| System.exit(0); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This application uses `WorkflowRuntime.getInstance().registerWorkflow()` in order to register `DemoWorkflow` as a Workflow in the Dapr Workflow runtime. | ||
|
|
||
| `WorkflowRuntime.getInstance().start()` method will build and start the engine within the Dapr workflow runtime. | ||
|
|
||
| Now, execute the following script in order to run DemoWorkflowWorker: | ||
| ```sh | ||
| dapr run --app-id demoworkflowworker --resources-path ./components/workflows --dapr-grpc-port 50001 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.DemoWorkflowWorker | ||
| ``` | ||
|
|
||
| ### Running the Workflow client | ||
|
|
||
| The `DemoWorkflowClient` starts instances of workflows that have been registered with Dapr. | ||
|
|
||
| With the DemoWorkflowWorker running, use the follow command to start the workflow with the DemoWorkflowClient: | ||
|
|
||
| ```sh | ||
| java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.DemoWorkflowClient | ||
| ``` |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.