|
| 1 | +package org.example.Config; |
| 2 | + |
| 3 | +import io.github.resilience4j.bulkhead.BulkheadConfig; |
| 4 | +import io.github.resilience4j.bulkhead.ThreadPoolBulkheadConfig; |
| 5 | +import io.github.resilience4j.bulkhead.annotation.Bulkhead; |
| 6 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; |
| 7 | +import io.github.resilience4j.timelimiter.TimeLimiterConfig; |
| 8 | +import org.example.component.TimeoutListener; |
| 9 | +import org.springframework.batch.core.*; |
| 10 | +import org.springframework.batch.core.configuration.annotation.*; |
| 11 | +import org.springframework.batch.core.job.builder.*; |
| 12 | +import org.springframework.batch.core.launch.JobLauncher; |
| 13 | +import org.springframework.batch.core.launch.support.*; |
| 14 | +import org.springframework.batch.core.repository.JobRepository; |
| 15 | +import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; |
| 16 | +import org.springframework.batch.core.step.builder.*; |
| 17 | +import org.springframework.batch.repeat.RepeatStatus; |
| 18 | +import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4jBulkheadProvider; |
| 19 | +import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory; |
| 20 | +import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder; |
| 21 | +import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4jBulkheadProvider; |
| 22 | +import org.springframework.cloud.client.circuitbreaker.Customizer; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 26 | +import org.springframework.transaction.PlatformTransactionManager; |
| 27 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 28 | +import javax.sql.DataSource; |
| 29 | +import java.time.Duration; |
| 30 | + |
| 31 | +@Configuration |
| 32 | +@EnableBatchProcessing |
| 33 | +@EnableTransactionManagement |
| 34 | +public class BatchConfig { |
| 35 | + |
| 36 | + @Bean |
| 37 | + public Job demoJob(JobRepository jobRepository, Step demoStep) { |
| 38 | + return new JobBuilder("demoJob", jobRepository) |
| 39 | + .listener(new TimeoutListener()) |
| 40 | + .start(demoStep) |
| 41 | + .build(); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + public Step demoStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { |
| 46 | + return new StepBuilder("demoStep", jobRepository) |
| 47 | + .tasklet((contribution, chunkContext) -> { |
| 48 | + for(int i=0; i<5000; i++) { |
| 49 | + System.out.println("Processing step " + (i + 1) + "/5"); |
| 50 | + Thread.sleep(1000); // Simulate work |
| 51 | + } |
| 52 | + System.out.println(">>> Running batch job with REST trigger <<<"); |
| 53 | + return RepeatStatus.FINISHED; |
| 54 | + }, transactionManager).allowStartIfComplete(true) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception { |
| 60 | + JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); |
| 61 | + factory.setDataSource(dataSource); |
| 62 | + factory.setTransactionManager(transactionManager); |
| 63 | + factory.afterPropertiesSet(); |
| 64 | + return factory.getObject(); |
| 65 | + } |
| 66 | + |
| 67 | + @Bean |
| 68 | + public JobLauncher jobLauncher(JobRepository jobRepository) throws Exception { |
| 69 | + TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher(); |
| 70 | + jobLauncher.setJobRepository(jobRepository); |
| 71 | + |
| 72 | + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
| 73 | + executor.setCorePoolSize(2); |
| 74 | + executor.setMaxPoolSize(2); |
| 75 | + executor.setQueueCapacity(2); |
| 76 | + executor.setThreadNamePrefix("batch-job-"); |
| 77 | + executor.initialize(); |
| 78 | + |
| 79 | + jobLauncher.setTaskExecutor(executor); |
| 80 | + jobLauncher.afterPropertiesSet(); |
| 81 | + return jobLauncher; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() { |
| 86 | + return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id) |
| 87 | + .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()) |
| 88 | + .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()) |
| 89 | + .build()); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public Customizer<ReactiveResilience4jBulkheadProvider> reactiveSpecificBulkheadCustomizer() { |
| 94 | + return provider -> provider.configure(builder -> { |
| 95 | + builder.bulkheadConfig(BulkheadConfig.custom() |
| 96 | + .maxConcurrentCalls(2) |
| 97 | + .build()); |
| 98 | + }, "serviceBulkhead"); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +} |
0 commit comments