Search in sources :

Example 16 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testDefinesAndLaunchesNamedJob.

@Test
public void testDefinesAndLaunchesNamedJob() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteRegisteredJob");
    this.context.register(NamedJobConfigurationWithRegisteredJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    JobRepository repository = this.context.getBean(JobRepository.class);
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    this.context.getBean(JobLauncherCommandLineRunner.class).run();
    assertThat(repository.getLastJobExecution("discreteRegisteredJob", new JobParameters())).isNotNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobParameters(org.springframework.batch.core.JobParameters) JobRepository(org.springframework.batch.core.repository.JobRepository) Test(org.junit.Test)

Example 17 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testRenamePrefix.

@Test
public void testRenamePrefix() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.name:batchtest", "spring.batch.schema:classpath:batch/custom-schema-hsql.sql", "spring.batch.tablePrefix:PREFIX_");
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    assertThat(this.context.getBean(BatchProperties.class).getInitializer().isEnabled()).isTrue();
    assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)).queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
    JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
    assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
    JobRepository jobRepository = this.context.getBean(JobRepository.class);
    assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobExplorer(org.springframework.batch.core.explore.JobExplorer) JobParameters(org.springframework.batch.core.JobParameters) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) JobRepository(org.springframework.batch.core.repository.JobRepository) Test(org.junit.Test)

Example 18 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunnerTests method retryFailedExecutionWithNonIdentifyingParameters.

@Test
public void retryFailedExecutionWithNonIdentifyingParameters() throws Exception {
    this.job = this.jobs.get("job").start(this.steps.get("step").tasklet(new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            throw new RuntimeException("Planned");
        }
    }).build()).incrementer(new RunIdIncrementer()).build();
    JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false).toJobParameters();
    this.runner.execute(this.job, jobParameters);
    this.runner.execute(this.job, jobParameters);
    assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
}
Also used : StepContribution(org.springframework.batch.core.StepContribution) JobParametersBuilder(org.springframework.batch.core.JobParametersBuilder) JobParameters(org.springframework.batch.core.JobParameters) Tasklet(org.springframework.batch.core.step.tasklet.Tasklet) ChunkContext(org.springframework.batch.core.scope.context.ChunkContext) RunIdIncrementer(org.springframework.batch.core.launch.support.RunIdIncrementer) Test(org.junit.Test)

Example 19 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunner method getNextJobParameters.

private JobParameters getNextJobParameters(Job job, JobParameters additionalParameters) {
    String name = job.getName();
    JobParameters parameters = new JobParameters();
    List<JobInstance> lastInstances = this.jobExplorer.getJobInstances(name, 0, 1);
    JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
    Map<String, JobParameter> additionals = additionalParameters.getParameters();
    if (lastInstances.isEmpty()) {
        // Start from a completely clean sheet
        if (incrementer != null) {
            parameters = incrementer.getNext(new JobParameters());
        }
    } else {
        List<JobExecution> previousExecutions = this.jobExplorer.getJobExecutions(lastInstances.get(0));
        JobExecution previousExecution = previousExecutions.get(0);
        if (previousExecution == null) {
            // Normally this will not happen - an instance exists with no executions
            if (incrementer != null) {
                parameters = incrementer.getNext(new JobParameters());
            }
        } else if (isStoppedOrFailed(previousExecution) && job.isRestartable()) {
            // Retry a failed or stopped execution
            parameters = previousExecution.getJobParameters();
            // Non-identifying additional parameters can be removed to a retry
            removeNonIdentifying(additionals);
        } else if (incrementer != null) {
            // New instance so increment the parameters if we can
            parameters = incrementer.getNext(previousExecution.getJobParameters());
        }
    }
    return merge(parameters, additionals);
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) JobInstance(org.springframework.batch.core.JobInstance) JobParameters(org.springframework.batch.core.JobParameters) JobParametersIncrementer(org.springframework.batch.core.JobParametersIncrementer) JobParameter(org.springframework.batch.core.JobParameter)

Example 20 with JobParameters

use of org.springframework.batch.core.JobParameters in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunner method merge.

private JobParameters merge(JobParameters parameters, Map<String, JobParameter> additionals) {
    Map<String, JobParameter> merged = new HashMap<>();
    merged.putAll(parameters.getParameters());
    merged.putAll(additionals);
    parameters = new JobParameters(merged);
    return parameters;
}
Also used : HashMap(java.util.HashMap) JobParameters(org.springframework.batch.core.JobParameters) JobParameter(org.springframework.batch.core.JobParameter)

Aggregations

JobParameters (org.springframework.batch.core.JobParameters)20 Test (org.junit.Test)12 Date (java.util.Date)5 JobRepository (org.springframework.batch.core.repository.JobRepository)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 RunIdIncrementer (org.springframework.batch.core.launch.support.RunIdIncrementer)4 Job (org.springframework.batch.core.Job)3 JobParameter (org.springframework.batch.core.JobParameter)3 JobParametersBuilder (org.springframework.batch.core.JobParametersBuilder)3 StepContribution (org.springframework.batch.core.StepContribution)3 ChunkContext (org.springframework.batch.core.scope.context.ChunkContext)3 Tasklet (org.springframework.batch.core.step.tasklet.Tasklet)3 HashMap (java.util.HashMap)2 JobExecution (org.springframework.batch.core.JobExecution)2 JobInstance (org.springframework.batch.core.JobInstance)2 Map (java.util.Map)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 DateTimeService (org.mifos.framework.util.DateTimeService)1 CronTrigger (org.quartz.CronTrigger)1 SimpleTrigger (org.quartz.SimpleTrigger)1