Skip to content

Commit 64549be

Browse files
Add files via upload
1 parent ff36280 commit 64549be

File tree

7 files changed

+343
-0
lines changed

7 files changed

+343
-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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example.controller;
2+
3+
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.batch.core.Job;
6+
import org.springframework.batch.core.JobParameters;
7+
import org.springframework.batch.core.JobParametersBuilder;
8+
import org.springframework.batch.core.JobParametersInvalidException;
9+
import org.springframework.batch.core.launch.JobLauncher;
10+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
11+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
12+
import org.springframework.batch.core.repository.JobRestartException;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.stereotype.Controller;
16+
import org.springframework.web.bind.annotation.GetMapping;
17+
18+
@Slf4j
19+
@Controller
20+
public class Batch {
21+
22+
23+
@Autowired
24+
private JobLauncher jobLauncher;
25+
26+
27+
@Autowired
28+
private Job demoJob;
29+
30+
31+
32+
@GetMapping("/batch")
33+
private ResponseEntity<Object> signUp() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
34+
JobParameters params = new JobParametersBuilder()
35+
.addLocalDateTime("date", java.time.LocalDateTime.now())
36+
.toJobParameters();
37+
jobLauncher.run(demoJob, params);
38+
log.info("===============================");
39+
return ResponseEntity.ok("Hello World");
40+
}
41+
42+
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.service;
2+
3+
import org.springframework.web.reactive.function.client.WebClient;
4+
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
5+
import org.springframework.stereotype.Service;
6+
import reactor.core.publisher.Mono;
7+
8+
@Service
9+
public class ExternalReactiveService {
10+
@Bulkhead(name = "serviceBulkhead", type = Bulkhead.Type.THREADPOOL)
11+
public Mono<String> fetchData() {
12+
return WebClient.create().get().uri("https://example.com").retrieve().bodyToMono(String.class);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Spring JPA
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
3+
spring.datasource.username=postgres
4+
spring.datasource.password=naveen
5+
spring.jpa.hibernate.ddl-auto=none
6+
spring.jpa.show-sql=true
7+
spring.jpa.properties.hibernate.format_sql=false
8+
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
9+
spring.servlet.multipart.enabled=true
10+
spring.servlet.multipart.max-file-size=50MB
11+
spring.servlet.multipart.max-request-size=60MB
12+
spring.output.ansi.enabled=ALWAYS
13+
management.endpoint.metrics.enabled=true
14+
management.endpoints.prometheus.enabled=true
15+
management.endpoints.web.exposure.include=*
16+
spring.batch.jdbc.initialize-schema=never

0 commit comments

Comments
 (0)