javajakarta-eejava-batch

JSR-352 Batchlet ejb nullPointerException


I have a problem with a batchlet where I want to use an EJB to populate a list.

When I start the project, glassfish flags an error:

Caught exception executing step: com.ibm.jbatch.container.exception.BatchContainerRuntimeException: java.lang.NullPointerException

Glassfish version is 4.1.1

The code of my batchlet is:

@Named
public class getPingStatusBatchlet extends AbstractBatchlet {


    private static GetPingStatus gps = new GetPingStatus();
    private List<Node> nodes = null;
    @EJB
    private NodeFacade nodeEJB;
    @Override
    public String process() throws NamingException {

        nodes = nodeEJB.findAll();

        for (Node item : nodes) {
            gps.getPingStatus(item.getIpv4());
        }
        return "COMPLETED";
    }

    @Override
    public void stop() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

I tried to run the application in debug and inspect the nodeEJB, it always keep the null value.

Have you an idea how I can use my EJB into my batchlet?

Thanks for your help

Ersch

EDIT:

the NodeFacade code:

@Stateless
public class NodeFacade extends AbstractFacade<Node> {

    @PersistenceContext(unitName = "powwoPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public NodeFacade() {
        super(Node.class);
    }

}

beans.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"/>

getNetworkStatusBatch.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<job version="1.0" id="getNetworkStatusBatch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/jobXML_1_0.xsd" xmlns="http://xmlns.jcp.org/xml/ns/javaee" >
    <step id="getNetworkStatusBatchlet">
        <batchlet ref="com.powwo.app.batchlet.getPingStatusBatchlet"/>
    </step>
</job>

myBackgroundJobManager.java:

@Singleton
@Startup
public class BackgroundJobManager {

    @Schedule(minute = "*", hour = "*", second = "*/10", persistent = false)
    public void myBackgroundJobManager() {
        BatchRuntime.getJobOperator().start("getNetworkStatusBatch", null);
    }

}

Solution

  • You need to reference the artifact from JSL by bean name (not class name).

    So you should have:

    <batchlet ref="getPingStatusBatchlet"/>

    which matches the @Named (default) value on your batchlet.

    You need this in order to load your batch artifact in Glassfish as a managed bean, and have the CDI engine perform injection of other beans.

    More info: Just for completeness, I'll mention something you already had taken care of, but someone else looking later may not have.

    You also need to make sure the batch artifact is discovered as a managed bean, which you've taken care of by using the 1.0-level beans.xml. In later levels of CDI, you could use bean discovery mode = all, which is the same thing as the 1.0 beans.xml you have, or add a "bean-defining annotation" to your batch artifact, like @Dependent).