1

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);
    }

}
Ersch
  • 173
  • 2
  • 15
  • 1
    Could you please post the content of NodeFacade.java – D00de Apr 17 '17 at 19:03
  • Hi, I added the the post – Ersch Apr 17 '17 at 19:07
  • Should be injected and not evaluated to null. Could you post your project to github? Which Glassfish version are you using? – D00de Apr 17 '17 at 19:29
  • Did you try `@Inject private NodeFacade nodeEJB` instead of `@EJB private NodeFacade nodeEJB` ? Was there something you were trying to achieve by using `@EJB` specifically instead of `@Inject`? – Scott Kurz Apr 17 '17 at 23:53
  • Yes, I did. The ejb is still evaluated to null if I use @inject – Ersch Apr 18 '17 at 07:15
  • Can you paste your beans.xml and also show how you're referencing the batchlet in your job XML (JSL) ? That is, what are you using as the *ref* value? Is it by bean name? What Glassfish version are you using? – Scott Kurz Apr 18 '17 at 12:45
  • I added all informations in the original post. – Ersch Apr 18 '17 at 21:11
  • By the way, I looked my glassfish config and the Implicit CDI is Enabled so it should work without the beans.xml according : https://blogs.oracle.com/theaquarium/entry/default_cdi_enablement_in_java> – Ersch Apr 18 '17 at 22:09
  • Thanks. This works in WebSphere Liberty (using `@Inject` not `@EJB`, though). Let me look into why it isn't working in Glassfish. – Scott Kurz Apr 19 '17 at 13:21

1 Answers1

1

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).

Scott Kurz
  • 4,985
  • 1
  • 18
  • 40