Skip to content

Commit ea74912

Browse files
committed
Add examples
Signed-off-by: siri-varma <[email protected]>
1 parent ba10525 commit ea74912

File tree

5 files changed

+226
-0
lines changed

5 files changed

+226
-0
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,36 @@
1+
package io.dapr.examples.jobs;
2+
3+
import io.dapr.client.DaprClientBuilder;
4+
import io.dapr.client.DaprPreviewClient;
5+
import io.dapr.client.domain.GetJobRequest;
6+
import io.dapr.client.domain.GetJobResponse;
7+
import io.dapr.client.domain.JobSchedule;
8+
import io.dapr.client.domain.ScheduleJobRequest;
9+
import io.dapr.config.Properties;
10+
import io.dapr.config.Property;
11+
12+
import java.util.Map;
13+
14+
public class DemoJobsClient {
15+
16+
/**
17+
* The main method of this app to register and fetch jobs.
18+
*/
19+
public static void main(String[] args) throws Exception {
20+
Map<Property<?>, String> overrides = Map.of(
21+
Properties.HTTP_PORT, "3500",
22+
Properties.GRPC_PORT, "51439"
23+
);
24+
25+
try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) {
26+
27+
// Schedule a job.
28+
ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1",
29+
JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes());
30+
client.scheduleJob(scheduleJobRequest).block();
31+
32+
// Get a job.
33+
GetJobResponse getJobResponse = client.getJob(new GetJobRequest("dapr-job-1")).block();
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.dapr.examples.jobs;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot application to demonstrate Dapr Jobs callback API.
8+
* <p>
9+
* This application demonstrates how to use Dapr Jobs API with Spring Boot.
10+
* </p>
11+
*/
12+
@SpringBootApplication
13+
public class DemoJobsSpringApplication {
14+
15+
public static void main(String[] args) throws Exception {
16+
SpringApplication.run(DemoJobsSpringApplication.class, args);
17+
}
18+
}
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,123 @@
1+
## Mange 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 JobsAPI
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+
Get into the examples' directory:
79+
```sh
80+
cd examples
81+
```
82+
83+
Use the following command to run this example-
84+
85+
<!-- STEP
86+
name: Run ConfigurationClient example
87+
expected_stdout_lines:
88+
- "== APP == Job Name: dapr-job-1"
89+
- "== APP == Job Payload: Hello World!"
90+
background: true
91+
output_match_mode: substring
92+
sleep: 10
93+
-->
94+
95+
```bash
96+
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
97+
```
98+
99+
```bash
100+
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsClient
101+
```
102+
103+
<!-- END_STEP -->
104+
105+
### Sample output
106+
```
107+
== APP == Job Name: dapr-job-1
108+
== APP == Job Payload: Hello World!
109+
```
110+
### Cleanup
111+
112+
To stop the app, run (or press CTRL+C):
113+
114+
<!-- STEP
115+
name: Cleanup
116+
-->
117+
118+
```bash
119+
dapr stop --app-id myapp
120+
```
121+
122+
<!-- END_STEP -->
123+

0 commit comments

Comments
 (0)