JSR-352 支持

JSR-352 支持

从 Spring Batch 3.0 开始,对 JSR-352 的支持已完全实现。本节不能替代 规范本身,而是旨在解释 JSR-352 特定概念如何应用于 Spring Batch。 有关 JSR-352 的其他信息,请参见 JCP 在这里: https://jcp.org/en/jsr/detail?id=352spring-doc.cadn.net.cn

关于 Spring Batch 和 JSR-352 的一般说明

Spring Batch 和 JSR-352 在结构上是相同的。他们都有由步骤组成的工作。他们 两者都具有 readers、processors、writer 和 listener。然而,他们的互动却略有不同。 例如,org.springframework.batch.core.SkipListener#onSkipInWrite(S item, Throwable t)在 Spring Batch 中接收两个参数:跳过的项和导致 跳。相同方法的 JSR-352 版本 (javax.batch.api.chunk.listener.SkipWriteListener#onSkipWriteItem(List<Object> items, Exception ex)) 还接收两个参数。但是,第一个是List的所有项目 在当前 chunk 中,第二个是Exception这导致了跳跃。 由于这些差异,请务必注意,执行作业有两种路径 Spring Batch:传统的 Spring Batch 作业或基于 JSR-352 的作业。虽然使用 Spring Batch 工件(读取器、写入器等)将在使用 JSR-352 的 JSL 配置的作业中工作,并使用JsrJobOperator,它们将按照 JSR-352 的规则运行。它也是 需要注意的是,针对 JSR-352 接口开发的批处理工件将不起作用 在传统的 Spring Batch 作业中。spring-doc.cadn.net.cn

设置

应用程序上下文

Spring Batch 中所有基于 JSR-352 的作业都由两个应用程序上下文组成。父上下文,即 包含与 Spring Batch 的基础架构相关的 bean,例如JobRepository,PlatformTransactionManager等,以及由配置 的 Job。父上下文通过jsrBaseContext.xml提供 通过框架。可以通过设置JSR-352-BASE-CONTEXT系统 财产。spring-doc.cadn.net.cn

JSR-352 处理器不会处理基本上下文,例如属性注入,因此 不应在此处配置需要额外处理的组件。spring-doc.cadn.net.cn

启动基于 JSR-352 的作业

JSR-352 需要一个非常简单的路径来执行批处理作业。以下代码是 执行您的第一个批处理作业:spring-doc.cadn.net.cn

JobOperator operator = BatchRuntime.getJobOperator();
jobOperator.start("myJob", new Properties());

虽然这对开发人员来说很方便,但细节决定成败。Spring Batch 引导了一点 开发人员可能想要覆盖的幕后基础设施。以下是 bootstrap 的 第一次BatchRuntime.getJobOperator()称为:spring-doc.cadn.net.cn

Bean 名称spring-doc.cadn.net.cn

默认配置spring-doc.cadn.net.cn

笔记spring-doc.cadn.net.cn

数据源spring-doc.cadn.net.cn

具有配置值的 Apache DBCP BasicDataSource。spring-doc.cadn.net.cn

默认情况下,HSQLDB 是 bootstrapped。spring-doc.cadn.net.cn

transactionManagerspring-doc.cadn.net.cn

org.springframework.jdbc.datasource.DataSourceTransactionManagerspring-doc.cadn.net.cn

引用上面定义的 dataSource Bean。spring-doc.cadn.net.cn

Datasource 初始值设定项spring-doc.cadn.net.cn

这被配置为执行通过batch.drop.scriptbatch.schema.script性能。由 default,则执行 HSQLDB 的 schema 脚本。可以通过设置batch.data.source.init财产。spring-doc.cadn.net.cn

jobRepository 的spring-doc.cadn.net.cn

基于 JDBC 的SimpleJobRepository.spring-doc.cadn.net.cn

JobRepository使用前面提到的数据源和交易 经理。架构的表前缀BATCH_可通过batch.table.prefix财产。spring-doc.cadn.net.cn

jobLauncher 的spring-doc.cadn.net.cn

org.springframework.batch.core.launch.support.SimpleJobLauncherspring-doc.cadn.net.cn

用于启动作业。spring-doc.cadn.net.cn

batchJobOperatorspring-doc.cadn.net.cn

org.springframework.batch.core.launch.support.SimpleJobOperatorspring-doc.cadn.net.cn

JsrJobOperator包装 this 以提供它的大部分功能。spring-doc.cadn.net.cn

jobExplorerspring-doc.cadn.net.cn

org.springframework.batch.core.explore.support.JobExplorerFactoryBeanspring-doc.cadn.net.cn

用于解决JsrJobOperator.spring-doc.cadn.net.cn

jobParameters转换器spring-doc.cadn.net.cn

org.springframework.batch.core.jsr.JsrJobParametersConverterspring-doc.cadn.net.cn

JSR-352 的JobParametersConverter.spring-doc.cadn.net.cn

jobRegistry (作业注册表)spring-doc.cadn.net.cn

org.springframework.batch.core.configuration.support.MapJobRegistryspring-doc.cadn.net.cn

SimpleJobOperator.spring-doc.cadn.net.cn

placeholder属性spring-doc.cadn.net.cn

org.springframework.beans.factory.config.PropertyPlaceholderConfigurespring-doc.cadn.net.cn

加载属性文件batch-${ENVIRONMENT:hsql}.properties配置 上面提到的属性。ENVIRONMENT 是 System 属性(默认为hsql) 可用于指定 Spring Batch 当前支持的任何数据库 支持。spring-doc.cadn.net.cn

以上 bean 对于执行基于 JSR-352 的作业都不是可选的。所有都可以被覆盖为 根据需要提供自定义功能。spring-doc.cadn.net.cn

依赖关系注入

JSR-352 在很大程度上基于 Spring Batch 编程模型。因此,虽然没有明确要求 正式的依赖注入实现,隐含某种 DI。Spring Batch 支持所有这三种 加载 JSR-352 定义的批处理工件的方法:spring-doc.cadn.net.cn

  • 特定于实现的加载器:Spring Batch 基于 Spring 构建,因此支持 JSR-352 批处理作业中的 Spring 依赖注入。spring-doc.cadn.net.cn

  • 归档加载器:JSR-352 定义了batch.xml提供映射的文件 在逻辑名称和类名之间。此文件必须在/META-INF/目录(如果使用了)。spring-doc.cadn.net.cn

  • 线程上下文类加载器:JSR-352 允许配置指定批处理工件 通过内联提供完全限定的类名来实现。Spring Batch 在 JSR-352 配置的作业中也支持此功能。spring-doc.cadn.net.cn

要在基于 JSR-352 的批处理作业中使用 Spring 依赖注入,包括 使用 Spring 应用程序上下文作为 bean 配置批处理工件。一旦豆子 已经定义,则 Job 可以引用它们,就像引用batch.xml文件。spring-doc.cadn.net.cn

下面的示例展示了如何在基于 JSR-352 的 XML 格式的批处理作业:spring-doc.cadn.net.cn

XML 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
                              https://www.springframework.org/schema/beans/spring-beans.xsd
                              http://xmlns.jcp.org/xml/ns/javaee
                              https://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd">

    <!-- javax.batch.api.Batchlet implementation -->
    <bean id="fooBatchlet" class="io.spring.FooBatchlet">
            <property name="prop" value="bar"/>
    </bean>

    <!-- Job is defined using the JSL schema provided in JSR-352 -->
    <job id="fooJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
        <step id="step1">
            <batchlet ref="fooBatchlet"/>
        </step>
    </job>
</beans>

下面的示例展示了如何在基于 JSR-352 的 Java 中的批处理作业:spring-doc.cadn.net.cn

Java 配置
@Configuration
public class BatchConfiguration {

    @Bean
    public Batchlet fooBatchlet() {
        FooBatchlet batchlet = new FooBatchlet();
        batchlet.setProp("bar");
        return batchlet;
       }
}


<?xml version="1.0" encoding="UTF-8"?>
<job id="fooJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <step id="step1" >
        <batchlet ref="fooBatchlet" />
    </step>
</job>

Spring 上下文的组装(导入等)与 JSR-352 作业一起工作,就像与任何其他作业一样 基于 Spring 的应用程序。与基于 JSR-352 的作业的唯一区别是 context 定义将是 /META-INF/batch-jobs/ 中的作业定义。spring-doc.cadn.net.cn

要使用线程上下文类加载器方法,您需要做的就是提供完全限定的类 name 作为 ref。需要注意的是,当使用此方法或batch.xml方法,类 referenced 需要一个无参数的构造函数,该构造函数将用于创建 bean。spring-doc.cadn.net.cn

<?xml version="1.0" encoding="UTF-8"?>
<job id="fooJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <step id="step1" >
        <batchlet ref="io.spring.FooBatchlet" />
    </step>
</job>

Batch 属性

物业支持

JSR-352 允许在 Job、Step 和 batch 工件级别定义属性,方法是 配置。Batch 属性按以下方式在每个级别进行配置:spring-doc.cadn.net.cn

<properties>
    <property name="propertyName1" value="propertyValue1"/>
    <property name="propertyName2" value="propertyValue2"/>
</properties>

Properties可以在任何 Batch 工件上配置。spring-doc.cadn.net.cn

@BatchProperty 注释

Properties在批处理工件中引用,方法是使用@BatchProperty@Inject注释(两个注释 是规范要求的)。根据 JSR-352 的定义,属性的字段必须是 String 类型的。任何类型 conversion 由实施开发人员来执行。spring-doc.cadn.net.cn

javax.batch.api.chunk.ItemReaderartifact 可以使用 properties 块(如上文所述)并按如下方式访问:spring-doc.cadn.net.cn

public class MyItemReader extends AbstractItemReader {
    @Inject
    @BatchProperty
    private String propertyName1;

    ...
}

字段 “propertyName1” 的值将为 “propertyValue1”spring-doc.cadn.net.cn

Property Substitution

属性替换是通过运算符和简单的条件表达式提供的。一般 usage 为#{operator['key']}.spring-doc.cadn.net.cn

支持的运算符:spring-doc.cadn.net.cn

#{jobParameters['unresolving.prop']}?:#{systemProperties['file.separator']}

赋值的左侧是预期值,右侧是 default 值。在前面的 example,结果将解析为系统属性 file.separator 的值为 #{jobParameters['unresolving.prop']} 被假定为不可解析。如果两者都不是 expressions 时,将返回一个空的 String。多个条件可以是 used,它们之间用 ';' 分隔。spring-doc.cadn.net.cn

处理模型

JSR-352 提供了与 Spring Batch 相同的两种基本处理模型:spring-doc.cadn.net.cn

  • 基于项目的处理 - 使用javax.batch.api.chunk.ItemReader,则为可选的javax.batch.api.chunk.ItemProcessorjavax.batch.api.chunk.ItemWriter.spring-doc.cadn.net.cn

  • 基于任务的处理 - 使用javax.batch.api.Batchlet实现。此处理模型与org.springframework.batch.core.step.tasklet.Tasklet基于处理 当前可用。spring-doc.cadn.net.cn

基于项目的处理

在此上下文中,基于项目的处理是由ItemReader.要以这种方式配置步骤,请指定item-count(默认为 10)并选择性地配置checkpoint-policy作为 item (这是默认值)。spring-doc.cadn.net.cn

...
<step id="step1">
    <chunk checkpoint-policy="item" item-count="3">
        <reader ref="fooReader"/>
        <processor ref="fooProcessor"/>
        <writer ref="fooWriter"/>
    </chunk>
</step>
...

如果选择了基于项目的检查点,则附加属性time-limit受支持。 这将设置必须处理指定项目数的时间限制。如果 达到超时时,该块将完成已读取的项目数 那么无论item-count被配置为 be。spring-doc.cadn.net.cn

自定义检查点

JSR-352 在步骤中围绕提交间隔调用进程 “checkpointing”。 如上所述,基于项目的检查点是一种方法。但是,这并不可靠 在许多情况下已经足够了。因此,该规范允许实现自定义 checkpointing 算法,方法是实现javax.batch.api.chunk.CheckpointAlgorithm接口。此功能在功能上与 Spring Batch 的自定义完成相同 政策。要使用CheckpointAlgorithm中,使用 习惯checkpoint-policy如下所示,其中fooCheckpointer指的是 实现CheckpointAlgorithm.spring-doc.cadn.net.cn

...
<step id="step1">
    <chunk checkpoint-policy="custom">
        <checkpoint-algorithm ref="fooCheckpointer"/>
        <reader ref="fooReader"/>
        <processor ref="fooProcessor"/>
        <writer ref="fooWriter"/>
    </chunk>
</step>
...

运行作业

执行基于 JSR-352 的作业的入口是通过javax.batch.operations.JobOperator.Spring Batch 提供了自己的 此接口 (org.springframework.batch.core.jsr.launch.JsrJobOperator).这 实现是通过javax.batch.runtime.BatchRuntime.启动 基于 JSR-352 的批处理作业的实现方式如下:spring-doc.cadn.net.cn

JobOperator jobOperator = BatchRuntime.getJobOperator();
long jobExecutionId = jobOperator.start("fooJob", new Properties());

上面的代码执行以下作:spring-doc.cadn.net.cn

  • 引导一个基础ApplicationContext:为了提供批处理功能, 框架需要一些基础设施引导。每个 JVM 发生一次。这 引导的组件类似于@EnableBatchProcessing.具体详细信息可以在 javadoc 中找到JsrJobOperator.spring-doc.cadn.net.cn

  • 加载一个ApplicationContext对于请求的作业:在示例中 在上面,框架在 /META-INF/batch-jobs 中查找名为 fooJob.xml 的文件,并加载一个 context 的 Context,它是前面提到的共享上下文的子级。spring-doc.cadn.net.cn

  • Launch the job:在上下文中定义的作业将异步执行。 这JobExecution’s将返回 ID。spring-doc.cadn.net.cn

所有基于 JSR-352 的批处理作业都是异步执行的。spring-doc.cadn.net.cn

什么时候JobOperator#start使用SimpleJobOperator中,Spring Batch 确定 该调用是初始运行或以前执行的运行的重试。使用 JSR-352 基于JobOperator#start(String jobXMLName, Properties jobParameters)、框架 将始终创建一个新的 JobInstance (JSR-352 作业参数是非标识的)。为了 重新启动作业,对JobOperator#restart(long executionId, Properties restartParameters)是必需的。spring-doc.cadn.net.cn

上下文

JSR-352 定义了两个上下文对象,用于与作业或步骤的元数据进行交互 在 Batch 工件中:javax.batch.runtime.context.JobContextjavax.batch.runtime.context.StepContext.这两者都可以在任何步骤中使用 级别工件 (Batchlet,ItemReader等)与JobContext也可用于作业级工件 (JobListener例如)。spring-doc.cadn.net.cn

要获取对JobContextStepContext在当前范围内,只需使用@Inject注解:spring-doc.cadn.net.cn

@Inject
JobContext jobContext;
JSR-352 上下文的 @Autowire

不支持使用 Spring 的 @Autowire 来注入这些上下文。spring-doc.cadn.net.cn

在 Spring Batch 中,JobContextStepContext将他们的 相应的执行对象 (JobExecutionStepExecution分别)。数据存储方式StepContext#setPersistentUserData(Serializable data)存储在 Spring BatchStepExecution#executionContext.spring-doc.cadn.net.cn

阶梯流

在基于 JSR-352 的作业中,步骤流程的工作方式与在 Spring Batch 中类似。 但是,存在一些细微的差异:spring-doc.cadn.net.cn

  • 决策是步骤 - 在常规的 Spring Batch 作业中,决策是一种没有 拥有独立的StepExecution或任何权利和 伴随着完整的步骤的责任..但是,在 JSR-352 中,决策 是一个步骤,并且其行为与任何其他步骤一样(事务性、 它得到一个StepExecution等)。这意味着他们被视为 与 Restarts 上的任何其他步骤相同。spring-doc.cadn.net.cn

  • next属性和步骤转换 - 在常规作业中,这些是 允许在同一步骤中一起出现。JSR-352 允许它们在 与下一个属性在评估中优先的步骤相同。spring-doc.cadn.net.cn

  • 过渡元素排序 - 在标准 Spring Batch 作业中,过渡元素是 从最具体到最不具体排序,并按此顺序进行评估。JSR-352 作业 按照 XML 中指定的顺序评估过渡元素。spring-doc.cadn.net.cn

扩展 JSR-352 批处理作业

传统的 Spring Batch 作业有四种扩展方式(最后两种能够跨 multiple JVMs):spring-doc.cadn.net.cn

JSR-352 提供了两个用于扩展批处理作业的选项。这两个选项都仅支持单个 JVM:spring-doc.cadn.net.cn

分区

从概念上讲,JSR-352 中的分区与 Spring Batch 中的分区相同。提供元数据 向每个 worker 报告要处理的输入,并由 worker 向 manager 报告 完成时的结果。但是,存在一些重要的差异:spring-doc.cadn.net.cn

  • 分区Batchlet- 这将运行 配置Batchlet在多个线程上。每个实例都将具有 它自己的属性集由 JSL 或PartitionPlanspring-doc.cadn.net.cn

  • PartitionPlan- 使用 Spring Batch 的分区,一个ExecutionContext为每个分区提供。在 JSR-352 中,一个 单javax.batch.api.partition.PartitionPlan提供了 数组的Properties为每个分区提供元数据。spring-doc.cadn.net.cn

  • PartitionMapper- JSR-352 提供了两种生成分区的方法 元数据。一种是通过 JSL (分区属性)。第二种是通过 implementation 的javax.batch.api.partition.PartitionMapper接口。 从功能上讲,此接口类似于org.springframework.batch.core.partition.support.Partitioner接口,因为它提供了一种以编程方式生成 meta-data 进行分区。spring-doc.cadn.net.cn

  • StepExecutions- 在 Spring Batch 中,分区步骤以 manager/worker 的在 JSR-352 中,会出现相同的配置。但是,worker 步骤可以 不正式StepExecutions.因此,对JsrJobOperator#getStepExecutions(long jobExecutionId)只会 返回StepExecution对于经理。spring-doc.cadn.net.cn

孩子StepExecutions仍然存在于作业存储库中并且可用 通过JobExplorer.spring-doc.cadn.net.cn

  • 补偿逻辑 - 由于 Spring Batch 实现了 使用步骤进行分区,StepExecutionListeners可用于 如果出现问题,则处理补偿逻辑。但是,由于 worker JSR-352 提供其他组件的集合,以便在 发生错误并动态设置 Exit 状态。这些组件包括:spring-doc.cadn.net.cn

Artifact 接口spring-doc.cadn.net.cn

描述spring-doc.cadn.net.cn

javax.batch.api.partition.PartitionCollectorspring-doc.cadn.net.cn

为工作程序步骤提供一种方法,将信息发送回 经理。每个工作线程有一个实例。spring-doc.cadn.net.cn

javax.batch.api.partition.PartitionAnalyzerspring-doc.cadn.net.cn

接收PartitionCollector以及由此产生的 状态。spring-doc.cadn.net.cn

javax.batch.api.partition.PartitionReducerspring-doc.cadn.net.cn

提供为分区的 步。spring-doc.cadn.net.cn

测试

由于所有基于 JSR-352 的作业都是异步执行的,因此很难确定作业何时具有 完成。为了帮助进行测试,Spring Batch 提供了org.springframework.batch.test.JsrTestUtils.此实用程序类提供 能够启动作业并重新启动作业并等待其完成。作业完成后, 相关JobExecution返回。spring-doc.cadn.net.cn