Vous êtes sur la page 1sur 5

File: /home/rdumbraveanu/workspace/ing/batch/SimpleJobRunner.

java
package com.legalbistro.util.spring.batch; import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import import java.util.ArrayList; java.util.Arrays; java.util.HashMap; java.util.HashSet; java.util.List; java.util.Map; java.util.Properties; java.util.Set; org.apache.commons.logging.Log; org.apache.commons.logging.LogFactory; org.springframework.batch.core.BatchStatus; org.springframework.batch.core.ExitStatus; org.springframework.batch.core.Job; org.springframework.batch.core.JobExecution; org.springframework.batch.core.JobInstance; org.springframework.batch.core.JobParameter; org.springframework.batch.core.JobParameters; org.springframework.batch.core.JobParametersIncrementer; org.springframework.batch.core.configuration.JobLocator; org.springframework.batch.core.converter.DefaultJobParametersConverter; org.springframework.batch.core.converter.JobParametersConverter; org.springframework.batch.core.explore.JobExplorer; org.springframework.batch.core.launch.JobExecutionNotFailedException; org.springframework.batch.core.launch.JobLauncher; org.springframework.batch.core.launch.JobParametersNotFoundException; org.springframework.batch.core.launch.support.ExitCodeMapper; org.springframework.batch.core.launch.support.JvmSystemExiter; org.springframework.batch.core.launch.support.SimpleJvmExitCodeMapper; org.springframework.batch.core.launch.support.SystemExiter; org.springframework.beans.factory.BeanDefinitionStoreException; org.springframework.beans.factory.config.AutowireCapableBeanFactory; org.springframework.context.ConfigurableApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; org.springframework.util.Assert; org.springframework.util.StringUtils;

Page 1 of 5

import com.legalbistro.parser.Bootstrap; public class SimpleJobRunner { protected static final Log logger = LogFactory .getLog(SimpleJobRunner.class); private ExitCodeMapper exitCodeMapper = new SimpleJvmExitCodeMapper(); private JobLauncher launcher; private JobLocator jobLocator; // Package private for unit test private SystemExiter systemExiter = new JvmSystemExiter(); private String message = ""; private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter(); private JobExplorer jobExplorer; /** * Injection setter for the {@link JobLauncher}. * * @param launcher the launcher to set */ public void setLauncher(JobLauncher launcher) {

File: /home/rdumbraveanu/workspace/ing/batch/SimpleJobRunner.java
this .launcher = launcher; } /** * Injection setter for {@link JobExplorer}. * * @param jobExplorer the {@link JobExplorer} to set */ public void setJobExplorer(JobExplorer jobExplorer) { this .jobExplorer = jobExplorer; } /** * Injection setter for the {@link ExitCodeMapper}. * * @param exitCodeMapper the exitCodeMapper to set */ public void setExitCodeMapper(ExitCodeMapper exitCodeMapper) { this .exitCodeMapper = exitCodeMapper; } /** * Static setter for the {@link SystemExiter} so it can be adjusted before * dependency injection. Typically overridden by * {@link #setSystemExiter(SystemExiter)}. * * @param systemExitor */ public static void presetSystemExiter(SystemExiter systemExiter) { systemExiter = systemExiter; } /** * Retrieve the error message set by an instance of * {@link CommandLineJobRunner} as it exits. Empty if the last job launched * was successful. * * @return the error message */ public String getErrorMessage() { return message; } /** * Injection setter for the {@link SystemExiter}. * * @param systemExitor */ public void setSystemExiter(SystemExiter systemExiter) { systemExiter = systemExiter; } /** * Injection setter for {@link JobParametersConverter}. * * @param jobParametersConverter */ public void setJobParametersConverter( JobParametersConverter jobParametersConverter) { this .jobParametersConverter = jobParametersConverter; } /** * Delegate to the exiter to (possibly) exit the VM gracefully. * * @param status */

Page 2 of 5

File: /home/rdumbraveanu/workspace/ing/batch/SimpleJobRunner.java
public void exit(int status) { systemExiter.exit(status); } /** * {@link JobLocator} to find a job to run. * @param jobLocator a {@link JobLocator} */ public void setJobLocator(JobLocator jobLocator) { this .jobLocator = jobLocator; } /* * Start a job by obtaining a combined classpath using the job launcher and * job paths. If a JobLocator has been set, then use it to obtain an actual * job, if not ask the context for it. */ public int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) { ConfigurableApplicationContext context = null; try { context = new ClassPathXmlApplicationContext(jobPath); context .getAutowireCapableBeanFactory() .autowireBeanProperties( this , AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); Assert

Page 3 of 5

.state(launcher != null, "A JobLauncher must be provided. Please add one to the configuration."); if (opts.contains("-restart") || opts.contains("-next")) { Assert .state( jobExplorer != null, "A JobExplorer must be provided for a restart or start next operation. Please add one to the configuration."); } String jobName = jobIdentifier; JobParameters jobParameters = jobParametersConverter .getJobParameters(StringUtils .splitArrayElementsIntoProperties( parameters, "=")); Assert .isTrue( parameters == null || parameters.length == 0 || !jobParameters.isEmpty(), "Invalid JobParameters " + Arrays.asList(parameters) + ". If parameters are provided they should be in the form name=value (no whitespace)."); if (opts.contains("-restart")) { JobExecution jobExecution = getLastFailedJobExecution(jobIdentifier); if (jobExecution == null) { throw new JobExecutionNotFailedException( "No failed or stopped execution found for job=" + jobIdentifier); } jobParameters = jobExecution.getJobInstance() .getJobParameters();

File: /home/rdumbraveanu/workspace/ing/batch/SimpleJobRunner.java
jobName = jobExecution.getJobInstance().getJobName(); } Job job; if (jobLocator != null) { job = jobLocator.getJob(jobName); } else { job = (Job) context.getBean(jobName); } if (opts.contains("-next")) { JobParameters nextParameters = getNextJobParameters(job); Map<String, JobParameter> map = new HashMap<String, JobParameter>( nextParameters.getParameters()); map.putAll(jobParameters.getParameters()); jobParameters = new JobParameters(map); } JobExecution jobExecution = launcher .run(job, jobParameters); return exitCodeMapper.intValue(jobExecution.getExitStatus() .getExitCode()); } catch (Throwable e) { String message = "Job Terminated in error: " + e.getMessage(); logger.error(message, e); message = message; return exitCodeMapper.intValue(ExitStatus.FAILED .getExitCode()); } finally { if (context != null) { context.close(); } } } /** * @param jobIdentifier * @return * @throws JobParametersNotFoundException */ private JobExecution getLastFailedJobExecution(String jobIdentifier) { Long executionId = getLongIdentifier(jobIdentifier); if (executionId != null) { JobExecution jobExecution = jobExplorer .getJobExecution(executionId); if (jobExecution.getStatus().isGreaterThan( BatchStatus.STOPPING)) { return jobExecution; } } int start = 0; int count = 100; List<JobInstance> lastInstances = jobExplorer.getJobInstances( jobIdentifier, start, count); while (!lastInstances.isEmpty()) { for (JobInstance jobInstance : lastInstances) { List<JobExecution> jobExecutions = jobExplorer .getJobExecutions(jobInstance); if (jobExecutions == null || jobExecutions.isEmpty()) { continue; }

Page 4 of 5

File: /home/rdumbraveanu/workspace/ing/batch/SimpleJobRunner.java
JobExecution jobExecution = jobExecutions .get(jobExecutions.size() - 1); if (jobExecution.getStatus().isGreaterThan( BatchStatus.STOPPING)) { return jobExecution; } } start += count; lastInstances = jobExplorer.getJobInstances(jobIdentifier, start, count); } return null; } private Long getLongIdentifier(String jobIdentifier) { try { return new Long(jobIdentifier); } catch (NumberFormatException e) { // Not an ID - must be a name return null; } } /** * @param job the job that we need to find the next parameters for * @return the next job parameters if they can be located * @throws JobParametersNotFoundException if there is a problem */ private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException { String jobIdentifier = job.getName(); JobParameters jobParameters; List<JobInstance> lastInstances = jobExplorer.getJobInstances( jobIdentifier, 0, 1); JobParametersIncrementer incrementer = job .getJobParametersIncrementer(); if (incrementer == null) { throw new JobParametersNotFoundException( "No job parameters incrementer found for job=" + jobIdentifier); } if (lastInstances.isEmpty()) { jobParameters = incrementer.getNext(new JobParameters()); if (jobParameters == null) { throw new JobParametersNotFoundException( "No bootstrap parameters found from incrementer for job=" + jobIdentifier); } } else { jobParameters = incrementer.getNext(lastInstances.get(0) .getJobParameters()); } return jobParameters; } }

Page 5 of 5

Vous aimerez peut-être aussi