I am trying to enter work parameters into a custom ItemReader. I looked through all the StackOverflow notes on this subject (for example: How to access job parameters from ItemReader, in Spring Batch? ), And I see this is a common point of pain, which is basically not resolved. I hope Spring gurus (@Michael Minella anyone) will see this and get some insight.
I have before determining that the operating parameters are available in approximately one of 10 starts, even without changing the code or configuration. This is a case of random success, not a random failure, so itβs hard to track.
I dug out the Spring code using a debugger and decided that there would be no bean named jobParameters registered in Spring at the time of the injection.
I am using Spring 4.1.4 with spring -batch 3.0.2 and spring -data-jpa 1.7.1 and spring -data-commons 1.9.1 working in java 8.
Java class
@Component("sourceSelectionReader") @Scope("step") public class SourceSelectionReaderImpl implements ItemReader<MyThing> { private Map<String,Object> jobParameters;
Startup options:
launch-context.xml job1 jobid(long)=1
launch-context.xml (minus fluff):
<context:property-placeholder location="classpath:batch.properties" /> <context:component-scan base-package="com.maxis.maximo.ilm" /> <jdbc:initialize-database data-source="myDataSource" enabled="false"> <jdbc:script location="${batch.schema.script}" /> </jdbc:initialize-database> <batch:job-repository id="jobRepository" data-source="myDataSource" transaction-manager="transactionManager" isolation-level-for-create="DEFAULT" max-varchar-length="1000"/> <import resource="classpath:/META-INF/spring/module-context.xml" />
Module-context.xml (minus fluff):
<description>Example job to get you started. It provides a skeleton for a typical batch application.</description> <import resource="classpath:/META-INF/spring/hibernate-context.xml"/> <import resource="classpath:/META-INF/spring/myapp-context.xml"/> <context:component-scan base-package="com.me" /> <bean class="org.springframework.batch.core.scope.StepScope" /> <batch:job id="job1"> <batch:step id="step0002" > <batch:tasklet transaction-manager="transactionManager" start-limit="100" > <batch:chunk reader="sourceSelectionReader" writer="selectedDataWriter" commit-interval="1" /> </batch:tasklet> </batch:step> </batch:job>
source share