*/
private synchronized long createAndLaunchJobWithSubJob(String clusterName,
String subJobName, List<JobParameters> subJobParameters,
ClusterStatus successStatus,
ClusterStatus failStatus) throws Exception {
SimpleJob mainJob =
new SimpleJob("composed-job-" + clusterName + "-" + subJobName
+ "-" + System.nanoTime());
//SimpleJob mainJob = new SimpleJob("composed-job-" + clusterName + "-" + subJobName);
StepExecutionListener[] jobStepListeners = createJobStepListener();
Map<String, JobParameter> mainJobParams =
new TreeMap<String, JobParameter>();
mainJobParams.put(JobConstants.TIMESTAMP_JOB_PARAM, new JobParameter(
new Date()));
mainJobParams.put(JobConstants.CLUSTER_NAME_JOB_PARAM, new JobParameter(
clusterName));
mainJobParams.put(JobConstants.CLUSTER_SUCCESS_STATUS_JOB_PARAM,
new JobParameter(successStatus.name()));
mainJobParams.put(JobConstants.CLUSTER_FAILURE_STATUS_JOB_PARAM,
new JobParameter(failStatus.name()));
//enable sub job indicator to for job progress query
mainJobParams.put(JobConstants.SUB_JOB_ENABLED, new JobParameter(1l));
Job subJob = jobRegistry.getJob(subJobName);
for (int stepNumber = 0, j = subJobParameters.size(); stepNumber < j; stepNumber++) {
SubJobStep subJobStep = new SubJobStep();
subJobStep.setName(subJobName + "-subJobStep-" + stepNumber);
subJobStep.setJob(subJob);
subJobStep.setJobParametersExtractor(jobParametersExtractor);
subJobStep.setJobExecutionStatusHolder(jobExecutionStatusHolder);
subJobStep
.setMainJobExecutionStatusHolder(mainJobExecutionStatusHolder);
subJobStep.setJobLauncher(jobLauncher);
subJobStep.setJobRepository(jobRepository);
subJobStep.setStepExecutionListeners(jobStepListeners);
subJobStep.afterPropertiesSet();
mainJob.addStep(subJobStep);
logger.debug("added sub job step: " + subJobStep.getName());
int subJobParametersNumber =
subJobParameters.get(stepNumber).getParameters().keySet().size();
mainJobParams.put(JobConstants.SUB_JOB_PARAMETERS_NUMBER + stepNumber,
new JobParameter((long) subJobParametersNumber));
int count = 0;
for (String key : subJobParameters.get(stepNumber).getParameters()
.keySet()) {
int index = count++;
mainJobParams.put(
JobUtils.getSubJobParameterPrefixKey(stepNumber, index),
new JobParameter(key));
mainJobParams.put(
JobUtils.getSubJobParameterPrefixValue(stepNumber, index),
subJobParameters.get(stepNumber).getParameters().get(key));
}
}
mainJob
.setJobExecutionListeners(new JobExecutionListener[] { mainJobExecutionListener });
mainJob.setJobRepository(jobRepository);
mainJob.afterPropertiesSet();
JobFactory jobFactory = new ReferenceJobFactory(mainJob);
jobRegistry.register(jobFactory);
logger.info("registered job: " + mainJob.getName());
JobParameters mainJobParameters = new JobParameters(mainJobParams);
JobExecution mainJobExecution =
jobLauncher.run(mainJob, mainJobParameters);
logger.info("launched main job: " + mainJob.getName());
return mainJobExecution.getId();
}