简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM