Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ jobs:
fail-fast: false # Keep running if one leg fails.
matrix:
java: [ 17, 21, 25 ]
dapr_ref: [ '' ]
include:
- java: 17
dapr_ref: master
# The dapr master leg tracks a moving target, so it must not block the release legs.
continue-on-error: ${{ matrix.dapr_ref != '' }}
env:
GOVER: "1.20"
GOVER: "1.26"
GOOS: linux
GOARCH: amd64
GOPROXY: https://proxy.golang.org
Expand All @@ -41,7 +47,7 @@ jobs:
DAPR_RUNTIME_VER: 1.19.1
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.19.0-rc.1/install/install.sh
DAPR_CLI_REF:
DAPR_REF:
DAPR_REF: ${{ matrix.dapr_ref }}
steps:
- uses: actions/checkout@v7
- name: Set up OpenJDK ${{ env.JDK_VER }}
Expand Down Expand Up @@ -103,7 +109,13 @@ jobs:
run: |
docker stop dapr_placement
cd dapr
./dist/linux_amd64/release/placement &
./dist/linux_amd64/release/placement --healthz-port 58080 --metrics-port 59090 &
- name: Override scheduler service.
if: env.DAPR_REF != ''
run: |
docker stop dapr_scheduler
cd dapr
./dist/linux_amd64/release/scheduler --etcd-data-dir /tmp/dapr-scheduler --override-broadcast-host-port=localhost:50006 --etcd-client-listen-address=0.0.0.0 --healthz-port 58081 --metrics-port 59091 &
- name: Install utilities dependencies
run: |
echo "PATH=$PATH:$HOME/.local/bin" >> $GITHUB_ENV
Expand Down Expand Up @@ -143,3 +155,8 @@ jobs:
(cd examples && mm.py ./src/main/java/io/dapr/examples/actors/README.md)
(cd examples && mm.py ./src/main/java/io/dapr/examples/querystate/README.md)
(cd examples && mm.py ./src/main/java/io/dapr/examples/pubsub/stream/README.md)
- name: Validate examples that need dapr master
if: env.DAPR_REF != ''
run: |
set -e
(cd examples && mm.py ./src/main/java/io/dapr/examples/workflows/schedule/README.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2026 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.schedule;

import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowScheduleOptions;
import io.dapr.workflows.client.WorkflowScheduleState;
import io.dapr.workflows.client.WorkflowScheduleStatus;

public class DemoScheduleClient {
private static final String SCHEDULE_NAME = "daily-report";

/**
* The main method to start the client.
*
* @param args Input arguments (unused).
* @throws InterruptedException if interrupted while waiting for the schedule to complete.
*/
public static void main(String[] args) throws InterruptedException {
try (DaprWorkflowClient client = new DaprWorkflowClient(PropertyUtils.getProperties(args))) {
client.createWorkflowSchedule(SCHEDULE_NAME, DemoScheduleWorkflow.class, "@every 5s",
new WorkflowScheduleOptions().setInput("sales").setRepeats(3).setOverwrite(true));
System.out.printf("Created schedule '%s'%n", SCHEDULE_NAME);

// A triggered run counts towards the repeats, so two scheduled runs follow it.
client.triggerWorkflowSchedule(SCHEDULE_NAME);
System.out.println("Triggered run");

WorkflowScheduleStatus status;
do {
Thread.sleep(2000);
status = client.getWorkflowSchedule(SCHEDULE_NAME).getStatus();
System.out.printf("Schedule state: %s runs=%d%n", status.getState(), status.getRunCount());
} while (status.getState() == WorkflowScheduleState.ACTIVE);

// Every run is a workflow instance whose ID starts with the schedule name.
int runs = client.listInstanceIds(null, null, SCHEDULE_NAME + "_").getInstanceIds().size();
System.out.printf("Runs: %d%n", runs);

client.deleteWorkflowSchedule(SCHEDULE_NAME);
System.out.printf("Deleted schedule '%s'%n", SCHEDULE_NAME);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2026 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.schedule;

import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.workflows.runtime.WorkflowRuntime;
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;

public class DemoScheduleWorker {
/**
* 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 {
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder(PropertyUtils.getProperties(args))
.registerWorkflow(DemoScheduleWorkflow.class);

WorkflowRuntime runtime = builder.build();
System.out.println("Start workflow runtime");
runtime.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2026 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.schedule;

import io.dapr.workflows.Workflow;
import io.dapr.workflows.WorkflowStub;

public class DemoScheduleWorkflow implements Workflow {
@Override
public WorkflowStub create() {
return ctx -> ctx.complete("Report '" + ctx.getInput(String.class) + "' by " + ctx.getInstanceId());
}
}
100 changes: 100 additions & 0 deletions examples/src/main/java/io/dapr/examples/workflows/schedule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## Workflow Schedules

A workflow schedule starts a new instance of a workflow repeatedly, on a cron expression or an
`@every <duration>` interval. This example creates a schedule, triggers a run by hand, watches the
schedule until its repeats are used up, counts the runs and deletes the schedule.

### Using the Workflow Schedules API

The `DaprWorkflowClient` exposes these methods:
* `client.createWorkflowSchedule(...)` to create or overwrite a schedule.
* `client.getWorkflowSchedule(...)` to read a schedule with its status and last run.
* `client.listWorkflowSchedules(...)` to page through the app's schedules.
* `client.triggerWorkflowSchedule(...)` to start a run now.
* `client.deleteWorkflowSchedule(...)` to delete a schedule.
* `client.listInstanceIds(null, null, "<name>_")` to list the runs of a schedule.

## Pre-requisites

* Dapr 1.20 or later (dapr master until 1.20.0-rc.1 is released).
* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/).
* Java JDK 17 (or greater):
* [Microsoft JDK 17](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-17)
* [Oracle JDK 17](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK17)
* [OpenJDK 17](https://jdk.java.net/17/)
* [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
```

Then get into the examples directory:

```sh
cd examples
```

### Initialize Dapr

Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized.

### Running the example

<!-- STEP
name: Run Workflow Schedule Worker
match_order: none
output_match_mode: substring
expected_stdout_lines:
- "Start workflow runtime"
background: true
sleep: 20
timeout_seconds: 45
-->

Run the worker:

```sh
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
```

<!-- END_STEP -->

<!-- STEP
name: Run Workflow Schedule Client
match_order: none
output_match_mode: substring
expected_stdout_lines:
- "Created schedule 'daily-report'"
- "Triggered run"
- "Schedule state: COMPLETED"
- "Runs:"
- "Deleted schedule 'daily-report'"
timeout_seconds: 90
-->

In a separate terminal, run the client. It connects to the worker's sidecar on
gRPC port 50007, so every run of the schedule executes on the worker:

```sh
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.schedule.DemoScheduleClient 50007
dapr stop --app-id scheduleworker
```

<!-- END_STEP -->

The client creates `daily-report` with three repeats, triggers one run by hand and then polls the
schedule every two seconds. The triggered run counts towards the repeats, so two scheduled runs
follow it and the schedule completes. The output shows the schedule state and run count on each
poll, the number of run instances and the deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<grpc.version>1.84.0</grpc.version>
<protobuf.version>3.25.5</protobuf.version>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.19.0/dapr/proto</dapr.proto.baseurl>
<durabletask.proto.baseurl>https://raw.githubusercontent.com/dapr/durabletask-protobuf/v0.6.0/protos</durabletask.proto.baseurl>
<durabletask.proto.baseurl>https://raw.githubusercontent.com/dapr/durabletask-protobuf/v0.7.0/protos</durabletask.proto.baseurl>
<dapr.sdk.version>1.20.0-SNAPSHOT</dapr.sdk.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
<maven-dependency-plugin.version>3.8.1</maven-dependency-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2026 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.it.testcontainers.workflows;

import io.dapr.workflows.Workflow;
import io.dapr.workflows.WorkflowStub;

public class ScheduleTestWorkflow implements Workflow {

@Override
public WorkflowStub create() {
return ctx -> ctx.complete(ctx.getInput(String.class));
}
}
Loading
Loading