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

孤者浪人 提交于 2020-01-06 07:53:33

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13858593/access-job-parameter-in-itemreader-spring-batch-using-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!