|
| 1 | +--- |
| 2 | +title: Schedule and broadcast jobs (Java) |
| 3 | +titleSuffix: Azure IoT Hub |
| 4 | +description: How to use the Azure IoT SDK for Java to create backend service application code for job scheduling. |
| 5 | +author: kgremban |
| 6 | +ms.author: kgremban |
| 7 | +ms.service: azure-iot-hub |
| 8 | +ms.devlang: java |
| 9 | +ms.topic: include |
| 10 | +ms.date: 1/7/2025 |
| 11 | +ms.custom: mqtt, devx-track-java, devx-track-extended-java |
| 12 | +--- |
| 13 | + |
| 14 | + * Requires [Java SE Development Kit 8](/azure/developer/java/fundamentals/). Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8. |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +This article describes how to use the [Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java) to create backend service application code for job scheduling. |
| 19 | + |
| 20 | +The [JobClient](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient) class contains methods that services can use to schedule jobs. |
| 21 | + |
| 22 | +### Service import statements |
| 23 | + |
| 24 | +Use the following service import statements to access the Azure IoT SDK for Java. |
| 25 | + |
| 26 | +```java |
| 27 | +import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice; |
| 28 | +import com.microsoft.azure.sdk.iot.service.devicetwin.Pair; |
| 29 | +import com.microsoft.azure.sdk.iot.service.devicetwin.Query; |
| 30 | +import com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery; |
| 31 | +import com.microsoft.azure.sdk.iot.service.jobs.JobClient; |
| 32 | +import com.microsoft.azure.sdk.iot.service.jobs.JobResult; |
| 33 | +import com.microsoft.azure.sdk.iot.service.jobs.JobStatus; |
| 34 | + |
| 35 | +import java.util.Date; |
| 36 | +import java.time.Instant; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.UUID; |
| 40 | +``` |
| 41 | + |
| 42 | +### Connect to the IoT Hub |
| 43 | + |
| 44 | +You can connect a backend service to IoT Hub using the following methods: |
| 45 | + |
| 46 | +* Shared access policy |
| 47 | +* Microsoft Entra |
| 48 | + |
| 49 | +[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)] |
| 50 | + |
| 51 | +#### Connect using a shared access policy |
| 52 | + |
| 53 | +Use a [JobClient](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient) constructor to create the connection to IoT hub. The `JobClient` object handles the communication with your IoT hub. |
| 54 | + |
| 55 | +In this article, you create a back-end service that schedules a job to invoke a direct method on a device, schedules a job to update the device twin, and monitors the progress of each job. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions. |
| 56 | + |
| 57 | +For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas). |
| 58 | + |
| 59 | +For example: |
| 60 | + |
| 61 | +```java |
| 62 | +public static final String iotHubConnectionString = "{Shared access policy connection string}"; |
| 63 | +public static final String deviceId = "myDeviceId"; |
| 64 | + |
| 65 | +JobClient jobClient = new JobClient(iotHubConnectionString); |
| 66 | +``` |
| 67 | + |
| 68 | +#### Connect using Microsoft Entra |
| 69 | + |
| 70 | +[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-java](iot-hub-howto-connect-service-iothub-entra-java.md)] |
| 71 | + |
| 72 | +### Create a device method update job |
| 73 | + |
| 74 | +Use [scheduleDeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-scheduledevicemethod(java-lang-string-java-lang-string-java-lang-string-java-lang-long-java-lang-long-java-lang-object-java-util-date-long)) to to run a device method on one or multiple devices. |
| 75 | + |
| 76 | +This example schedules a device method call job for a specific job Id. |
| 77 | + |
| 78 | +```java |
| 79 | +private static JobResult scheduleJobCallDirectMethod(JobClient jobClient, String jobId) { |
| 80 | + // Schedule a job now to call the lockDoor direct method |
| 81 | + // against a single device. Response and connection |
| 82 | + // timeouts are set to 5 seconds. |
| 83 | + System.out.println("Schedule job " + jobId + " for device " + deviceId); |
| 84 | + try { |
| 85 | + JobResult jobResult = jobClient.scheduleDeviceMethod(jobId, |
| 86 | + "deviceId='" + deviceId + "'", |
| 87 | + "lockDoor", |
| 88 | + 5L, 5L, null, |
| 89 | + new Date(), |
| 90 | + maxExecutionTimeInSeconds); |
| 91 | + return jobResult; |
| 92 | + } catch (Exception e) { |
| 93 | + System.out.println("Exception scheduling direct method job: " + jobId); |
| 94 | + System.out.println(e.getMessage()); |
| 95 | + return null; |
| 96 | + } |
| 97 | +}; |
| 98 | +``` |
| 99 | + |
| 100 | +### Schedule a device twin update job |
| 101 | + |
| 102 | +Use [scheduleUpdateTwin](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-scheduleupdatetwin(java-lang-string-java-lang-string-com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice-java-util-date-long)) to create a new job to run a device twin update on one or multiple devices. |
| 103 | + |
| 104 | +This example schedules a device twin update job for a specific job Id. |
| 105 | + |
| 106 | +```java |
| 107 | +private static JobResult scheduleJobSetDesiredProperties(JobClient jobClient, String jobId) { |
| 108 | + DeviceTwinDevice twin = new DeviceTwinDevice(deviceId); |
| 109 | + Set<Pair> desiredProperties = new HashSet<Pair>(); |
| 110 | + desiredProperties.add(new Pair("Building", 43)); |
| 111 | + desiredProperties.add(new Pair("Floor", 3)); |
| 112 | + twin.setDesiredProperties(desiredProperties); |
| 113 | + // Optimistic concurrency control |
| 114 | + twin.setETag("*"); |
| 115 | + |
| 116 | + // Schedule the update twin job to run now |
| 117 | + // against a single device |
| 118 | + System.out.println("Schedule job " + jobId + " for device " + deviceId); |
| 119 | + try { |
| 120 | + JobResult jobResult = jobClient.scheduleUpdateTwin(jobId, |
| 121 | + "deviceId='" + deviceId + "'", |
| 122 | + twin, |
| 123 | + new Date(), |
| 124 | + maxExecutionTimeInSeconds); |
| 125 | + return jobResult; |
| 126 | + } catch (Exception e) { |
| 127 | + System.out.println("Exception scheduling desired properties job: " + jobId); |
| 128 | + System.out.println(e.getMessage()); |
| 129 | + return null; |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Monitor a job |
| 135 | + |
| 136 | +Use [getJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-getjob(java-lang-string)) to access job status. |
| 137 | + |
| 138 | +```java |
| 139 | +private static void monitorJob(JobClient jobClient, String jobId) { |
| 140 | + try { |
| 141 | + JobResult jobResult = jobClient.getJob(jobId); |
| 142 | + if(jobResult == null) |
| 143 | + { |
| 144 | + System.out.println("No JobResult for: " + jobId); |
| 145 | + return; |
| 146 | + } |
| 147 | + // Check the job result until it's completed |
| 148 | + while(jobResult.getJobStatus() != JobStatus.completed) |
| 149 | + { |
| 150 | + Thread.sleep(100); |
| 151 | + jobResult = jobClient.getJob(jobId); |
| 152 | + System.out.println("Status " + jobResult.getJobStatus() + " for job " + jobId); |
| 153 | + } |
| 154 | + System.out.println("Final status " + jobResult.getJobStatus() + " for job " + jobId); |
| 155 | + } catch (Exception e) { |
| 156 | + System.out.println("Exception monitoring job: " + jobId); |
| 157 | + System.out.println(e.getMessage()); |
| 158 | + return; |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### Query a job status |
| 164 | + |
| 165 | +Use [queryDeviceJob](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-querydevicejob(java-lang-string)) to query job status. |
| 166 | + |
| 167 | +```java |
| 168 | +private static void queryDeviceJobs(JobClient jobClient, String start) throws Exception { |
| 169 | + System.out.println("\nQuery device jobs since " + start); |
| 170 | + |
| 171 | + // Create a jobs query using the time the jobs started |
| 172 | + Query deviceJobQuery = jobClient |
| 173 | + .queryDeviceJob(SqlQuery.createSqlQuery("*", SqlQuery.FromType.JOBS, "devices.jobs.startTimeUtc > '" + start + "'", null).getQuery()); |
| 174 | + |
| 175 | + // Iterate over the list of jobs and print the details |
| 176 | + while (jobClient.hasNextJob(deviceJobQuery)) { |
| 177 | + System.out.println(jobClient.getNextJob(deviceJobQuery)); |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +### SDK schedule job example |
| 183 | + |
| 184 | +The Azure IoT SDK for Java provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job Client Sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/main/service/iot-service-samples/job-client-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/JobClientSample.java) |
0 commit comments