Search in sources :

Example 1 with Tasklet

use of org.springframework.batch.core.step.tasklet.Tasklet in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunnerTests method retryFailedExecution.

@Test
public void retryFailedExecution() 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();
    this.runner.execute(this.job, new JobParameters());
    this.runner.execute(this.job, new JobParameters());
    assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
}
Also used : StepContribution(org.springframework.batch.core.StepContribution) 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 2 with Tasklet

use of org.springframework.batch.core.step.tasklet.Tasklet in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunnerTests method retryFailedExecutionOnNonRestartableJob.

@Test
public void retryFailedExecutionOnNonRestartableJob() throws Exception {
    this.job = this.jobs.get("job").preventRestart().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();
    this.runner.execute(this.job, new JobParameters());
    this.runner.execute(this.job, new JobParameters());
    // A failed job that is not restartable does not re-use the job params of
    // the last execution, but creates a new job instance when running it again.
    assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);
}
Also used : StepContribution(org.springframework.batch.core.StepContribution) 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 3 with Tasklet

use of org.springframework.batch.core.step.tasklet.Tasklet in project RecordManager2 by moravianlibrary.

the class DeleteAllRecordsFromSolrJobConfig method deleteTasklet.

@Bean(name = "deleteAllRecordsFromSolrJob:deleteTasklet")
@StepScope
public Tasklet deleteTasklet(@Value("#{jobParameters[" + Constants.JOB_PARAM_SOLR_URL + "]}") final String solrUrl, @Value("#{jobParameters[" + Constants.JOB_PARAM_SOLR_QUERY + "]}") final String solrQuery) {
    final String query = (solrQuery != null) ? solrQuery : ALL_DOCUMENTS_QUERY;
    return new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            SolrServerFacade server = factory.create(solrUrl);
            server.deleteByQuery(query);
            server.commit();
            return RepeatStatus.FINISHED;
        }
    };
}
Also used : StepContribution(org.springframework.batch.core.StepContribution) Tasklet(org.springframework.batch.core.step.tasklet.Tasklet) ChunkContext(org.springframework.batch.core.scope.context.ChunkContext) SolrServerFacade(cz.mzk.recordmanager.server.solr.SolrServerFacade) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) Bean(org.springframework.context.annotation.Bean)

Example 4 with Tasklet

use of org.springframework.batch.core.step.tasklet.Tasklet 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 5 with Tasklet

use of org.springframework.batch.core.step.tasklet.Tasklet in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunnerTests method init.

@Before
public void init() throws Exception {
    this.context.register(BatchConfiguration.class);
    this.context.refresh();
    JobRepository jobRepository = this.context.getBean(JobRepository.class);
    this.jobLauncher = this.context.getBean(JobLauncher.class);
    this.jobs = new JobBuilderFactory(jobRepository);
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    this.steps = new StepBuilderFactory(jobRepository, transactionManager);
    this.step = this.steps.get("step").tasklet(new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            return null;
        }
    }).build();
    this.job = this.jobs.get("job").start(this.step).build();
    this.jobExplorer = this.context.getBean(JobExplorer.class);
    this.runner = new JobLauncherCommandLineRunner(this.jobLauncher, this.jobExplorer);
    this.context.getBean(BatchConfiguration.class).clear();
}
Also used : SimpleJobLauncher(org.springframework.batch.core.launch.support.SimpleJobLauncher) JobLauncher(org.springframework.batch.core.launch.JobLauncher) StepContribution(org.springframework.batch.core.StepContribution) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) JobRepository(org.springframework.batch.core.repository.JobRepository) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Tasklet(org.springframework.batch.core.step.tasklet.Tasklet) ChunkContext(org.springframework.batch.core.scope.context.ChunkContext) JobBuilderFactory(org.springframework.batch.core.configuration.annotation.JobBuilderFactory) RepeatStatus(org.springframework.batch.repeat.RepeatStatus) JobExplorer(org.springframework.batch.core.explore.JobExplorer) Before(org.junit.Before)

Aggregations

Tasklet (org.springframework.batch.core.step.tasklet.Tasklet)6 StepContribution (org.springframework.batch.core.StepContribution)5 ChunkContext (org.springframework.batch.core.scope.context.ChunkContext)5 Test (org.junit.Test)3 JobParameters (org.springframework.batch.core.JobParameters)3 RunIdIncrementer (org.springframework.batch.core.launch.support.RunIdIncrementer)3 SolrServerFacade (cz.mzk.recordmanager.server.solr.SolrServerFacade)1 IOException (java.io.IOException)1 Before (org.junit.Before)1 MifosRuntimeException (org.mifos.core.MifosRuntimeException)1 TaskSystemException (org.mifos.framework.components.batchjobs.exceptions.TaskSystemException)1 SchedulerException (org.quartz.SchedulerException)1 SimpleTrigger (org.quartz.SimpleTrigger)1 Job (org.springframework.batch.core.Job)1 JobParametersBuilder (org.springframework.batch.core.JobParametersBuilder)1 JobFactory (org.springframework.batch.core.configuration.JobFactory)1 JobBuilderFactory (org.springframework.batch.core.configuration.annotation.JobBuilderFactory)1 StepBuilderFactory (org.springframework.batch.core.configuration.annotation.StepBuilderFactory)1 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)1 JobExplorer (org.springframework.batch.core.explore.JobExplorer)1