在默认情况,如果你对 Spring Batch 不进行配置的话。

Spring Batch 将会对所有存在的作业(Job)进行执行。

考察项目:https://github.com/cwiki-us-spring/cwiki-us-spring-batch-examples 你可以 Check out 到本地后运行:

gradlew :service:bootJar

将会对项目进行编译,程序将会输出:

C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>gradlew :service:bootJar

> Configure project :
You are using one or more deprecated Asciidoctor Gradle plugin features. To help with migration run with --warning-mode=all.
Repository https://jcenter.bintray.com/ replaced by https://maven.ossez.com/repository/internal.

BUILD SUCCESSFUL in 10s
6 actionable tasks: 2 executed, 4 up-to-date
C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>

Spring Batch 在默认情况下的任务执行_jar

然后你可以运行

java -jar service/build/libs/service.jar

你会发现没有作业被执行

程序输出如下:

C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>java -jar service/build/libs/service.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2020-02-18 15:32:38.016  INFO 28560 --- [           main] com.ossez.batch.service.Application      : Starting Application on USWTWNB47141 with PID 28560 (C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-exam
ples\service\build\libs\service.jar started by yhu in C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples)
2020-02-18 15:32:38.023  INFO 28560 --- [           main] com.ossez.batch.service.Application      : No active profile set, falling back to default profiles: default
2020-02-18 15:32:40.344  INFO 28560 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-02-18 15:32:40.353  WARN 28560 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=org.hsqldb.jdbcDriver was not found, trying direct instantiation.
2020-02-18 15:32:40.854  INFO 28560 --- [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)
2020-02-18 15:32:40.866  INFO 28560 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-02-18 15:32:42.711  INFO 28560 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2020-02-18 15:32:43.137  INFO 28560 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2020-02-18 15:32:44.502  INFO 28560 --- [           main] com.ossez.batch.service.Application      : Started Application in 7.373 seconds (JVM running for 8.576)
2020-02-18 15:32:44.529  INFO 28560 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-18 15:32:44.537  INFO 28560 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>

Spring Batch 在默认情况下的任务执行_java_02

如果你希望有作业被默认执行的话,你需要修改你检出项目的 application.properties 文件

将参数

spring.batch.job.enabled=false

修改为 true

然后重新编译然后执行,你将会发现所有作业将会被默认执行了。

C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>java -jar service/build/libs/service.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2020-02-18 15:36:29.621  INFO 47168 --- [           main] com.ossez.batch.service.Application      : Starting Application on USWTWNB47141 with PID 47168 (C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-exam
ples\service\build\libs\service.jar started by yhu in C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples)
2020-02-18 15:36:29.625  INFO 47168 --- [           main] com.ossez.batch.service.Application      : No active profile set, falling back to default profiles: default
2020-02-18 15:36:31.611  INFO 47168 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-02-18 15:36:31.620  WARN 47168 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=org.hsqldb.jdbcDriver was not found, trying direct instantiation.
2020-02-18 15:36:31.978  INFO 47168 --- [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)
2020-02-18 15:36:31.984  INFO 47168 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-02-18 15:36:33.087  INFO 47168 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2020-02-18 15:36:33.493  INFO 47168 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2020-02-18 15:36:34.706  INFO 47168 --- [           main] com.ossez.batch.service.Application      : Started Application in 5.952 seconds (JVM running for 7.127)
2020-02-18 15:36:34.709  INFO 47168 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2020-02-18 15:36:34.851  INFO 47168 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=cloudClean]] launched with the following parameters: [{run.id=1}]
2020-02-18 15:36:34.884  INFO 47168 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [readFilesStep]
2020-02-18 15:36:34.957  INFO 47168 --- [           main] c.o.batch.service.tasklet.FileTasklet    : >>++>1582058194905
2020-02-18 15:36:34.974  INFO 47168 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [deleteFilesStep]
2020-02-18 15:36:34.977  INFO 47168 --- [           main] c.o.batch.service.tasklet.FileTasklet    : >>++>1582058194977
2020-02-18 15:36:34.987  INFO 47168 --- [           main] .o.b.s.JobCompletionNotificationListener : !!! JOB FINISHED! Time to verify the results
2020-02-18 15:36:34.990  INFO 47168 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=cloudClean]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
2020-02-18 15:36:35.023  INFO 47168 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-18 15:36:35.030  INFO 47168 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

C:\WorkDir\Repository\Spring\cwiki-us-spring-guides\cwiki-us-spring-batch-examples>

请注意,在这里我们配置了

spring.batch.job.names=cloudClean

所以默认将会执行 cloudClean 作业。

Spring Batch 在默认情况下的任务执行_sql_03

或者你可以执行运行命令:

java -jar service/build/libs/service.jar --spring.batch.job.enabled=true --spring.batch.job.names=cloudClean

将会执行你指定的作业。