Spring Batch Get Started with IDEA

Spring Batch with IDEA

Create Spring Batch with IntelliJ.
It’s almost same as

  • Create simple job with Spring Batch
  • Use Mysql as default dataSource

Preparation

build.gradle
Prepare build.gradle for gradle project

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: "java"
apply plugin: "spring-boot"
apply plugin: "idea"

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

jar {
    baseName = "SpringBatchSample"
    version =  "0.0.1-SNAPSHOT"
}

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-batch')
    compile('mysql:mysql-connector-java:5.1.40')
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Create source folders

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/test/java

Create IntelliJ project using gradle command

gradle idea

Import Project

Just open Project from IntelliJ. No special settings.

Example

Project
|- src
|   |- main
|       |- java
|       |     |- xxxxxx(package)
|       |           |- Main.java
|       |           |- config
|       |                |- SpringBatchConfig.java 
|       |- resources
|              |- application.yml
|- build.gradle

SpringBatchConfig.java

Main.java

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class SpringBatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(new Tasklet(){
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(new Tasklet(){
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Job job(Step step1, Step step2) throws Exception {
        return jobBuilderFactory.get("job1")
                .incrementer(new RunIdIncrementer())
                .start(step1)
                .next(step2)
                .build();
    }
}

application.yml

spring:
  datasource:
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test
    username: username
    password: password

The database name is test.