references/examples.md · 24000
throw new RuntimeException(e);
}
}
}
public String runJob(String jobId, List<String> commands) {
// Create job
client.createJob(
new BatchJobCreateParameters(jobId, new BatchPoolInfo().setPoolId(poolId))
.setOnAllTasksComplete(OnAllBatchTasksComplete.TERMINATE_JOB),
null);
System.out.println("Job created: " + jobId);
// Create tasks
List<BatchTaskCreateParameters> tasks = new ArrayList<>();
for (int i = 0; i < commands.size(); i++) {
tasks.add(new BatchTaskCreateParameters("task" + i, commands.get(i)));
}
client.createTasks(jobId, tasks);
System.out.println("Created " + tasks.size() + " tasks");
// Wait for completion
waitForJobComplete(jobId);
return jobId;
}
private void waitForJobComplete(String jobId) {
System.out.println("Waiting for job to complete...");
while (true) {
BatchJob job = client.getJob(jobId, null, null);
if (job.getState() == BatchJobState.COMPLETED) {
System.out.println("Job completed");
break;
} else if (job.getState() == BatchJobState.DISABLED
|| job.getState() == BatchJobState.TERMINATING) {
throw new RuntimeException("Job failed or was terminated");
}
// Show progress
BatchTaskCountsResult counts = client.getJobTaskCounts(jobId);
System.out.printf("Progress: %d/%d tasks completed%n",
counts.getTaskCounts().getCompleted(),
counts.getTaskCounts().getActive() + counts.getTaskCounts().getRunning()
+ counts.getTaskCounts().getCompleted());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
public Map<String, String> getTaskOutputs(String jobId) {
Map<String, String> outputs = new HashMap<>();
PagedIterable<BatchTask> tasks = client.listTasks(jobId);
for (BatchTask task : tasks) {
try {
BinaryData stdout = client.getTaskFile(jobId, task.getId(), "stdout.txt");
outputs.put(task.getId(), new String(stdout.toBytes(), StandardCharsets.UTF_8));
} catch (Exception e) {
outputs.put(task.getId(), "Error: " + e.getMessage());
}
}
return outputs;
}
public void cleanup(String jobId) {
try {
client.beginDeleteJob(jobId).waitForCompletion();
System.out.println("Job deleted: " + jobId);
} catch (Exception e) {
System.err.println("Failed to delete job: " + e.getMessage());
}
}
public static void main(String[] args) {
BatchJobRunner runner = new BatchJobRunner("my-batch-pool");
try {
// Ensure pool exists
runner.ensurePoolExists();
// Run job with tasks
List<String> commands = Arrays.asList(
"echo 'Processing partition 1' && sleep 5",
"echo 'Processing partition 2' && sleep 5",
"echo 'Processing partition 3' && sleep 5",
"echo 'Processing partition 4' && sleep 5"
);
String jobId = "job-" + System.currentTimeMillis();
runner.runJob(jobId, commands);
// Get outputs
Map<String, String> outputs = runner.getTaskOutputs(jobId);
System.out.println("\n=== Task Outputs ===");
outputs.forEach((taskId, output) -> {
System.out.printf("%s: %s%n", taskId, output.trim());
});
// Cleanup
runner.cleanup(jobId);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
## Environment Variables
```bash
AZURE_BATCH_ENDPOINT=https://<account>.<region>.batch.azure.com
AZURE_BATCH_ACCOUNT=<account-name>
AZURE_BATCH_ACCESS_KEY=<account-key>
# For DefaultAzureCredential
AZURE_CLIENT_ID=<service-principal-client-id>
AZURE_CLIENT_SECRET=<service-principal-secret>
AZURE_TENANT_ID=<tenant-id>
Best Practices
- Use Entra ID — Preferred over shared key for authentication
- Use management SDK for pools —
azure-resourcemanager-batchsupports managed identities - Batch task creation — Use
createTaskCollectionorcreateTasksfor multiple tasks - Handle LRO properly — Pool resize, delete operations are long-running
- Monitor task counts — Use
getJobTaskCountsto track progress - Set constraints — Configure
maxWallClockTimeandmaxTaskRetryCount - Use low-priority nodes — Cost savings for fault-tolerant workloads
- Enable autoscale — Dynamically adjust pool size based on workload