Reputation: 12803
I wonder how to pass parameter to bean.xml.
If I write like this in bean.xml, it works as expected
<bean id="notificationReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS ='A'" />
<property name="rowMapper">
<bean name = "campaignMapper" class="rh.com.app.domain.AgingMapper">
</bean>
</property>
</bean>
But if I write like this, I get error
<property name="sql" value="SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS = #{jobParameters['edcbatchStatus']}" />
My bean.xml
<task:scheduled-tasks>
<task:scheduled ref="agingScheduler" method="run" cron="*/5 * * * * *" /><!--0 0 5 * * *-->
</task:scheduled-tasks>
<!-- class = bean-->
<bean id="agingScheduler" class="rh.com.ap.AgingScheduler">
<property name="jobLauncher" ref="jobLauncher" />
<property name="agingJob" ref="agingJob" />
<property name="mailClient" ref="mailClient" />
</bean>
<batch:job id="agingJob">
<batch:step id="step1" next = "emailFile" >
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="notificationReader" writer="notificationWriter" processor="notificationProcessor" commit-interval="10" />
</batch:tasklet>
</batch:step>
<batch:step id="emailFile">
<batch:tasklet ref="emailTasklet" />
</batch:step>
<batch:listeners>
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
AgingScheduler
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
builder.addString("fileName", "AgingReporting_" + PropertiseUtil.settlementDateyyyyMMdd());
builder.addString("edcbatchStatus","A").toJobParameters();
Error
Job failed with following exceptions
exception :Failed to initialize the reader
Upvotes: 2
Views: 1498
Reputation: 36
PS the reason your code doesn't work is that Spring thinks #{jobParameters['edcbatchStatus']} is part of the SQL and doesn't interpret it. If you pass it as the complete value of a field it should work, assuming the syntax is correct.
Upvotes: 0
Reputation: 36
Hello John,
You could create a subclass of JdbcCursorItemReader where you set #{jobParameters['edcbatchStatus']} as a seperate parameter. And then use Springs InitializingBean to set the Sql property. How about something like this?
class EdcBatchStatusItemReader extends org.springframework.batch.item.database.JdbcCursorItemReader implements
org.springframework.beans.factory.InitializingBean {
protected String batchStatus;
public void getBatchStatus(String batchStatus) {
this.batchStatus = batchStatus;
}
public void afterPropertiesSet() {
setSql("SELECT r.EDCBATCH_OPEN_DATETIME As openDate FROM rev_acq_edcbatch r WHERE r.EDCBATCH_STATUS ='" + batchStatus + "'");
}
}
and then leave sql out of the bean definition and use setBatchStatus instead:
<bean id="notificationReader" class="EdcBatchItemReader" scope="step">
<property name="dataSource" ref="dataSource" />
<property name="batchStatus" value="#{jobParameters['edcbatchStatus']}" />
<property name="rowMapper">
<bean name = "campaignMapper" class="rh.com.app.domain.AgingMapper">
</bean>
</property>
</bean>
Best wishes Marc
Upvotes: 2