|
| 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 Demo Jobs Client 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