注意:JSR-352、Java EE、wildfly 17.0.1、无 spring
我在 xml 中定义了以下作业:
<?xml version="1.0" encoding="UTF-8"?>
<job id="xml2json" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" version="1.0">
<flow id="flow">
<step id="step1" next="step2">
<batchlet ref="xmlRead">
</batchlet>
</step>
<step id="step2">
<batchlet ref="notificationBatchlet">
</batchlet>
</step>
</flow>
</job>
我的要求是处理作业停止,我可以在 xmlRead 中覆盖 stop() (第一步)。
问题是我还需要确保始终执行第二步,但是从我的测试来看,如果我在 XmlRead 第一批中运行以下代码:
if (shouldStop) {
log.warn("xmlRead - job stopped by user");
return FAILED.toString();
}
最终作业的退出状态将为 FAILED(正确!),但第二步永远不会执行。
知道如何模拟这个场景吗?
最佳答案
您可以在第一步中使用过渡元素列表,而不是 next
属性,以根据退出状态(batchlet 的返回值)指导作业的执行流程。像下面这样:
<step id = "step1">
<batchlet ref="xmlRead"/>
<next on = "flag1" to = "step2"/>
<stop on = "flag2"/>
<fail on = "flag3"/>
</step>
第 2 步可以从其他常见应用程序组件中观察相同的条件来决定要做什么,或者您可以将该条件保存为作业执行数据(请参阅 somehow related discussion )
我刚刚创建了一个示例批处理应用程序 batchlet-singleton.war演示批处理、单例 session Bean 和条件转换的使用。
关于Java EE 7 批处理 API (JSR-352) : it's possible to stop a single step and not all the job?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396589/