在ItemReader中访问作业参数(使用grails进行spring-batch) [英] Access job parameter in ItemReader (spring-batch using grails)
问题描述
我从我的服务中启动一项工作,代码如下所示:
$ b $ pre $ def jobParameters = new JobParametersBuilder
.addDate('toDate',toDate)
.addDate('fromDate',fromDate)
def jobEx = jobLauncher.run(billProcessJob,jobParameters.toJobParameters())
它正在成功执行。但我需要在我的项目阅读器中访问上面的工作参数。 My Item Reader看起来像
class MyDomainMapper实现了FieldSetMapper {
def mapLine(FieldSet fs){
if (!fs){
return null
}
log.debug('Record:'+ fs); //成功打印文件记录
//这里我需要访问我的作业参数才能映射到某个域
code
$ p >任何机构可以告诉我如何实现它?
谢谢
解决方案
您可以在您的spring-batch上下文中定义一个bean:
< bean id =executionContextclass =com.xxx.ExecutionContextImplscope =step>
< property name =toDatevalue =#{jobParameters ['toDate']}/>
< property name =fromDatevalue =#{jobParameters ['fromDate']}/>
< / bean>
然后在您的ItemReader中注入 executionContext
以访问您的工作参数变量。
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.
这篇关于在ItemReader中访问作业参数(使用grails进行spring-batch)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!