|
| 1 | +## Workflow Schedules |
| 2 | + |
| 3 | +A workflow schedule starts a new instance of a workflow repeatedly, on a cron expression or an |
| 4 | +`@every <duration>` interval. This example creates a schedule, triggers a run by hand, watches the |
| 5 | +schedule until its repeats are used up, counts the runs and deletes the schedule. |
| 6 | + |
| 7 | +### Using the Workflow Schedules API |
| 8 | + |
| 9 | +The `DaprWorkflowClient` exposes these methods: |
| 10 | +* `client.createWorkflowSchedule(...)` to create or overwrite a schedule. |
| 11 | +* `client.getWorkflowSchedule(...)` to read a schedule with its status and last run. |
| 12 | +* `client.listWorkflowSchedules(...)` to page through the app's schedules. |
| 13 | +* `client.triggerWorkflowSchedule(...)` to start a run now. |
| 14 | +* `client.deleteWorkflowSchedule(...)` to delete a schedule. |
| 15 | +* `client.listInstanceIds(null, null, "<name>_")` to list the runs of a schedule. |
| 16 | + |
| 17 | +## Pre-requisites |
| 18 | + |
| 19 | +* Dapr 1.20 or later (dapr master until 1.20.0-rc.1 is released). |
| 20 | +* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/). |
| 21 | +* Java JDK 17 (or greater): |
| 22 | + * [Microsoft JDK 17](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-17) |
| 23 | + * [Oracle JDK 17](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK17) |
| 24 | + * [OpenJDK 17](https://jdk.java.net/17/) |
| 25 | +* [Apache Maven](https://maven.apache.org/install.html) version 3.x. |
| 26 | + |
| 27 | +### Checking out the code |
| 28 | + |
| 29 | +Clone this repository: |
| 30 | + |
| 31 | +```sh |
| 32 | +git clone https://github.com/dapr/java-sdk.git |
| 33 | +cd java-sdk |
| 34 | +``` |
| 35 | + |
| 36 | +Then build the Maven project: |
| 37 | + |
| 38 | +```sh |
| 39 | +# make sure you are in the `java-sdk` directory. |
| 40 | +mvn install |
| 41 | +``` |
| 42 | + |
| 43 | +Then get into the examples directory: |
| 44 | + |
| 45 | +```sh |
| 46 | +cd examples |
| 47 | +``` |
| 48 | + |
| 49 | +### Initialize Dapr |
| 50 | + |
| 51 | +Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized. |
| 52 | + |
| 53 | +### Running the example |
| 54 | + |
| 55 | +<!-- STEP |
| 56 | +name: Run Workflow Schedule Worker |
| 57 | +match_order: none |
| 58 | +output_match_mode: substring |
| 59 | +expected_stdout_lines: |
| 60 | + - "Start workflow runtime" |
| 61 | +background: true |
| 62 | +sleep: 20 |
| 63 | +timeout_seconds: 45 |
| 64 | +--> |
| 65 | + |
| 66 | +Run the worker: |
| 67 | + |
| 68 | +```sh |
| 69 | +dapr run --app-id scheduleworker --resources-path ./components/workflows --dapr-grpc-port 50007 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.schedule.DemoScheduleWorker 50007 |
| 70 | +``` |
| 71 | + |
| 72 | +<!-- END_STEP --> |
| 73 | + |
| 74 | +<!-- STEP |
| 75 | +name: Run Workflow Schedule Client |
| 76 | +match_order: none |
| 77 | +output_match_mode: substring |
| 78 | +expected_stdout_lines: |
| 79 | + - "Created schedule 'daily-report'" |
| 80 | + - "Triggered run" |
| 81 | + - "Schedule state: COMPLETED" |
| 82 | + - "Runs:" |
| 83 | + - "Deleted schedule 'daily-report'" |
| 84 | +timeout_seconds: 90 |
| 85 | +--> |
| 86 | + |
| 87 | +In a separate terminal, run the client. It connects to the worker's sidecar on |
| 88 | +gRPC port 50007, so every run of the schedule executes on the worker: |
| 89 | + |
| 90 | +```sh |
| 91 | +java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.schedule.DemoScheduleClient 50007 |
| 92 | +dapr stop --app-id scheduleworker |
| 93 | +``` |
| 94 | + |
| 95 | +<!-- END_STEP --> |
| 96 | + |
| 97 | +The client creates `daily-report` with three repeats, triggers one run by hand and then polls the |
| 98 | +schedule every two seconds. The triggered run counts towards the repeats, so two scheduled runs |
| 99 | +follow it and the schedule completes. The output shows the schedule state and run count on each |
| 100 | +poll, the number of run instances and the deletion. |
0 commit comments