Spring boot - jsr352 batch implementation - Beans not getting reference in jobxml

242 views Asked by At

I am implementing a batch application using springboot 2.4.3 + jsr352. There is a simple batchlet class(sleepybatchlet) defined. I am trying to reference it in the JSL. but It fails saying classnotfound exception when the job is started using joboperator.

sleepy-batchlet.xml:

<job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" restartable="true" version="1.0" id="sleepy-batchlet">
    <step id="step1">
        <batchlet ref="sleepyBatchlet">
            <properties>
                <property name="sleep.time.seconds" value="#{jobParameters['sleep.time.seconds']}" />
            </properties>
        </batchlet>
    </step>
</job>

Below is my batchlet class which is annotated with @Named

@Named public class SleepyBatchlet extends AbstractBatchlet{

private final static Logger logger = Logger.getLogger(SleepyBatchlet.class.getName());
private Map<ReportMetaData,byte[]> pdfMetadataMap;

/**
 * Logging helper.
 */
protected static void log(String method, Object msg) {
    System.out.println("SleepyBatchlet: " + method + ": " + msg);
    // logger.info("SleepyBatchlet: " + method + ": " + String.valueOf(msg));
}

/**
 * This flag gets set if the batchlet is stopped.  This will break the batchlet
 * out of its sleepy loop.
 */
private volatile boolean stopRequested = false;

/**
 * The total sleep time, in seconds.
 */
@Inject
@BatchProperty(name = "sleep.time.seconds")
String sleepTimeSecondsProperty;
private int sleepTime_s = 3;

@Inject
private JschFileUtil jschFileUtil;

@Override
public String process() throws Exception {

    log("process", "entry");
    System.out.println("Test");
    return "exitStatus";
}

/**
 * Called if the batchlet is stopped by the container.
 */
@Override
public void stop() throws Exception {
    log("stop:", "");
    stopRequested = true;
}

}

Defined the bean in java configuration class as well.

@Autowired
private SleepyBatchlet sleepyBatchlet;
@Bean
public Batchlet fooBatchlet() {
    return sleepyBatchlet;
}
 

But for some reason, Its not getting referenced in the JSL. Can someone please suggest what needs to be done to use the bean created already ?

1

There are 1 answers

4
Mahmoud Ben Hassine On

I am trying to reference it in the JSL. but It fails saying classnotfound exception when the job is started using joboperator.

This is because you are referring to the class by its name and not its fully qualified name.

I added sample springboot+jsr352 application here . github.com/MekalaJ/demo

In your example, you need to update your step definition as follows:

        <batchlet ref="com.example.demo.batch.SleepyBatchlet">
            <properties>
                <property name="sleep.time.seconds" value="#{jobParameters['sleep.time.seconds']}" />
            </properties>
        </batchlet>