Question

I'm trying to make a batch using javax.batch 1.0 with Java SE 7 and Guice.

Reading the JavaEE 7 documentation about JSR-352 they obviously set-up the code to be executed within Glassfish and I found no mention about how to run it outside.

Now my problem is about JobContext interface which should be automatically injected through CDI: how to make it with Guice? Trying something like:

@Provides
@Inject
JobContext providesJobContext(Provider<JobContext> provider) {
    return provider.get()
}

is conceptually wrong and it leads to StackOverflowError (so here I am :>). Is it really necessary? Examples inject it to reach Job properties. Could I reach them, maybe, through

jobOperator.getParameters(<executionID>)

?

Thank you.

I never used Weld (which, as I understand, would provide CDI on Java SE) so the only alternative would be Spring Batch.

Was it helpful?

Solution

API explanation

First, to clarify between "job properties" and "job parameters".

The "job parameters" are the Properties passed to the JobOperator#start(String,Properties) and the JobOperator#restart(long,Properties).

The "job properties" accessible via JobContext#getProperties() are the children of the /job/properties element.

E.g. this job has two properties in the object returned by: JobContext#getProperties()

<job>
   <properties>
    <property name="initialAmount" value="100" />
    <property name="bonusAmount" value="#{jobParameters['bonusAmount']}?:100;" />
   </properties>
</job>

So a job parameter is just one possible source of values for a job property definition.

As far as accessing parameters, JobOperator.getParameters(long executionId), this probably doesn't help you much since the most natural way to get the current execution id is from the very same context you're having trouble injecting.

SE with Guice

It sounds like you're referring to the JSR 352 reference implementation (jbatch, included in Glassfish).

It's true that we didn't give much thought to SE with Guice dependency injection, and I'm not enough of an expert to know if that even helps to mention.

With Weld, in Glassfish we do actually use a technique like this to allow the batch runtime to populate the batch-defined injection points.

public class BatchProducerBean {
...
@Produces
@Dependent
public JobContext getJobContext() {
    ...
        return JobContextImpl;
    }
}

I'd be happy to hear more about Guice integration with jbatch. You could follow up at the home for the project source (no real Wiki yet) on GitHub

OTHER TIPS

Weld is the reference implementation of the JSR-299 which brings CDI in JavaEE 6. This reference implementation is included in Glassfish so you can effectively use it.

Example from Oracle found here:

@Named
public class SimpleItemReader
       extends AbstractItemReader {

       @Inject
       private JobContext jobContext; 
       ...
}

Quote:

I never used Weld

Here the implementation doesn't matter because, it just implements the specification of the JSR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top