This example provides the different capabilities provided by Dapr Java SDK for Jobs. For further information about Job APIs please refer to this link
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.
- Dapr CLI.
- Java JDK 11 (or greater):
- Apache Maven version 3.x.
Clone this repository:
git clone https://github.com/dapr/java-sdk.git
cd java-sdk
Then build the Maven project:
# make sure you are in the `java-sdk` directory.
mvn install
Then get into the examples directory:
cd examples
Run dapr init
to initialize Dapr in Self-Hosted Mode if it's not already initialized.
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 for a detailed description of the supported APIs.
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-
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
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.jobs.DemoJobsClient
== APP == Job Name: dapr-job-1
== APP == Job Payload: Hello World!
To stop the app, run (or press CTRL+C):
dapr stop --app-id myapp