Spring Boot Technical Discussions - Pt 2

BatchConfiguration.java

I am comparatively new to the Spring Boot paradigm. I've only been working with it for a little over 2 years. That time is typical developer time and as a team lead I develop less than the average developer. I say this because my two years has not been heavily Spring Boot development.

My division within USAA does an awful lot of Spring Batch development and that makes sense given that financial institutions perform the vast majority of their work after hours. Seriously, what systems can handle billions of transactions in the middle of the business day? Batch jobs are perfect for that in the off peak hours when demand on the systems is lower.

When I was first introduced to Spring Batch I wrote a fairly simple system monitor. It basically checked something, went back to sleep, woke up later and checked again. It really wasn't a perfect example of a batch application but there were aspects of Spring Batch that it needed. For non-disclosure agreement (NDA) considerations, I won't go into it in greater depth.

After I'd worked with Spring Batch for awhile I realized there was no logical equivalent in J2EE at that time (I include JEE in this grouping). As I look back I realize that this should have been one of the first API's (after JDBC) that should have been provided, rather than servlets and the extensive web infrastructure. After all, the batch API is fairly simple and its use case is extensive across many industries. Oh well, that's the way things go.

So enough griping, where am I going? Well the batch paradigm supports a fairly simple idea that surrounds fault tolerant reading, writing and 'rithmetic which is really processing but that doesn't fit the cliche'. Nice & simple... More code in the imports than the actual code.

package org.jmresler.spring.batch.config;

import java.util.*;
import java.util.stream.IntStream;

import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.*;
import org.springframework.batch.item.*;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableBatchProcessing
@Configuration
@SpringBootApplication
public class BatchConfiguration {

    @Autowired
    private JobBuilderFactory jobFactory;

    @Autowired
    private StepBuilderFactory stepFactory;

    @Bean
    protected Job startJob() {
        return jobFactory.get("job").start(startStep()).build();
    }

    @Bean
    protected Step startStep() {
        return stepFactory.get("start-step")
                .<String, String>chunk(100)
                .reader(itemReader())
                .processor(itemProcessor())
                .writer(itemWriter())
                .build();
    }

    @Bean
    protected ItemReader<String> itemReader() {
        return new ListItemReader<>(stringList());
    }

    @Bean
    protected ItemWriter<String> itemWriter() {
        return (list) -> list.forEach(System.out::println);
    }

    @Bean
    protected ItemProcessor<String, String> itemProcessor() {
        return item -> item + "-" + System.lineSeparator();
    }

    public List<String> stringList() {
        List<String> ints = new ArrayList<>();
        IntStream.range(0, 1_000).forEach(i -> ints.add(String.valueOf(i)));
        return ints;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(BatchConfiguration.class, args);
    }
}

        

Comments

Popular posts from this blog

Back To Technology Discussions

Simple Spring Boot Technical Answers