Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:41
Show Gist options
  • Save fmbenhassine/d23fcd0b0833111a2998a03cc043af36 to your computer and use it in GitHub Desktop.
Save fmbenhassine/d23fcd0b0833111a2998a03cc043af36 to your computer and use it in GitHub Desktop.
Vanilla Spring Batch hello world job using an in-memory database, configured with XML and packaged in a standalone jar #SpringBatch
$>git clone https://gist.github.com/d23fcd0b0833111a2998a03cc043af36.git hello-world-job
$>cd hello-world-job
$>mkdir -p src/main/java && mv GreetingTasklet.java src/main/java
$>mkdir -p src/main/resources && mv hello-world-job.xml src/main/resources
$>mvn package
$>java -cp target/hello-world-job-1.0-SNAPSHOT.jar \
  org.springframework.batch.core.launch.support.CommandLineJobRunner \
  hello-world-job.xml \
  helloWorldJob \
  name=foo
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class GreetingTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
Object name = chunkContext.getStepContext().getJobParameters().get("name");
System.out.println("hello " + name);
return RepeatStatus.FINISHED;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<batch:job id="helloWorldJob">
<batch:step id="step1">
<batch:tasklet>
<bean class="GreetingTasklet"/>
</batch:tasklet>
</batch:step>
</batch:job>
</beans>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.benas</groupId>
<artifactId>hello-world-job</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment