AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

azure-compute-batch-java

Azure Batch SDK for Java. Run large-scale parallel and HPC batch jobs with pools, jobs, tasks, and compute nodes. Triggers: "BatchClient java", "azure batch java", "batch pool java", "batch job java", "HPC java", "parallel computing java".

Original source
microsoft
License
MIT
Pinned revision
3f5b461b2314e01c400564051e92469114c5b68f

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read microsoft--azure-compute-batch-java at revision 3f5b461b2314e01c400564051e92469114c5b68f, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 2

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

  1. Use Entra ID — Preferred over shared key for authentication
  2. Use management SDK for pools — azure-resourcemanager-batch supports managed identities
  3. Batch task creation — Use createTaskCollection or createTasks for multiple tasks
  4. Handle LRO properly — Pool resize, delete operations are long-running
  5. Monitor task counts — Use getJobTaskCounts to track progress
  6. Set constraints — Configure maxWallClockTime and maxTaskRetryCount
  7. Use low-priority nodes — Cost savings for fault-tolerant workloads
  8. Enable autoscale — Dynamically adjust pool size based on workload