Description
Stéphane Nicoll opened BATCH-1461 and commented
Spring Batch does not offer a way to define a custom parameter type. The reason behind it is that they are persisted in the database and require some kind of pre-defined types. Right now String, long, double and Date are supported. A first improvement would allow any batch job developer to plug ParameterMapper implementation(s). Here is a proposal for the contract of the ParameterMapper interface:
/**
* Maps the specified primitive parameter into the actual parameter object. Returns
* <tt>null</tt> if the mapper cannot handle the specified parameter, otherwise returns
* the mapped object.
*
* @param parameterName the name of the parameter
* @param parameterType the primitive type of the parameter
* @param parameterValue the primitve value of the parameter
* @return the mapped parameter or <tt>null</tt> if the primitive parameter cannot be handled
*/
Object map(String parameterName, ParameterType parameterType, Object parameterValue);
The parameter mapper is only used to manipulate a more complex object during the batch job execution instead of a primitive/raw value. It's not meant to transform the complex value to the primitive type.
The mappers could be defined on the job like this
<job id="myJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step">
<tasklet>
<chunk reader="reader"processor=" processor" writer="writer" commit-interval="10"/>
</tasklet>
</step>
<parameter-mappers>
<parameter-mapper ref="myMapper"/>
</parameter-mappers>
</job>
A default mapper implementation can be added at the end of the chain to return the actual primitive type if no custom mapper did map it.
One concrete example of this is a business object identified by an ID. The job parameter would be initially the ID but the job would actually use the actual business object while executing the batch. The ParameterType interface may need to be renamed to something more explicit since the JobParameter could hold other data types in this case.
The advantage of this approach is that it does not break command line executions for instance since the raw ID is still used as input. It is also persistent-friendly.
Affects: 2.1.0.M3