Spring Batch - JobParameters Example.
In this example, we will show you how to pass the JobParameter values to the Tasklet and use it ?
Please refer Spring Batch Docs and API: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/JobParameters.html
JobParametersApplication.java
@EnableBatchProcessing
@SpringBootApplication
public class JobParametersApplication implements CommandLineRunner{
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) {
SpringApplication.run(JobParametersApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("message", "MyHello")
.addDate("date", new Date())
.addLong("time",System.currentTimeMillis()).toJobParameters();
JobExecution execution = jobLauncher.run(job, jobParameters);
System.out.println("STATUS :: "+execution.getStatus());
}
}
logs:
2018-12-22 12:07:15.168 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{-spring.output.ansi.enabled=always}]
2018-12-22 12:07:15.179 INFO 1176 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
null
2018-12-22 12:07:15.203 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{-spring.output.ansi.enabled=always}] and the following status: [COMPLETED]
2018-12-22 12:07:15.207 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{message=MyHello, date=1545460635203, time=1545460635203}]
2018-12-22 12:07:15.210 INFO 1176 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
MyHello
2018-12-22 12:07:15.215 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{message=MyHello, date=1545460635203, time=1545460635203}] and the following status: [COMPLETED]
STATUS :: COMPLETED
2018-12-22 12:07:15.218 INFO 1176 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-12-22 12:07:15.219 INFO 1176 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
In this example, we will show you how to pass the JobParameter values to the Tasklet and use it ?
Please refer Spring Batch Docs and API: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/JobParameters.html
JobParametersApplication.java
@EnableBatchProcessing
@SpringBootApplication
public class JobParametersApplication implements CommandLineRunner{
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) {
SpringApplication.run(JobParametersApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("message", "MyHello")
.addDate("date", new Date())
.addLong("time",System.currentTimeMillis()).toJobParameters();
JobExecution execution = jobLauncher.run(job, jobParameters);
System.out.println("STATUS :: "+execution.getStatus());
}
}
JobConfiguration.java
@Configuration
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
@StepScope
public Tasklet helloWorldTasklet(@Value("#{jobParameters['message']}") String message) {
return (stepContribution, chukContext) -> {
System.out.println(message);
return RepeatStatus.FINISHED;
};
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet(helloWorldTasklet(null))
.build();
}
@Bean
public Job jobParametersJob() {
return jobBuilderFactory.get("jobParametersJob")
.start(step1())
.build();
}
}
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>jobParameters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jobParameters</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
2018-12-22 12:07:15.168 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{-spring.output.ansi.enabled=always}]
2018-12-22 12:07:15.179 INFO 1176 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
null
2018-12-22 12:07:15.203 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{-spring.output.ansi.enabled=always}] and the following status: [COMPLETED]
2018-12-22 12:07:15.207 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] launched with the following parameters: [{message=MyHello, date=1545460635203, time=1545460635203}]
2018-12-22 12:07:15.210 INFO 1176 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
MyHello
2018-12-22 12:07:15.215 INFO 1176 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=jobParametersJob]] completed with the following parameters: [{message=MyHello, date=1545460635203, time=1545460635203}] and the following status: [COMPLETED]
STATUS :: COMPLETED
2018-12-22 12:07:15.218 INFO 1176 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-12-22 12:07:15.219 INFO 1176 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Comments
Post a Comment