|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "How to: Author and manage Dapr Jobs in the Java SDK" |
| 4 | +linkTitle: "How to: Author and manage Jobs" |
| 5 | +weight: 20000 |
| 6 | +description: How to get up and running with Jobs using the Dapr Java SDK |
| 7 | +--- |
| 8 | + |
| 9 | +As part of this demonstration we will schedule a Dapr Job. The scheduled job will trigger an endpoint registered in the |
| 10 | +same app. With the [provided jobs example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/jobs), you will: |
| 11 | + |
| 12 | +- Schedule a Job [Job scheduling example](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/jobs/DemoJobsClient.java) |
| 13 | +- Register an endpoint for the dapr sidecar to invoke at trigger time [Endpoint Registration](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/jobs/DemoJobsSpringApplication.java) |
| 14 | + |
| 15 | +This example uses the default configuration from `dapr init` in [self-hosted mode](https://github.com/dapr/cli#install-dapr-on-your-local-machine-self-hosted). |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started). |
| 20 | +- Java JDK 11 (or greater): |
| 21 | + - [Oracle JDK](https://www.oracle.com/java/technologies/downloads), or |
| 22 | + - OpenJDK |
| 23 | +- [Apache Maven](https://maven.apache.org/install.html), version 3.x. |
| 24 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop) |
| 25 | + |
| 26 | +## Set up the environment |
| 27 | + |
| 28 | +Clone the [Java SDK repo](https://github.com/dapr/java-sdk) and navigate into it. |
| 29 | + |
| 30 | +```bash |
| 31 | +git clone https://github.com/dapr/java-sdk.git |
| 32 | +cd java-sdk |
| 33 | +``` |
| 34 | + |
| 35 | +Run the following command to install the requirements for running the jobs example with the Dapr Java SDK. |
| 36 | + |
| 37 | +```bash |
| 38 | +mvn clean install -DskipTests |
| 39 | +``` |
| 40 | + |
| 41 | +From the Java SDK root directory, navigate to the Dapr Jobs example. |
| 42 | + |
| 43 | +```bash |
| 44 | +cd examples |
| 45 | +``` |
| 46 | + |
| 47 | +Run the Dapr sidecar. |
| 48 | + |
| 49 | +```sh |
| 50 | +dapr run --app-id jobsapp --dapr-grpc-port 51439 --dapr-http-port 3500 --app-port 8080 |
| 51 | +``` |
| 52 | + |
| 53 | +> Now, Dapr is listening for HTTP requests at `http://localhost:3500` and internal Jobs gRPC requests at `http://localhost:51439`. |
| 54 | +
|
| 55 | +## Schedule and Get a job |
| 56 | + |
| 57 | +In the `DemoJobsClient` there are steps to schedule a job. Calling `scheduleJob` using the `DaprPreviewClient` |
| 58 | +will schedule a job with the Dapr Runtime. |
| 59 | + |
| 60 | +```java |
| 61 | +public class DemoJobsClient { |
| 62 | + |
| 63 | + /** |
| 64 | + * The main method of this app to schedule and get jobs. |
| 65 | + */ |
| 66 | + public static void main(String[] args) throws Exception { |
| 67 | + try (DaprPreviewClient client = new DaprClientBuilder().withPropertyOverrides(overrides).buildPreviewClient()) { |
| 68 | + |
| 69 | + // Schedule a job. |
| 70 | + System.out.println("**** Scheduling a Job with name dapr-jobs-1 *****"); |
| 71 | + ScheduleJobRequest scheduleJobRequest = new ScheduleJobRequest("dapr-job-1", |
| 72 | + JobSchedule.fromString("* * * * * *")).setData("Hello World!".getBytes()); |
| 73 | + client.scheduleJob(scheduleJobRequest).block(); |
| 74 | + |
| 75 | + System.out.println("**** Scheduling job dapr-jobs-1 completed *****"); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Call `getJob` to retrieve the job details that were previously created and scheduled. |
| 82 | +``` |
| 83 | +client.getJob(new GetJobRequest("dapr-job-1")).block() |
| 84 | +``` |
| 85 | + |
| 86 | +Run the `DemoJobsClient` with the following command. |
| 87 | + |
| 88 | +```sh |
| 89 | +java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsClient |
| 90 | +``` |
| 91 | + |
| 92 | +### Sample output |
| 93 | +``` |
| 94 | +**** Scheduling a Job with name dapr-jobs-1 ***** |
| 95 | +**** Scheduling job dapr-jobs-1 completed ***** |
| 96 | +**** Retrieving a Job with name dapr-jobs-1 ***** |
| 97 | +``` |
| 98 | + |
| 99 | +## Set up an endpoint to be invoked when the job is triggered |
| 100 | + |
| 101 | +The `DemoJobsSpringApplication` class starts a Spring Boot application that registers the endpoints specified in the `JobsController` |
| 102 | +This endpoint acts like a callback for the scheduled job requests. |
| 103 | + |
| 104 | +```java |
| 105 | +@RestController |
| 106 | +public class JobsController { |
| 107 | + |
| 108 | + /** |
| 109 | + * Handles jobs callback from Dapr. |
| 110 | + * |
| 111 | + * @param jobName name of the job. |
| 112 | + * @param payload data from the job if payload exists. |
| 113 | + * @return Empty Mono. |
| 114 | + */ |
| 115 | + @PostMapping("/job/{jobName}") |
| 116 | + public Mono<Void> handleJob(@PathVariable("jobName") String jobName, |
| 117 | + @RequestBody(required = false) byte[] payload) { |
| 118 | + System.out.println("Job Name: " + jobName); |
| 119 | + System.out.println("Job Payload: " + new String(payload)); |
| 120 | + |
| 121 | + return Mono.empty(); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Parameters: |
| 127 | + |
| 128 | +* `jobName`: The name of the triggered job. |
| 129 | +* `payload`: Optional payload data associated with the job (as a byte array). |
| 130 | + |
| 131 | +Run the Spring Boot application with the following command. |
| 132 | + |
| 133 | +```sh |
| 134 | +java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsSpringApplication |
| 135 | +``` |
| 136 | + |
| 137 | +### Sample output |
| 138 | +``` |
| 139 | +Job Name: dapr-job-1 |
| 140 | +Job Payload: Hello World! |
| 141 | +``` |
| 142 | + |
| 143 | +## Delete a scheduled job |
| 144 | + |
| 145 | +```java |
| 146 | +public class DemoJobsClient { |
| 147 | + |
| 148 | + /** |
| 149 | + * The main method of this app deletes a job that was previously scheduled. |
| 150 | + */ |
| 151 | + public static void main(String[] args) throws Exception { |
| 152 | + try (DaprPreviewClient client = new DaprClientBuilder().buildPreviewClient()) { |
| 153 | + |
| 154 | + // Delete a job. |
| 155 | + System.out.println("**** Delete a Job with name dapr-jobs-1 *****"); |
| 156 | + client.deleteJob(new DeleteJobRequest("dapr-job-1")).block(); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Next steps |
| 163 | +- [Learn more about Jobs]({{< ref jobs-overview.md >}}) |
| 164 | +- [Jobs API reference]({{< ref jobs_api.md >}}) |
0 commit comments