Java Batch JobUtil

package com.espalier.jbatch.course.util;
 
import static org.junit.Assert.assertEquals;
 
import java.util.List;
import java.util.logging.Logger;
 
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;
import javax.batch.runtime.StepExecution;
 
public class JobUtil {
 
	private final static Logger LOG = BatLogger.TEST.getLogger();
 
	public static void waitForJobToComplete(final JobOperator jobOper, long executionId) throws Exception {
 
		loop: while (true) {
			JobExecution je = jobOper.getJobExecution(executionId);
			List<StepExecution> steps = jobOper.getStepExecutions(executionId);
 
			switch (je.getBatchStatus()) {
			case COMPLETED:
				assertEquals("Job batch status", BatchStatus.COMPLETED, je.getBatchStatus());
				assertEquals("exit status", "COMPLETED", je.getExitStatus());
				assertEquals("Steps executed", 1, steps.size());
				assertEquals("Step failed", BatchStatus.COMPLETED, steps.get(0).getBatchStatus());
				break loop;
 
			case ABANDONED:
			case FAILED:
				LOG.severe("Batch status " + je.getBatchStatus());
				break;
 
			default:
				LOG.info("Sleeping for 1 secs");
				Thread.sleep(1000);
			}
		}
	}
}
package com.espalier.jbatch.course.util;
 
import javax.batch.runtime.context.JobContext;
import javax.batch.runtime.context.StepContext;
 
public class ContextHelper {
 
	private ContextHelper() {
	}
 
	public static String getInfo(final JobContext jobContext) {
		if (null != jobContext) {
			StringBuilder builder = new StringBuilder(jobContext.getJobName());
			builder.append(" batchStatus = " + jobContext.getBatchStatus());
			builder.append(", executionId = " + jobContext.getExecutionId());
			builder.append(", exitStatus = " + jobContext.getExitStatus());
			builder.append(", instanceId = " + jobContext.getInstanceId());
			builder.append(", jobName = " + jobContext.getJobName());
			builder.append(", properties = " + jobContext.getProperties());
			builder.append(", transientUserData = " + jobContext.getTransientUserData());
 
			return builder.toString();
		}
 
		return "jobContext == null";
	}
 
	public static String getInfo(final StepContext stepContext) {
		if (null != stepContext) {
			StringBuilder builder = new StringBuilder(stepContext.getStepName());
			builder.append(" batchStatus = " + stepContext.getBatchStatus());
			builder.append(", executionId = " + stepContext.getStepExecutionId());
			builder.append(", exitStatus = " + stepContext.getException());
			builder.append(", instanceId = " + stepContext.getMetrics());
			builder.append(", jobName = " + stepContext.getPersistentUserData());
			builder.append(", properties = " + stepContext.getProperties());
			builder.append(", transientUserData = " + stepContext.getTransientUserData());
 
			return builder.toString();
		}
 
		return "stepContext == null";
	}
}
Posted in Code, Java EE

Leave a Reply