Skip to content

Commit 803ed55

Browse files
AI Trash
1 parent 7716554 commit 803ed55

File tree

17 files changed

+572
-0
lines changed

17 files changed

+572
-0
lines changed

SpringBatch/pom.xml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>SpringBatch</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
15+
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>3.5.4</version>
21+
</parent>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-batch</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-jpa</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.mapstruct</groupId>
47+
<artifactId>mapstruct</artifactId>
48+
<version>${org.mapstruct.version}</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.postgresql</groupId>
53+
<artifactId>postgresql</artifactId>
54+
<scope>runtime</scope>
55+
</dependency>
56+
57+
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-actuator</artifactId>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>jakarta.validation</groupId>
65+
<artifactId>jakarta.validation-api</artifactId>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-validation</artifactId>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.springframework.cloud</groupId>
75+
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
76+
<version>3.3.0</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>io.github.resilience4j</groupId>
81+
<artifactId>resilience4j-bulkhead</artifactId>
82+
<version>2.2.0</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-webflux</artifactId>
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-maven-plugin</artifactId>
96+
<configuration>
97+
<excludes>
98+
<exclude>
99+
<groupId>org.projectlombok</groupId>
100+
<artifactId>lombok</artifactId>
101+
</exclude>
102+
</excludes>
103+
</configuration>
104+
</plugin>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-surefire-plugin</artifactId>
108+
</plugin>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<version>3.8.1</version>
113+
<configuration>
114+
<source>${java.version}</source>
115+
<target>${java.version}</target>
116+
<encoding>${project.build.sourceEncoding}</encoding>
117+
<annotationProcessorPaths>
118+
<path>
119+
<groupId>org.projectlombok</groupId> <!-- IMPORTANT - LOMBOK BEFORE MAPSTRUCT -->
120+
<artifactId>lombok</artifactId>
121+
<version>${lombok.version}</version>
122+
</path>
123+
<path>
124+
<groupId>org.mapstruct</groupId>
125+
<artifactId>mapstruct-processor</artifactId>
126+
<version>${org.mapstruct.version}</version>
127+
</path>
128+
</annotationProcessorPaths>
129+
</configuration>
130+
</plugin>
131+
132+
</plugins>
133+
</build>
134+
135+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package org.example.Config;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.github.resilience4j.bulkhead.BulkheadConfig;
5+
import io.github.resilience4j.bulkhead.ThreadPoolBulkheadConfig;
6+
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
7+
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
8+
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
9+
import lombok.Data;
10+
import org.example.component.TimeoutListener;
11+
import org.example.dto.PersonDTO;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
import org.springframework.batch.core.*;
15+
import org.springframework.batch.core.configuration.annotation.*;
16+
import org.springframework.batch.core.job.builder.*;
17+
import org.springframework.batch.core.job.flow.Flow;
18+
import org.springframework.batch.core.launch.JobLauncher;
19+
import org.springframework.batch.core.launch.support.*;
20+
import org.springframework.batch.core.repository.ExecutionContextSerializer;
21+
import org.springframework.batch.core.repository.JobRepository;
22+
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
23+
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
24+
import org.springframework.batch.core.step.builder.*;
25+
import org.springframework.batch.item.ExecutionContext;
26+
import org.springframework.batch.repeat.RepeatStatus;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4jBulkheadProvider;
29+
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JCircuitBreakerFactory;
30+
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
31+
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4jBulkheadProvider;
32+
import org.springframework.cloud.client.circuitbreaker.Customizer;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
36+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
37+
import org.springframework.transaction.PlatformTransactionManager;
38+
import org.springframework.transaction.annotation.EnableTransactionManagement;
39+
import javax.sql.DataSource;
40+
import java.io.Serializable;
41+
import java.time.Duration;
42+
import java.util.Objects;
43+
44+
@Configuration
45+
@EnableBatchProcessing(executionContextSerializerRef = "jacksonSerializer")
46+
@EnableTransactionManagement
47+
public class BatchConfig {
48+
49+
50+
private static final Logger log = LoggerFactory.getLogger(BatchConfig.class);
51+
52+
@Bean
53+
public Job demoJob(JobRepository jobRepository, Step demoStep, Step step1, Step step2, Step step3) {
54+
// Create a flow for step1 followed by step3
55+
Flow flow1 = new FlowBuilder<Flow>("flow1")
56+
.start(step1)
57+
.next(step3)
58+
.build();
59+
60+
// Create a flow for step2 (runs in parallel with flow1)
61+
Flow flow2 = new FlowBuilder<Flow>("flow2")
62+
.start(step2)
63+
.build();
64+
65+
// Create a split that runs flow1 and flow2 in parallel
66+
Flow parallelFlow = new FlowBuilder<Flow>("parallelFlow")
67+
.split(new SimpleAsyncTaskExecutor())
68+
.add(flow1, flow2)
69+
.build();
70+
71+
return new JobBuilder("demoJob", jobRepository)
72+
.listener(new TimeoutListener())
73+
.start(demoStep)
74+
.on("COMPLETED").to(parallelFlow)
75+
.end()
76+
.build();
77+
}
78+
79+
@Autowired
80+
private ObjectMapper objectMapper;
81+
82+
@Bean
83+
public Step demoStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
84+
return new StepBuilder("demoStep", jobRepository)
85+
.tasklet((contribution, chunkContext) -> {
86+
StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
87+
JobExecution jobExecution = stepExecution.getJobExecution();
88+
89+
// Get job parameter person as JSON string
90+
String personJson = jobExecution.getJobParameters().getString("person");
91+
log.info("Person JSON: {}", personJson);
92+
93+
// Deserialize JSON to PersonDTO object
94+
PersonDTO personDTO = objectMapper.readValue(personJson, PersonDTO.class);
95+
log.info("Person DTO - Name: {}, Age: {}", personDTO.getName(), personDTO.getAge());
96+
97+
ExecutionContext jobContext = jobExecution.getExecutionContext();
98+
99+
jobContext.putString("Data", new MyCustomData("LOLLLLL<>" + jobExecution.getId() + ".txt").toString());
100+
101+
String earlyData = jobContext.getString("Data");
102+
log.info("Early Data from Job Execution Context: {}", earlyData);
103+
System.out.println(">>> Running batch job with REST trigger <<<");
104+
return RepeatStatus.FINISHED;
105+
}, transactionManager).allowStartIfComplete(true)
106+
.build();
107+
}
108+
109+
@Bean
110+
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager, org.example.service.ExternalReactiveService externalReactiveService) {
111+
return new StepBuilder("step1", jobRepository)
112+
.tasklet((contribution, chunkContext) -> {
113+
log.info("Step 1: Fetching data from external source...");
114+
String result = externalReactiveService.fetchData().block();
115+
log.info("Step 1 Result: {}", result);
116+
return RepeatStatus.FINISHED;
117+
}, transactionManager)
118+
.build();
119+
}
120+
121+
@Bean
122+
public Step step2(JobRepository jobRepository, PlatformTransactionManager transactionManager, org.example.service.ExternalReactiveService externalReactiveService) {
123+
return new StepBuilder("step2", jobRepository)
124+
.tasklet((contribution, chunkContext) -> {
125+
int page = 0;
126+
int size = 100;
127+
log.info("Step 2: Fetching data batch - Page: {}, Size: {}", page, size);
128+
String result = Objects.requireNonNull(externalReactiveService.fetchAllDataFromPath("/api/data/v1", size).collectList().block()).toString();
129+
log.info("Step 2 Result: {}", result);
130+
return RepeatStatus.FINISHED;
131+
}, transactionManager)
132+
.build();
133+
}
134+
135+
@Bean
136+
public Step step3(JobRepository jobRepository, PlatformTransactionManager transactionManager, org.example.service.ExternalReactiveService externalReactiveService) {
137+
return new StepBuilder("step3", jobRepository)
138+
.tasklet((contribution, chunkContext) -> {
139+
int page = 1;
140+
int size = 500;
141+
log.info("Step 3: Fetching data batch - Page: {}, Size: {}", page, size);
142+
String result = Objects.requireNonNull(externalReactiveService.fetchAllDataFromPath("/api/data/v2", size).collectList().block()).toString();
143+
log.info("Step 3 Result: {}", result);
144+
return RepeatStatus.FINISHED;
145+
}, transactionManager)
146+
.build();
147+
}
148+
149+
@Bean
150+
public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
151+
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
152+
factory.setDataSource(dataSource);
153+
factory.setTransactionManager(transactionManager);
154+
factory.afterPropertiesSet();
155+
return factory.getObject();
156+
}
157+
158+
@Bean
159+
public JobLauncher jobLauncher(JobRepository jobRepository) throws Exception {
160+
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
161+
jobLauncher.setJobRepository(jobRepository);
162+
163+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
164+
executor.setCorePoolSize(2);
165+
executor.setMaxPoolSize(2);
166+
executor.setQueueCapacity(2);
167+
executor.setThreadNamePrefix("batch-job-");
168+
executor.initialize();
169+
170+
jobLauncher.setTaskExecutor(executor);
171+
jobLauncher.afterPropertiesSet();
172+
return jobLauncher;
173+
}
174+
175+
@Bean
176+
public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
177+
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
178+
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build())
179+
.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
180+
.build());
181+
}
182+
183+
@Bean
184+
public Customizer<ReactiveResilience4jBulkheadProvider> reactiveSpecificBulkheadCustomizer() {
185+
return provider -> provider.configure(builder -> {
186+
builder.bulkheadConfig(BulkheadConfig.custom()
187+
.maxConcurrentCalls(2)
188+
.build());
189+
}, "serviceBulkhead");
190+
}
191+
192+
193+
@Bean
194+
public ExecutionContextSerializer jacksonSerializer() {
195+
return new Jackson2ExecutionContextStringSerializer();
196+
}
197+
198+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.Config;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NonNull;
6+
7+
import java.io.Serializable;
8+
9+
10+
@Data
11+
public class MyCustomData implements Serializable {
12+
@NonNull
13+
private String value;
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.component;
2+
3+
import org.springframework.batch.core.JobExecution;
4+
import org.springframework.batch.core.JobExecutionListener;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class TimeoutListener implements JobExecutionListener {
9+
10+
@Override
11+
public void beforeJob(JobExecution jobExecution) {
12+
System.out.println("Job is starting...");
13+
}
14+
15+
@Override
16+
public void afterJob(JobExecution jobExecution) {
17+
System.out.println("Job has ended.");
18+
}
19+
}

0 commit comments

Comments
 (0)