Last active
February 9, 2025 09:05
-
-
Save fmbenhassine/e2867f483ddd96517e421e82ea2c9481 to your computer and use it in GitHub Desktop.
#SpringBatch aggregation job sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.springframework.batch.core.Job; | |
import org.springframework.batch.core.Step; | |
import org.springframework.batch.core.job.builder.JobBuilder; | |
import org.springframework.batch.core.repository.JobRepository; | |
import org.springframework.batch.core.step.builder.StepBuilder; | |
import org.springframework.batch.item.*; | |
import org.springframework.batch.item.support.IteratorItemReader; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.support.JdbcTransactionManager; | |
import java.util.function.Consumer; | |
import java.util.stream.Stream; | |
@Configuration | |
public class AggregationJobConfiguration { | |
record Metric(int value, long timestamp) { }; | |
// items | |
Stream<Metric> items = Stream.of( | |
new Metric(1, 1000), | |
new Metric(2, 2000), | |
new Metric(3, 3000), | |
new Metric(4, 4000), | |
new Metric(5, 5000), | |
new Metric(6, 6000) | |
); | |
@Bean | |
public Job job(JobRepository jobRepository, Step step) { | |
return new JobBuilder("job", jobRepository).start(step).build(); | |
} | |
@Bean | |
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) { | |
// item reader | |
IteratorItemReader<Metric> reader = new IteratorItemReader<>(items.iterator()); | |
// item processor (filter items as needed, for example select the time window and/or the range of values) | |
ItemProcessor<Metric, Metric> processor = | |
item -> (item.timestamp() > 2000 && item.timestamp() < 6000) && item.value() > 3 ? item : null; | |
// item writer | |
ItemWriter<Metric> writer = new ItemStreamWriter<>() { | |
private ExecutionContext executionContext; | |
@Override | |
public void open(ExecutionContext executionContext) throws ItemStreamException { | |
this.executionContext = executionContext; | |
} | |
@Override | |
public void write(Chunk<? extends Metric> chunk) { | |
chunk.forEach((Consumer<Metric>) metric -> { | |
// do the calculation and save it in the step's execution context for later use outside the job | |
int currentSum = executionContext.getInt("sum", 0); | |
executionContext.put("sum", currentSum + metric.value()); | |
}); | |
} | |
}; | |
return new StepBuilder("step", jobRepository) | |
.<Metric, Metric>chunk(2, transactionManager) | |
.reader(reader) | |
.processor(processor) | |
.writer(writer) | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.springframework.batch.core.JobExecution; | |
import org.springframework.batch.core.explore.JobExplorer; | |
import org.springframework.batch.item.ExecutionContext; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.ConfigurableApplicationContext; | |
@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) { | |
// run the job and get the execution | |
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args); | |
JobExplorer jobExplorer = applicationContext.getBean(JobExplorer.class); | |
JobExecution jobExecution = jobExplorer.getJobExecution(1L); | |
// get the execution context of the step | |
ExecutionContext executionContext = jobExecution.getStepExecutions().iterator().next().getExecutionContext(); | |
// get the calculation from the context | |
int sum = executionContext.getInt("sum"); | |
System.out.println("sum = " + sum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>3.4.2</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.example</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>demo</name> | |
<description>Demo project for Spring Boot</description> | |
<url/> | |
<licenses> | |
<license/> | |
</licenses> | |
<developers> | |
<developer/> | |
</developers> | |
<scm> | |
<connection/> | |
<developerConnection/> | |
<tag/> | |
<url/> | |
</scm> | |
<properties> | |
<java.version>17</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-batch</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.batch</groupId> | |
<artifactId>spring-batch-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prints: