Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Created August 10, 2018 08:12
Show Gist options
  • Save fmbenhassine/beb4db7cafd5ff00dca349962b195d00 to your computer and use it in GitHub Desktop.
Save fmbenhassine/beb4db7cafd5ff00dca349962b195d00 to your computer and use it in GitHub Desktop.
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample;
import java.util.ArrayList;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.step.NoWorkFoundStepExecutionListener;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
@Configuration
@EnableBatchProcessing
public class MyJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public ItemReader<String> itemReader() {
return new ListItemReader<>(new ArrayList<>());
}
@Bean
public FlatFileItemWriter<String> itemWriter() {
return
new FlatFileItemWriterBuilder<String>()
.name("itemWriter")
.resource(new FileSystemResource("build/output.txt"))
.lineAggregator(new PassThroughLineAggregator<>())
// .shouldDeleteIfEmpty(true) // use this or fail the step with NoWorkFoundStepExecutionListener
.build();
}
@Bean
public Step step() {
return steps.get("step")
.<String, String>chunk(1)
.reader(itemReader())
.writer(itemWriter())
// the following listener sets the exit status of the step to FAILED, but the status will still be completed
// so by default, any further step will be executed.
// It is required to use .on("FAILED").fail() to fail the step (and the job) in addition to this listener
// See BatchStatus vs ExitStatus: https://docs.spring.io/spring-batch/4.0.x/reference/html/step.html#batchStatusVsExitStatus
.listener(new NoWorkFoundStepExecutionListener())
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step())
.on("FAILED").fail()
.build()
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
JobParameters jobParameters = new JobParametersBuilder()
.addString("name", "foo")
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
System.out.println(jobExecution.getStepExecutions().iterator().next().getSummary());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment