我是Spring Batch的新手,相信我那天我读了很多关于它的内容,以尝试熟悉它的概念。我对JobInstance
,RunIdIncrementer
,JobParamaters
的工作方式有些困惑,我想了解一些方面:
JobInstance
表中已经有BATCH_JOB_INSTANCE
名称时,作业不会启动。那么,为我的JobInstance
生成新名称的最佳方法是什么?编辑:请参见下面的代码
@Bean
public Job batchExecution() {
return jobs
.get("BatchJob")
.incrementer(new JobIdIncrementer())
.start(downloadFile())
.next(archiveFile())
.next(readFile())
.build();
}
JobIdIncrementer:
public class JobIdIncrementer implements JobParametersIncrementer {
private static String RUN_ID_KEY = "run.id";
private String key;
public JobIdIncrementer() {
this.key = RUN_ID_KEY;
}
public void setKey(String key) {
this.key = key;
}
@Override
public JobParameters getNext(JobParameters parameters) {
JobParameters params = parameters == null ? new JobParameters() : parameters;
long id = new Date().getTime();
return (new JobParametersBuilder(params)).addLong(this.key, Long.valueOf(id)).toJobParameters();
}
}
当我第一次开始批处理时,我有此日志(工作正常):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.RELEASE)
"2018-08-08 15:36:03 - Starting Application on MA18-012.local with PID 39543
""2018-08-08 15:36:05 - HikariPool-1 - Starting...
""2018-08-08 15:36:05 - HikariPool-1 - Start completed.
""2018-08-08 15:36:06 - HHH000204: Processing PersistenceUnitInfo [
name: default
...]
""2018-08-08 15:36:06 - HHH000412: Hibernate Core {5.2.14.Final}
""2018-08-08 15:36:06 - HHH000206: hibernate.properties not found
""2018-08-08 15:36:06 - HHH80000001: hibernate-spatial integration enabled : true
""2018-08-08 15:36:06 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
""2018-08-08 15:36:06 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQL5SpatialDialect
""2018-08-08 15:36:06 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect
""2018-08-08 15:36:09 - Started Application in 6.294 seconds (JVM running for 6.812)
""2018-08-08 15:36:09 - Loading the file name
""2018-08-08 15:36:23 - Downloading the file
""2018-08-08 15:36:24 - Archiving the file
""2018-08-08 15:36:24 - Unzipping the file
""2018-08-08 15:36:24 - Removing the file
""2018-08-08 15:36:51 - Reading the file
""2018-08-08 15:36:52 - HHH000397: Using ASTQueryTranslatorFactory
""2018-08-08 15:36:54 - HikariPool-1 - Shutdown initiated...
""2018-08-08 15:36:54 - HikariPool-1 - Shutdown completed.
第二次启动批处理时,我有这个(没有错误,但是它立即启动并关闭):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.RELEASE)
"2018-08-08 15:38:28 - Starting Application on MA18-012.local with PID 39638
""2018-08-08 15:38:28 - No active profile set, falling back to default profiles: default
""2018-08-08 15:38:30 - HikariPool-1 - Starting...
""2018-08-08 15:38:30 - HikariPool-1 - Start completed.
""2018-08-08 15:38:30 - HHH000204: Processing PersistenceUnitInfo [
name: default
...]
""2018-08-08 15:38:30 - HHH000412: Hibernate Core {5.2.14.Final}
""2018-08-08 15:38:30 - HHH000206: hibernate.properties not found
""2018-08-08 15:38:30 - HHH80000001: hibernate-spatial integration enabled : true
""2018-08-08 15:38:30 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
""2018-08-08 15:38:31 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQL5SpatialDialect
""2018-08-08 15:38:31 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect
""2018-08-08 15:38:33 - Started Application in 6.376 seconds (JVM running for 6.873)
""2018-08-08 15:38:34 - HikariPool-1 - Shutdown initiated...
""2018-08-08 15:38:34 - HikariPool-1 - Shutdown completed.
答案 0 :(得分:0)
当您运行Job且JobInstance名称已经在BATCH_JOB_INSTANCE表中时,将不会启动Job。那么,为JobInstance生成新名称的最佳方法是什么?
您不需要每次都更改作业名称。您的作业可以有多个作业实例,每个实例由一个键标识,该键是用于运行您的作业的(标识)作业参数的哈希值。
当我要开始工作时始终生成一个新名称是一种好习惯吗?
否,您不需要生成新名称(这是同一工作,因此名称不应更改)。您需要做的是每次通过指定不同的作业参数来创建一个新的作业实例。
应将作业安排为多次运行。创建批处理(作业)以计划运行多次而不生成新名称的最佳实践是什么?
根据您的工作频率,您可以将日期添加为工作参数。例如,如果您的作业计划为每天运行,则当前日期是一个很好的参数。检查文档的这一部分:https://docs.spring.io/spring-batch/4.0.x/reference/html/domain.html#job提供了一个示例。
RunIdIncrementer()是否应该创建一个ID来生成新的JobName?
RunIdIncrementer
将使run.id
作业参数递增,因此您将获得一个JobParameters
的新实例,这将导致一个新的作业实例。但是工作名称将保持不变。运作方式如下:https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java#L44