JobParameters from Spring Package

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; // ... snip ... @Autowired @Lazy @Qualifier(value="#{jobParameters}") public void setJobParameters(Map<String, Object> jobParameters) { this.jobParameters = 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> 
+5
source share
2 answers

Important steps for Job Parameters to work are defining a StepScope bean and make sure your reader is a component of @StepScope .

I would try the following:

First make sure the bean step is set. This is convenient for configuration using the Java configuration:

 @Configuration public class JobFrameworkConfig { @Bean public static StepScope scope() { return new StepScope(); } // jobRegistry, transactionManager etc... } 

Then make sure your bean is modified using @StepScope -nnotation (almost like in your example). @Value a @Value which is not @Lazy .

 @Component("sourceSelectionReader") @StepScope // required, also works with @Scope("step") public class SourceSelectionReaderImpl implements ItemReader<MyThing> { private final long myParam; // Not lazy, specified param name for the jobParameters @Autowired public SourceSelectionReaderImpl(@Value("#{jobParameters['myParam']}") final long myParam) { this.myParam = myParam; } // the rest of the reader... } 
+2
source

Try adding @DependsOn ("jobParameters") after @Component ("sourceSelectionReader")

-1
source

Source: https://habr.com/ru/post/1210734/


All Articles