user9873999
user9873999

Reputation: 311

Access job parameter in ItemReader (spring-batch using grails)

I am launching a job from my service and code look like

def jobParameters = new JobParametersBuilder()
                .addDate('toDate',toDate)
                .addDate('fromDate',fromDate)
def jobEx = jobLauncher.run(billProcessJob,jobParameters.toJobParameters())

And it is executing successfully. But I need to access above job parameter in my Item Reader. My Item Reader looks like

class MyDomainMapper implements FieldSetMapper {
def mapLine(FieldSet fs) {
    if(!fs) {
         return null
    }
log.debug('Record:'+fs);//Printing the file record successfully

//Here I need to access my job parameter to map with some domain
}
}

Can any body tell me how I can achieve it? Thanks

Upvotes: 0

Views: 5965

Answers (1)

Benoit Wickramarachi
Benoit Wickramarachi

Reputation: 6216

You can define a bean in your spring-batch context:

<bean id="executionContext" class="com.xxx.ExecutionContextImpl" scope="step" >
    <property name="toDate" value="#{jobParameters['toDate']}"  />
    <property name="fromDate" value="#{jobParameters['fromDate']}"  />
</bean>

And then inject the executionContext in your ItemReader to have acces to your job parameters variables.

Upvotes: 2

Related Questions