Example usage for org.springframework.batch.support PropertiesConverter stringToProperties

List of usage examples for org.springframework.batch.support PropertiesConverter stringToProperties

Introduction

In this page you can find the example usage for org.springframework.batch.support PropertiesConverter stringToProperties.

Prototype

public static Properties stringToProperties(String stringToParse) 

Source Link

Document

Parse a String to a Properties object.

Usage

From source file:de.codecentric.batch.web.JobOperationsController.java

@RequestMapping(value = "/jobs/{jobName}", method = RequestMethod.POST)
public String launch(@PathVariable String jobName, @RequestParam MultiValueMap<String, String> payload)
        throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException,
        JobRestartException, JobInstanceAlreadyCompleteException, JobParametersNotFoundException {
    String parameters = payload.getFirst(JOB_PARAMETERS);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Attempt to start job with name " + jobName + " and parameters " + parameters + ".");
    }/*from w  w  w  .  j  a v  a2 s  .c om*/
    try {
        Job job = jobRegistry.getJob(jobName);
        JobParameters jobParameters = createJobParametersWithIncrementerIfAvailable(parameters, job);
        Long id = jobLauncher.run(job, jobParameters).getId();
        return String.valueOf(id);
    } catch (NoSuchJobException e) {
        // Job hasn't been found in normal context, so let's check if there's a JSR-352 job.
        String jobConfigurationLocation = "/META-INF/batch-jobs/" + jobName + ".xml";
        Resource jobXml = new ClassPathResource(jobConfigurationLocation);
        if (!jobXml.exists()) {
            throw e;
        } else {
            Long id = jsrJobOperator.start(jobName, PropertiesConverter.stringToProperties(parameters));
            return String.valueOf(id);
        }
    }
}

From source file:de.codecentric.batch.web.JobOperationsController.java

private JobParameters createJobParametersWithIncrementerIfAvailable(String parameters, Job job)
        throws JobParametersNotFoundException {
    JobParameters jobParameters = jobParametersConverter
            .getJobParameters(PropertiesConverter.stringToProperties(parameters));
    // use JobParametersIncrementer to create JobParameters if incrementer is set and only if the job is no restart
    if (job.getJobParametersIncrementer() != null) {
        JobExecution lastJobExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
        boolean restart = false;
        // check if job failed before
        if (lastJobExecution != null) {
            BatchStatus status = lastJobExecution.getStatus();
            if (status.isUnsuccessful() && status != BatchStatus.ABANDONED) {
                restart = true;/*w w  w  .ja v  a 2  s.  c o  m*/
            }
        }
        // if it's not a restart, create new JobParameters with the incrementer
        if (!restart) {
            JobParameters nextParameters = getNextJobParameters(job);
            Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters());
            map.putAll(jobParameters.getParameters());
            jobParameters = new JobParameters(map);
        }
    }
    return jobParameters;
}

From source file:org.springframework.batch.core.launch.support.SimpleJobOperator.java

@Override
public Long start(String jobName, String parameters)
        throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException {

    logger.info("Checking status of job with name=" + jobName);

    JobParameters jobParameters = jobParametersConverter
            .getJobParameters(PropertiesConverter.stringToProperties(parameters));

    if (jobRepository.isJobInstanceExists(jobName, jobParameters)) {
        throw new JobInstanceAlreadyExistsException(
                String.format("Cannot start a job instance that already exists with name=%s and parameters=%s",
                        jobName, parameters));
    }//from   w  ww.  java  2 s .co m

    Job job = jobRegistry.getJob(jobName);

    logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
    try {
        return jobLauncher.run(job, jobParameters).getId();
    } catch (JobExecutionAlreadyRunningException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job execution already running", jobName, parameters), e);
    } catch (JobRestartException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job not restartable", jobName, parameters), e);
    } catch (JobInstanceAlreadyCompleteException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job already complete", jobName, parameters), e);
    }

}