Hello,
I've already read this thread, but I'm still not able to pass an object from a step to another.
What I want to do is to get the "parameterName" variable value in Tasklet2 (step2), but I am not able to set the context properly.
I have a simple Bean, which is as it follows:
I have also two tasklet (which are pretty similar):Code:import java.io.Serializable; public class Parameter implements Serializable { /** * */ private static final long serialVersionUID = -7567239730365058768L; private String parameterName; public void setParameter(String param){ parameterName = param; } public String getParameter(){ return parameterName; } }
Code:public class Tasklet1 implements Tasklet{ private StepExecution stepExecution; public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { ApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] { "passingParametersJob.xml" }); Parameter parameter = (Parameter) appContext.getBean("parameter"); parameter.setParameter("HELLO"); ExecutionContext stepContext = this.stepExecution.getExecutionContext(); stepContext.put("keyValue", parameter); return RepeatStatus.FINISHED; } @BeforeStep public void saveStepExecution(StepExecution stepExecution){ this.stepExecution = stepExecution; } }And this is what I've written in the XML configuration file, within the beans tag:Code:public class Tasklet2 implements Tasklet{ private Parameter parameter; public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { if(parameter!=null) System.out.print(parameter.getParameter()); else System.out.println("Missing parameter"); return RepeatStatus.FINISHED; } @BeforeStep public void retrieveInterstepData(StepExecution stepExecution){ JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); this.parameter = (Parameter) jobContext.get("keyValue"); } }
I've read the documentation, at 11.8. Passing Data to Future Steps paragraph, and I did as above, but the problem is that, executing the job, the following row in Tasklet1 class, falls in a NullpointerException:Code:<bean id="tasklet1" class="com.springbatch.test.Tasklet1"/> <bean id="tasklet2" class="com.springbatch.test.Tasklet2"/> <bean id="parameter" class="com.springbatch.test.Parameter" /> <bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener"> <property name="keys" value="keyValue" /> </bean> <batch:job id="passingParametersJob" job-repository="jobRepository"> <batch:step id="step1" next="step2"> <batch:tasklet ref="tasklet1" transaction-manager="jobRepository-transactionManager" /> <batch:listeners> <batch:listener ref="promotionListener"/> </batch:listeners> </batch:step> <batch:step id="step2"> <batch:tasklet ref="tasklet2" transaction-manager="jobRepository-transactionManager" /> </batch:step> </batch:job>
As far as I can see, the getExecutionContext() call, returns a NULL value, but I don't understand why.Code:ExecutionContext stepContext = this.stepExecution.getExecutionContext();
Could somebody help me to debug this simple piece of code please?


Reply With Quote
