Search in sources :

Example 66 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class RoleBasedAuthenticationSample method runJobClientSample.

private static void runJobClientSample(String iotHubHostName, TokenCredential credential) {
    // JobClient has some configurable options for HTTP read and connect timeouts, as well as for setting proxies.
    // For this sample, the default options will be used though.
    JobClientOptions options = JobClientOptions.builder().build();
    // This constructor takes in your implementation of TokenCredential which allows you to use RBAC authentication
    // rather than symmetric key based authentication that comes with constructors that take connection strings.
    JobClient jobClient = new JobClient(iotHubHostName, credential, options);
    try {
        System.out.println("Querying all active jobs for your IoT Hub");
        Query deviceJobQuery = jobClient.queryDeviceJob(SqlQuery.createSqlQuery("*", SqlQuery.FromType.JOBS, null, null).getQuery());
        int queriedJobCount = 0;
        while (jobClient.hasNextJob(deviceJobQuery)) {
            queriedJobCount++;
            JobResult job = jobClient.getNextJob(deviceJobQuery);
            System.out.println(String.format("Job %s of type %s has status %s", job.getJobId(), job.getJobType(), job.getJobStatus()));
        }
        if (queriedJobCount == 0) {
            System.out.println("No active jobs found for your IoT Hub");
        }
    } catch (IotHubException | IOException e) {
        System.err.println("Failed to query the jobs for your IoT Hub");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : JobClientOptions(com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 67 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class JobClientSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting sample...");
    // *************************************** Create JobClient ***************************************
    JobClient jobClient = createJobClient();
    // *************************************** Schedule twin job ***************************************
    JobResult jobResultTwin = scheduleUpdateTwin(jobClient);
    monitorJob(jobClient, jobIdTwin);
    // *************************************** Schedule method job ***************************************
    JobResult jobResultMethod = scheduleUpdateMethod(jobClient);
    monitorJob(jobClient, jobIdMethod);
    // *************************************** Query Jobs Response ***************************************
    queryJobsResponse(jobClient);
    // *************************************** Query Device Job ***************************************
    queryDeviceJobs(jobClient);
    System.out.println("Shutting down sample...");
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient)

Aggregations

JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)67 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)61 Test (org.junit.Test)61 NonStrictExpectations (mockit.NonStrictExpectations)36 Date (java.util.Date)26 HashSet (java.util.HashSet)21 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)14 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)14 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)13 Verifications (mockit.Verifications)13 Expectations (mockit.Expectations)11 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)10 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)10 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)8 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)8 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)7 IOException (java.io.IOException)6 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)5 MalformedURLException (java.net.MalformedURLException)4 JobClientOptions (com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions)3