0

Note: JSR-352, Java EE, on wildfly 17.0.1, no spring

I have defined the following job in 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>

My requirement is to handle job stop and I can do it overriding stop() in xmlRead (first step).

The problem is I need also to be sure second step is always executed, but from my tests if I run the following code inside XmlRead first batch:

        if (shouldStop) {
            log.warn("xmlRead - job stopped by user");
            return FAILED.toString();
        }

In the end exit status for the job will be FAILED ( correct ! ) but second step is never executed.

Any idea how to model this scenario ?

Vokail
  • 630
  • 8
  • 27

1 Answers1

3

You can have a list of transition elements inside the first step, instead of the next attributem, to direct the execution flow of the job depending on the exit status (the return value from the batchlet). Somethig like the following:

<step id = "step1">
  <batchlet ref="xmlRead"/>

  <next on = "flag1" to = "step2"/>
  <stop on = "flag2"/>
  <fail on = "flag3"/>
</step>

Step2 can watch for the same condition from some other common application component to decide on what to do, or you can save the condition as job execution data (see somehow related discussion)

I've just created a sample batch app batchlet-singleton.war to demonstrate the use of batchlet, singleton session bean, and conditional transition.

cheng
  • 1,076
  • 6
  • 6
  • My requirement is to stop the job outside of the batchlet (xmlRead or notificationBatchlet). If I implement a stop method inside of xmlRead Batchlet, whole job is failed, not only a step, so any next on does not work, and second step is not executed. How to solve this ? – Vokail Apr 01 '20 at 12:50
  • There is no way to have an external client to issue a command to stop a single step without stopping the whole job. Your definition of "stopping a step" seems more like part of the normal workflow: upon certain condition, stop any work in the batchlet and continue to step2. So the current transition mechanism should already work for your case. The batch status of 1st will be COMPLETED and its exit status can be any value defined by your app. – cheng Apr 01 '20 at 17:50
  • Okay, but this condition on how to "redirect" internally a flow should be controlled from outside of the job. So at this point a viable solution could be to set from outside of the job a variable in the context, so in the step I can react on this ? – Vokail Apr 02 '20 at 14:45
  • The batchlet class can periodically check a condition inside a shared object, which can be kept in job context or step context, or to be injected with JBeret's `@JobScoped` or `@StepScoped` scopes. See the link "related discussion" in above answer section for details. – cheng Apr 02 '20 at 15:49
  • It seems tto me it would be nice to not only be able to send a stop signal to a batch job but other signals as well. Upon such a custom signal one step could be stopped - the batch itself might keep running. This might be similar to the signals you can send to processes using kill. But for that the JSR would need to be enhanced. – Queeg Oct 05 '20 at 06:35
  • For ideas to improve batch API or spec, please create issues at [batch spec project](https://github.com/eclipse-ee4j/batch-api/issues) – cheng Oct 07 '20 at 03:13