-
Notifications
You must be signed in to change notification settings - Fork 221
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
Add the Jobs SDK #1255
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
ea1e17f
Add jobs
siri-varma 6ff418d
Add validations
siri-varma 540607f
Add things
siri-varma 50ee84d
Add things
siri-varma a8a340f
Remove builder and change to setter
siri-varma 066a0c3
Remove module
siri-varma de1db0a
remove jobs
siri-varma ebea4c7
change bean name
siri-varma 87c07ca
Use latest Dapr release
artur-ciocanu ab3a75d
fix things
siri-varma 1f767ef
Fix comments and fix tests
siri-varma 186ed35
remove *
siri-varma 5191770
fix conflicts
siri-varma d193586
remove space
siri-varma ac19747
Update sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprJobsIT.java
siri-varma 9e022c5
Update testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprC…
siri-varma 4678e82
Add comment
siri-varma 9145ca2
Merge branch 'users/svegiraju/cron-2' of https://github.com/siri-varm…
siri-varma 9d2daa1
Update DaprPreviewClientGrpcTest.java
siri-varma afeb8c1
Update DaprPreviewClientGrpcTest.java
siri-varma 4c52d12
Fix spaces
siri-varma 1cd76a0
Merge branch 'users/svegiraju/cron-2' of https://github.com/siri-varm…
siri-varma 6c833bc
Fix spaces
siri-varma ba10525
fixt hings
siri-varma 17429d6
Add examples
siri-varma 25b236f
Cleanup
siri-varma 68568c5
indent to spaces
siri-varma 9504bfc
Update README.md
siri-varma 267c67a
Merge branch 'master' into users/svegiraju/cron-2
artur-ciocanu 65fb5d5
Merge branch 'master' into users/svegiraju/cron-2
siri-varma 09354ed
Update README.md
siri-varma de4da72
Update DemoJobsClient.java
siri-varma 913f879
Update DaprClientImpl.java
siri-varma d18bad6
Update DaprClientImpl.java
siri-varma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/src/main/java/io/dapr/examples/jobs/DemoJobsSpringApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/src/main/java/io/dapr/examples/jobs/JobsController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
sdk-tests/src/test/java/io/dapr/it/testcontainers/ContainerConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.