Enterprise Java

2 Ways of Passing Properties / Parameters in Java EE 7 Batch

When it comes to the Java EE 7 Batch Processing facility, there are 2 ways in passing properties / parameters to the chunks and batchlets. This quick guide shows you the 2 ways, which could be use very frequently when developing batch processing the Java EE 7 way.

1. Pre-Defined Properties / Parameters Before Runtime

Pre-Defined properties are properties (name value pairs) which you define before deploying the application. In other words, it is fix and static, never dynamic and the values will always stay the same when you retrieve it. This is done through the job descriptor XML file, which resides in e.g. META-INF/batch-jobs/demo-job.xml. For example:

<?xml version="1.0" encoding="UTF-8"?>
<job id="demoJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     version="1.0">
    <properties>
        <property name="staticParamName1" value="staticParamValue1" />
        <property name="staticParamName2" value="staticParamValue2" />
    </properties>
 
    <!-- Then, the rest of the steps definition -->
</job>

All that it takes it is to have each pre-defined properties placed within the <properties /> tag. After the app is deployed, these properties will be made available to the objects of ItemReader, ItemProcessor, ItemWriter and Batchlet defined in the XML file during runtime.

Here’s an example of how to retrieve the pre-defined properties / parameters during runtime.

@Dependent
@Named( "DemoReader" )
public class DemoReader extends AbstractItemReader {
    @Inject
    private JobContext jobCtx;
 
    @Override
    public void open( Serializable ckpt ) throws Exception {
 
        // Retrieve the value of staticParamName1 defined in job descriptor XML
        String staticParamValue1 = jobCtx.getProperties().getProperty( "staticParamName1" );
 
        // The rest of the implementation
    }
 
    // The rest of the overridden methods
}

The down side of this that the properties’ value will always stay the same throughout the runtime. If you need to pass a dynamic value to the batch step objects, read on…

2. Passing Properties / Parameters Dynamically During Runtime

There are situations when dynamic property / parameters values are desired during batch run. To do this, first, the properties / parameters would have to be defined and have the job operator pass to the batch job.

For example, I have a JobOperator (Singleton EJB) which will start the batch job through the method runBatchJob() with two dynamic properties / parameters to be passed to the batch job objects:

@Singleton
public class BatchJobOperator implements Serializable {
 
    public void runBatchJob() {
        Properties runtimeParameters = new Properties();
        runtimeParameters.setProperty( "dynamicPropertyName1", "dynamicPropertyValue1" );
        runtimeParameters.setProperty( "dynamicPropertyName2", "dynamicPropertyValue2" );
 
        JobOperator jo = BatchRuntime.getJobOperator();
 
        // Run the batch job with the runtimeParameters passed
        jo.start( "name-of-job-xml-file-without-dot-xml", runtimeParameters );
    }
}

Once when the application server has the jobs running, the objects involved in the job (ItemReader, ItemProcessor, ItemsWriter and Batchlet) could retrieve the properties set in runtimeParameters, but in a different way. Here’s how to do it in an ItemReader (the same goes for the rest of the batch job step objects):

@Dependent
@Named( "DemoReader" )
public class DemoReader extends AbstractItemReader {
    @Inject
    private JobContext jobCtx;
 
    @Override
    public void open( Serializable ckpt ) throws Exception {
 
        // Here's how to retrieve dynamic runtime properties / parameters
        Properties runtimeParams = BatchRuntime.getJobOperator().getParameters( jobCtx.getExecutionId() );
        String dynamicPropertyValue1 = runtimeParams.getProperty( "dynamicPropertyName1" );
        String dynamicPropertyValue2 = runtimeParams.getProperty( "dynamicPropertyName2" );
 
        // The rest of the implementation
    }
 
    // The rest of the overridden methods
}

Notice the difference, instead of getting the properties from the JobContext, the dynamic runtime defined properties has to be gotten from the BatchRuntime’s JobOperator, by passing the Job Context’s execution ID.

Hope this is useful.

Max Lam

Born and currently resides in Malaysia, a seasoned Java developer whom had held positions as Senior Developer, Consultant and Technical Architect in various enterprise software development companies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button