Skip to content

Add the Jobs SDK #1255

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 34 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ea1e17f
Add jobs
siri-varma Mar 9, 2025
6ff418d
Add validations
siri-varma Mar 9, 2025
540607f
Add things
siri-varma Mar 9, 2025
50ee84d
Add things
siri-varma Mar 9, 2025
a8a340f
Remove builder and change to setter
siri-varma Mar 14, 2025
066a0c3
Remove module
siri-varma Mar 18, 2025
de1db0a
remove jobs
siri-varma Mar 24, 2025
ebea4c7
change bean name
siri-varma Mar 26, 2025
87c07ca
Use latest Dapr release
artur-ciocanu Mar 28, 2025
ab3a75d
fix things
siri-varma Apr 6, 2025
1f767ef
Fix comments and fix tests
siri-varma Apr 6, 2025
186ed35
remove *
siri-varma Apr 8, 2025
5191770
fix conflicts
siri-varma Apr 8, 2025
d193586
remove space
siri-varma Apr 9, 2025
ac19747
Update sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprJobsIT.java
siri-varma Apr 10, 2025
9e022c5
Update testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprC…
siri-varma Apr 10, 2025
4678e82
Add comment
siri-varma Apr 10, 2025
9145ca2
Merge branch 'users/svegiraju/cron-2' of https://github.com/siri-varm…
siri-varma Apr 10, 2025
9d2daa1
Update DaprPreviewClientGrpcTest.java
siri-varma Apr 10, 2025
afeb8c1
Update DaprPreviewClientGrpcTest.java
siri-varma Apr 10, 2025
4c52d12
Fix spaces
siri-varma Apr 10, 2025
1cd76a0
Merge branch 'users/svegiraju/cron-2' of https://github.com/siri-varm…
siri-varma Apr 10, 2025
6c833bc
Fix spaces
siri-varma Apr 10, 2025
ba10525
fixt hings
siri-varma Apr 10, 2025
17429d6
Add examples
siri-varma Apr 12, 2025
25b236f
Cleanup
siri-varma Apr 12, 2025
68568c5
indent to spaces
siri-varma Apr 12, 2025
9504bfc
Update README.md
siri-varma Apr 12, 2025
267c67a
Merge branch 'master' into users/svegiraju/cron-2
artur-ciocanu Apr 14, 2025
65fb5d5
Merge branch 'master' into users/svegiraju/cron-2
siri-varma Apr 15, 2025
09354ed
Update README.md
siri-varma Apr 16, 2025
de4da72
Update DemoJobsClient.java
siri-varma Apr 16, 2025
913f879
Update DaprClientImpl.java
siri-varma Apr 16, 2025
d18bad6
Update DaprClientImpl.java
siri-varma Apr 16, 2025
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
6 changes: 6 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ jobs:
run: ./mvnw install -q
env:
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
- name: Validate Jobs example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/jobs/README.md
env:
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
- name: Validate invoke http example
working-directory: ./examples
run: |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021 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.jobs;

import io.dapr.client.DaprClientBuilder;
import io.dapr.client.DaprPreviewClient;
import io.dapr.client.domain.GetJobRequest;
import io.dapr.client.domain.GetJobResponse;
import io.dapr.client.domain.JobSchedule;
import io.dapr.client.domain.ScheduleJobRequest;
import io.dapr.config.Properties;
import io.dapr.config.Property;

import java.util.Map;

public class DemoJobsClient {

/**
* The main method of this app to register and fetch jobs.
*/
public static void main(String[] args) throws Exception {
Map<Property<?>, String> overrides = Map.of(
Properties.HTTP_PORT, "3500",
Properties.GRPC_PORT, "51439"
);

try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {

// Schedule a job.
System.out.println("**** Scheduling a Job with name dapr-jobs-1 *****");
ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1",
JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes());
client.scheduleJob(scheduleJobRequest).block();

System.out.println("**** Scheduling job dapr-jobs-1 completed *****");

// Get a job.
System.out.println("**** Retrieving a Job with name dapr-jobs-1 *****");
GetJobResponse getJobResponse = client.getJob(new GetJobRequest("dapr-job-1")).block();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2021 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.jobs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Spring Boot application to demonstrate Dapr Jobs callback API.
* <p>
* This application demonstrates how to use Dapr Jobs API with Spring Boot.
* </p>
*/
@SpringBootApplication
public class DemoJobsSpringApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(DemoJobsSpringApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2021 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.jobs;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
* SpringBoot Controller to handle jobs callback.
*/
@RestController
public class JobsController {

/**
* Handles jobs callback from Dapr.
*
* @param jobName name of the job.
* @param payload data from the job if payload exists.
* @return Empty Mono.
*/
@PostMapping("/job/{jobName}")
public Mono<Void> handleJob(@PathVariable("jobName") String jobName,
@RequestBody(required = false) byte[] payload) {
System.out.println("Job Name: " + jobName);
System.out.println("Job Payload: " + new String(payload));

return Mono.empty();
}
}
118 changes: 118 additions & 0 deletions examples/src/main/java/io/dapr/examples/jobs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
## Manage Dapr Jobs via the Jobs API

This example provides the different capabilities provided by Dapr Java SDK for Jobs. For further information about Job APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/jobs/jobs-overview/)

### Using the Jobs API

The Java SDK exposes several methods for this -
* `client.scheduleJob(...)` for scheduling a job.
* `client.getJob(...)` for retrieving a scheduled job.
* `client.deleteJob(...)` for deleting a job.

## Pre-requisites

* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/).
* 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
```

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

This example uses the Java SDK Dapr client in order to **Schedule and Get** Jobs.
`DemoJobsClient.java` is the example class demonstrating these features.
Kindly check [DaprPreviewClient.java](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java) for a detailed description of the supported APIs.

```java
public class DemoJobsClient {
/**
* The main method of this app to register and fetch jobs.
*/
public static void main(String[] args) throws Exception {
Map<Property<?>, String> overrides = Map.of(
Properties.HTTP_PORT, "3500",
Properties.GRPC_PORT, "51439"
);

try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {

// Schedule a job.
ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1",
JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes());
client.scheduleJob(scheduleJobRequest).block();

// Get a job.
GetJobResponse getJobResponse = client.getJob(new GetJobRequest("dapr-job-1")).block();
}
}
}
```

Use the following command to run this example-

<!-- STEP
name: Run Demo Jobs Client example
expected_stdout_lines:
- "== APP == Job Name: dapr-job-1"
- "== APP == Job Payload: Hello World!"
background: true
output_match_mode: substring
sleep: 10
-->

```bash
dapr run --resources-path ./components/configuration --app-id myapp --app-port 8080 --dapr-http-port 3500 --dapr-grpc-port 51439 --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsSpringApplication
```

```bash
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsClient
```

<!-- END_STEP -->

### Sample output
```
== APP == Job Name: dapr-job-1
== APP == Job Payload: Hello World!
```
### Cleanup

To stop the app, run (or press CTRL+C):

<!-- STEP
name: Cleanup
-->

```bash
dapr stop --app-id myapp
```

<!-- END_STEP -->

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.69.0</grpc.version>
<protobuf.version>3.25.5</protobuf.version>
<protocCommand>protoc</protocCommand>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.14.4/dapr/proto</dapr.proto.baseurl>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.15.3/dapr/proto</dapr.proto.baseurl>
<dapr.sdk.version>1.15.0-SNAPSHOT</dapr.sdk.version>
<dapr.sdk.alpha.version>0.15.0-SNAPSHOT</dapr.sdk.alpha.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.dapr.it.testcontainers;

public interface ContainerConstants {
String DAPR_IMAGE_TAG = "daprio/daprd:1.14.1";
String TOXIPROXY_IMAGE_TAG = "ghcr.io/shopify/toxiproxy:2.5.0";
String DAPR_RUNTIME_VERSION = "1.15.3";
String DAPR_IMAGE_TAG = "daprio/daprd:" + DAPR_RUNTIME_VERSION;
String DAPR_PLACEMENT_IMAGE_TAG = "daprio/placement:" + DAPR_RUNTIME_VERSION;
String DAPR_SCHEDULER_IMAGE_TAG = "daprio/scheduler:" + DAPR_RUNTIME_VERSION;
}
Loading
Loading