use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.
the class JobClientTests method setUp.
@BeforeClass
public static void setUp() throws IOException, IotHubException, InterruptedException, URISyntaxException {
iotHubConnectionString = Tools.retrieveEnvironmentVariableValue(TestConstants.IOT_HUB_CONNECTION_STRING_ENV_VAR_NAME);
isBasicTierHub = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_BASIC_TIER_HUB_ENV_VAR_NAME));
isPullRequest = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_PULL_REQUEST));
jobClient = new JobClient(iotHubConnectionString);
registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
String uuid = UUID.randomUUID().toString();
for (int i = 0; i < MAX_DEVICES; i++) {
testDevice = Tools.addDeviceWithRetry(registryManager, Device.createFromId(DEVICE_ID_NAME.concat("-" + i + "-" + uuid), DeviceStatus.Enabled, null));
DeviceTestManager testManager = new DeviceTestManager(new DeviceClient(registryManager.getDeviceConnectionString(testDevice), IotHubClientProtocol.AMQPS));
testManager.client.open();
testManager.subscribe(true, true);
devices.add(testManager);
}
}
use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.
the class JobClientTests method buildJobClientWithAzureSasCredential.
private static JobClient buildJobClientWithAzureSasCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
JobClientOptions options = JobClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
return new JobClient(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.
the class JobClientTests method jobClientTokenRenewalWithAzureSasCredential.
@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
public void jobClientTokenRenewalWithAzureSasCredential() throws InterruptedException, IOException, IotHubException {
// Arrange
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
JobClient jobClientWithSasCredential = new JobClient(iotHubConnectionStringObj.getHostName(), sasCredential);
// JobClient usage should succeed since the shared access signature hasn't expired yet
scheduleDeviceMethod(jobClientWithSasCredential);
// deliberately expire the SAS token to provoke a 401 to ensure that the job client is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
scheduleDeviceMethod(jobClientWithSasCredential);
fail("Expected scheduling a job to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (IotHubUnathorizedException e) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
sasCredential.update(serviceSasToken.toString());
// JobClient usage should succeed since the shared access signature has been renewed
scheduleDeviceMethod(jobClientWithSasCredential);
}
use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.
the class AzureSasCredentialSample method runJobClientSample.
private static void runJobClientSample(String iotHubHostName, AzureSasCredential 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 AzureSasCredential which allows you to use symmetric key based
// authentication without giving the client your connection string.
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);
}
}
use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.
the class JobClientSample method createJobClient.
private static JobClient createJobClient() throws IOException {
System.out.println("Create JobClient from the connectionString...");
JobClient jobClient = JobClient.createFromConnectionString(iotHubConnectionString);
System.out.println("JobClient created with success");
System.out.println();
return jobClient;
}
Aggregations