Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBatch fails to execute job on @Startup #204

Closed
tosuns opened this issue Feb 10, 2015 · 6 comments
Closed

JBatch fails to execute job on @Startup #204

tosuns opened this issue Feb 10, 2015 · 6 comments

Comments

@tosuns
Copy link

tosuns commented Feb 10, 2015

Hi,

During my testing with jbatch I observed that the batch runtime fails to execute the job if the job gets started via @PostConstruct of a singleton ejb with @Startup-Annotation.
The jobs runs fine, but (in my case) the actual batchlet gets not executed.

I think/guess the probleme is related to the following issue, which I posted last year on the glassfish issue tracker: https://java.net/jira/browse/GLASSFISH-21216

To reproduce:

1). create a simple web-app
2). create a singleton ejb:

@Singleton
@Startup
public class JobStarter {
    private static final Logger LOG = Logger.getLogger(JobStarter.class.getName());
    @PostConstruct
    protected void start() {
        LOG.log(Level.INFO,
                "simple-job with id {0} started...",
                BatchRuntime.getJobOperator().start("simple-job", null));
    }
}

3). create a simple Batchlet implementation:

@Named
public class Batchlet extends AbstractBatchlet {
    private static final Logger LOG = Logger.getLogger(Batchlet.class.getName());
    @Inject
    private JobContext jobContext;
    @Inject
    private StepContext stepContext;
    @Override
    public String process() throws Exception {
        LOG.log(Level.INFO, "Batchlet {0} of job {1} finished!",
                new Object[]{
                    stepContext.getStepName(),
                    jobContext.getJobName()
                });

        return BatchStatus.COMPLETED.name();
    }
}

4). and the simple-job.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<job 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"
     id="simple-job">           
    <step id="step-one"> 
        <batchlet ref="batchlet"/>       
    </step>   
</job>

On the console you will see the output of the ejb but not the output of the batchlet.

But if you use a @schedule annotation instead of @PostConstruct, the batchlet output gets printed.

@smillidge
Copy link
Contributor

This has been reproduced.

This is caused by the Batch job executor service checking that the application is enabled before executing the Batch task. However as the application is still deploying this check fails and the batch task is not started.

The race condition arises because the Session bean @PostConstruct is called before the application is fully deployed.

@smillidge
Copy link
Contributor

This is fixable but I'm not sure if it would work in all cases as the application may not be fully deployed before the batch job is executed.

@smillidge
Copy link
Contributor

The EJB specification doesn't state that you can use JBatch or concurrency utility within a Singleton @startup @PostConstruct method. I suppose this needs clarification from EJB spec leads. I will try to raise with them.

The spec says you can only do;

SessionContext methods: getBusinessObject, getRollbackOnly, setRollbackOnly, getTimerService, lookup, getContextData
JNDI access to java:comp/env
Resource manager access
Enterprise bean access
EntityManagerFactory access
EntityManager access
TimerService and Timer methods

@smillidge
Copy link
Contributor

Requires input from EJB spec leads

@AdamBien
Copy link

According to the spec 3.2:
"
4.3.14 Transaction Context of Session Bean Methods
The following session bean methods are invoked in the scope of a transaction determined by the trans- action attribute specified in the bean’s metadata annotations or deployment descriptor.
(...)
• A singleton session bean’s PostConstruct or PreDestroy lifecycle callback interceptor method."

In PostConstruct all injected components should be already initialized and "ready to use".

Try to run the PostConstruct method within a transaction. Beginning with Java EE 7 it is allowed to execute lifecycle methods within a transaction.

@smillidge
Copy link
Contributor

Related GlassFish JIRA https://java.net/jira/browse/GLASSFISH-21087 for concurrent tasks

@smillidge smillidge added this to the Payara Server 4.1.153 milestone May 1, 2015
smillidge added a commit that referenced this issue Jul 25, 2015
PAYARA-220 fixes #204  still submits executor task during deployment.
Cousjava added a commit to Cousjava/Payara that referenced this issue Nov 2, 2020
FISH-472 Preserve JDK selectors when deleting a JVM option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants