Skip to content

Commit ed9a3fb

Browse files
siri-varmaartur-ciocanucicoyle
authored
Add the Jobs SDK (#1255)
* Add jobs Signed-off-by: sirivarma <[email protected]> * Add validations Signed-off-by: sirivarma <[email protected]> * Add things Signed-off-by: sirivarma <[email protected]> * Add things Signed-off-by: sirivarma <[email protected]> * Remove builder and change to setter Signed-off-by: sirivarma <[email protected]> * Remove module Signed-off-by: sirivarma <[email protected]> * remove jobs Signed-off-by: sirivarma <[email protected]> * change bean name Signed-off-by: sirivarma <[email protected]> * Use latest Dapr release Signed-off-by: artur-ciocanu <[email protected]> * fix things Signed-off-by: siri-varma <[email protected]> * Fix comments and fix tests Signed-off-by: siri-varma <[email protected]> * remove * Signed-off-by: siri-varma <[email protected]> * fix conflicts Signed-off-by: siri-varma <[email protected]> * remove space Signed-off-by: siri-varma <[email protected]> * Update sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprJobsIT.java Co-authored-by: Cassie Coyle <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java Co-authored-by: Cassie Coyle <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]> * Add comment Signed-off-by: siri-varma <[email protected]> * Update DaprPreviewClientGrpcTest.java Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update DaprPreviewClientGrpcTest.java Signed-off-by: Siri Varma Vegiraju <[email protected]> * Fix spaces Signed-off-by: siri-varma <[email protected]> * Fix spaces Signed-off-by: siri-varma <[email protected]> * fixt hings Signed-off-by: siri-varma <[email protected]> * Add examples Signed-off-by: siri-varma <[email protected]> * Cleanup Signed-off-by: siri-varma <[email protected]> * indent to spaces Signed-off-by: siri-varma <[email protected]> * Update README.md Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update README.md Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update DemoJobsClient.java Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update DaprClientImpl.java Signed-off-by: Siri Varma Vegiraju <[email protected]> * Update DaprClientImpl.java Signed-off-by: Siri Varma Vegiraju <[email protected]> --------- Signed-off-by: sirivarma <[email protected]> Signed-off-by: artur-ciocanu <[email protected]> Signed-off-by: siri-varma <[email protected]> Signed-off-by: Siri Varma Vegiraju <[email protected]> Co-authored-by: artur-ciocanu <[email protected]> Co-authored-by: Cassie Coyle <[email protected]>
1 parent 128cfde commit ed9a3fb

23 files changed

+2211
-499
lines changed

.github/workflows/validate.yml

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ jobs:
115115
run: ./mvnw install -q
116116
env:
117117
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
118+
- name: Validate Jobs example
119+
working-directory: ./examples
120+
run: |
121+
mm.py ./src/main/java/io/dapr/examples/jobs/README.md
122+
env:
123+
DOCKER_HOST: ${{steps.setup_docker.outputs.sock}}
118124
- name: Validate invoke http example
119125
working-directory: ./examples
120126
run: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.jobs;
15+
16+
import io.dapr.client.DaprClientBuilder;
17+
import io.dapr.client.DaprPreviewClient;
18+
import io.dapr.client.domain.GetJobRequest;
19+
import io.dapr.client.domain.GetJobResponse;
20+
import io.dapr.client.domain.JobSchedule;
21+
import io.dapr.client.domain.ScheduleJobRequest;
22+
import io.dapr.config.Properties;
23+
import io.dapr.config.Property;
24+
25+
import java.util.Map;
26+
27+
public class DemoJobsClient {
28+
29+
/**
30+
* The main method of this app to register and fetch jobs.
31+
*/
32+
public static void main(String[] args) throws Exception {
33+
Map<Property<?>, String> overrides = Map.of(
34+
Properties.HTTP_PORT, "3500",
35+
Properties.GRPC_PORT, "51439"
36+
);
37+
38+
try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {
39+
40+
// Schedule a job.
41+
System.out.println("**** Scheduling a Job with name dapr-jobs-1 *****");
42+
ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1",
43+
JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes());
44+
client.scheduleJob(scheduleJobRequest).block();
45+
46+
System.out.println("**** Scheduling job dapr-jobs-1 completed *****");
47+
48+
// Get a job.
49+
System.out.println("**** Retrieving a Job with name dapr-jobs-1 *****");
50+
GetJobResponse getJobResponse = client.getJob(new GetJobRequest("dapr-job-1")).block();
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.jobs;
15+
16+
import org.springframework.boot.SpringApplication;
17+
import org.springframework.boot.autoconfigure.SpringBootApplication;
18+
19+
/**
20+
* Spring Boot application to demonstrate Dapr Jobs callback API.
21+
* <p>
22+
* This application demonstrates how to use Dapr Jobs API with Spring Boot.
23+
* </p>
24+
*/
25+
@SpringBootApplication
26+
public class DemoJobsSpringApplication {
27+
28+
public static void main(String[] args) throws Exception {
29+
SpringApplication.run(DemoJobsSpringApplication.class, args);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.jobs;
15+
16+
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.RequestBody;
19+
import org.springframework.web.bind.annotation.RestController;
20+
import reactor.core.publisher.Mono;
21+
22+
/**
23+
* SpringBoot Controller to handle jobs callback.
24+
*/
25+
@RestController
26+
public class JobsController {
27+
28+
/**
29+
* Handles jobs callback from Dapr.
30+
*
31+
* @param jobName name of the job.
32+
* @param payload data from the job if payload exists.
33+
* @return Empty Mono.
34+
*/
35+
@PostMapping("/job/{jobName}")
36+
public Mono<Void> handleJob(@PathVariable("jobName") String jobName,
37+
@RequestBody(required = false) byte[] payload) {
38+
System.out.println("Job Name: " + jobName);
39+
System.out.println("Job Payload: " + new String(payload));
40+
41+
return Mono.empty();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## Manage Dapr Jobs via the Jobs API
2+
3+
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/)
4+
5+
### Using the Jobs API
6+
7+
The Java SDK exposes several methods for this -
8+
* `client.scheduleJob(...)` for scheduling a job.
9+
* `client.getJob(...)` for retrieving a scheduled job.
10+
* `client.deleteJob(...)` for deleting a job.
11+
12+
## Pre-requisites
13+
14+
* [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/).
15+
* Java JDK 11 (or greater):
16+
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11)
17+
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11)
18+
* [OpenJDK 11](https://jdk.java.net/11/)
19+
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.
20+
21+
### Checking out the code
22+
23+
Clone this repository:
24+
25+
```sh
26+
git clone https://github.com/dapr/java-sdk.git
27+
cd java-sdk
28+
```
29+
30+
Then build the Maven project:
31+
32+
```sh
33+
# make sure you are in the `java-sdk` directory.
34+
mvn install
35+
```
36+
37+
Then get into the examples directory:
38+
39+
```sh
40+
cd examples
41+
```
42+
43+
### Initialize Dapr
44+
45+
Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initialized.
46+
47+
### Running the example
48+
49+
This example uses the Java SDK Dapr client in order to **Schedule and Get** Jobs.
50+
`DemoJobsClient.java` is the example class demonstrating these features.
51+
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.
52+
53+
```java
54+
public class DemoJobsClient {
55+
/**
56+
* The main method of this app to register and fetch jobs.
57+
*/
58+
public static void main(String[] args) throws Exception {
59+
Map<Property<?>, String> overrides = Map.of(
60+
Properties.HTTP_PORT, "3500",
61+
Properties.GRPC_PORT, "51439"
62+
);
63+
64+
try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {
65+
66+
// Schedule a job.
67+
ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1",
68+
JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes());
69+
client.scheduleJob(scheduleJobRequest).block();
70+
71+
// Get a job.
72+
GetJobResponse getJobResponse = client.getJob(new GetJobRequest("dapr-job-1")).block();
73+
}
74+
}
75+
}
76+
```
77+
78+
Use the following command to run this example-
79+
80+
<!-- STEP
81+
name: Run Demo Jobs Client example
82+
expected_stdout_lines:
83+
- "== APP == Job Name: dapr-job-1"
84+
- "== APP == Job Payload: Hello World!"
85+
background: true
86+
output_match_mode: substring
87+
sleep: 10
88+
-->
89+
90+
```bash
91+
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
92+
```
93+
94+
```bash
95+
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsClient
96+
```
97+
98+
<!-- END_STEP -->
99+
100+
### Sample output
101+
```
102+
== APP == Job Name: dapr-job-1
103+
== APP == Job Payload: Hello World!
104+
```
105+
### Cleanup
106+
107+
To stop the app, run (or press CTRL+C):
108+
109+
<!-- STEP
110+
name: Cleanup
111+
-->
112+
113+
```bash
114+
dapr stop --app-id myapp
115+
```
116+
117+
<!-- END_STEP -->
118+

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<grpc.version>1.69.0</grpc.version>
1818
<protobuf.version>3.25.5</protobuf.version>
1919
<protocCommand>protoc</protocCommand>
20-
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.14.4/dapr/proto</dapr.proto.baseurl>
20+
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.15.3/dapr/proto</dapr.proto.baseurl>
2121
<dapr.sdk.version>1.15.0-SNAPSHOT</dapr.sdk.version>
2222
<dapr.sdk.alpha.version>0.15.0-SNAPSHOT</dapr.sdk.alpha.version>
2323
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.dapr.it.testcontainers;
22

33
public interface ContainerConstants {
4-
String DAPR_IMAGE_TAG = "daprio/daprd:1.14.1";
54
String TOXIPROXY_IMAGE_TAG = "ghcr.io/shopify/toxiproxy:2.5.0";
5+
String DAPR_RUNTIME_VERSION = "1.15.3";
6+
String DAPR_IMAGE_TAG = "daprio/daprd:" + DAPR_RUNTIME_VERSION;
7+
String DAPR_PLACEMENT_IMAGE_TAG = "daprio/placement:" + DAPR_RUNTIME_VERSION;
8+
String DAPR_SCHEDULER_IMAGE_TAG = "daprio/scheduler:" + DAPR_RUNTIME_VERSION;
69
}

0 commit comments

Comments
 (0)