PurgeVerresBatchConfiguration.java

package com.sintia.ffl.admin.optique.catalogue.batch.config.purgeglasses;

import com.sintia.ffl.admin.optique.catalogue.batch.config.ChunkCountListener;
import com.sintia.ffl.admin.optique.catalogue.batch.reader.purgeglasses.PurgeGlassesItemReader;
import com.sintia.ffl.admin.optique.catalogue.batch.tasklet.purgeglasses.PurgeGlassesDataBackup;
import com.sintia.ffl.admin.optique.catalogue.batch.writer.purgeglasses.PurgeGlassesItemWriter;
import com.sintia.ffl.admin.optique.services.dto.Glasses;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
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.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.PassThroughItemProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Configure the bean to launch the purge of the modeleVerre entries job
 *
 * @author hlouanzi
 */
@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class PurgeVerresBatchConfiguration {

	public final JobBuilderFactory jobBuilderFactory;

	public final StepBuilderFactory stepBuilderFactory;

	public final ChunkCountListener chunkCountListener;

	private final PurgeGlassesItemReader purgeGlassesItemReader;

	private final PurgeGlassesItemWriter purgeGlassesItemWriter;

	private final PurgeGlassesDataBackup purgeGlassesDataBackup;

	@Bean
	public Job purgeVerresJob(PurgeVerresJobCompletionNotificationListener listener) {
		FlowBuilder<Flow> flowBuilder = new FlowBuilder<>("purgeVerres");

		Flow flow = flowBuilder
			.start(backupDatabase())
			.next(purgeStep())
			.build();

		return jobBuilderFactory.get("purgeVerresJob")
			.incrementer(new RunIdIncrementer())
			.listener(listener)
			.start(flow)
			.end()
			.build();
	}

	/**
	 * @return
	 */
	@Bean
	public Step backupDatabase() {
		return stepBuilderFactory.get("backup database")
			.tasklet(purgeGlassesDataBackup)
			.build();
	}

	@Bean
	public Step purgeStep() {
		return stepBuilderFactory.get("purgeStep")
			.<Glasses, Glasses> chunk(1000)
			.reader(purgeGlassesItemReader)
			.processor(new PassThroughItemProcessor<>())
			.writer(purgeGlassesItemWriter)
			.listener(chunkCountListener)
			.build();
	}

}