2
votes

I'm getting this error when I try to use StepExecutionContext, to share data between steps. I'm very noob at this framework so I hope it's an easy question, but as far I can tell, it must be on the framework since I dont use an "UnmodifiableMap" anywhere.

If someone has run into this, it can be of great help to share the solution, thanks in advance.

The bean is defined like:

<bean id="Candidatos_itemProcessor" class="com.xxx.CandidatoProcessorTest" scope="step">
<property name="stepContext" value="#{stepExecutionContext}"/>

The implementation of the processor is:

public class CandidatoProcessorTest implements ItemProcessor<Candidato, Candidato> {

private ExecutionContext stepContext = null;
//other private xxx

public Candidato process(Candidato item) throws Exception {

    //some logic here
        stepContext.put("cod_situacion", cod_situacion);
        stepContext.put("des_situacion", des_situacion);
        stepContext.put("fec_situacion", fec_situacion);
    //other logic
    return item;
}

//a private aux method

public void setStepContext(ExecutionContext stepContext) {
    this.stepContext = stepContext;
}

}

And the stack trace of the error is:

SEVERE: Encountered an error executing step stepCandidatos in job XXXBatch org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.Candidatos_itemProcessor' defined in class path resource [spring/batch/jobs/jobXXX.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Collections$UnmodifiableMap' to required type 'org.springframework.batch.item.ExecutionContext' for property 'stepContext'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [org.springframework.batch.item.ExecutionContext] for property 'stepContext': no matching editors or conversion strategy found at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:343) at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:338) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:187) at com.sun.proxy.$Proxy23.process(Unknown Source) at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126) at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293) at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192) at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) at com.XXX.Test.run(Test.java:32) at com.XXX.Test.main(Test.java:15) Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Collections$UnmodifiableMap' to required type 'org.springframework.batch.item.ExecutionContext' for property 'stepContext'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [org.springframework.batch.item.ExecutionContext] for property 'stepContext': no matching editors or conversion strategy found at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:474) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511) at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ... 34 more Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.util.Collections$UnmodifiableMap] to required type [org.springframework.batch.item.ExecutionContext] for property 'stepContext': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ... 40 more jun 28, 2017 9:54:31 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: Job: [FlowJob: [name=CertSeurBatch]] completed with the following parameters: [{}] and the following status: [FAILED]

1
If I remember well, stepExecutionContext can't be wired directly using spELLuca Basso Ricci
@Luca I was following the second reply of Michael Minella here: forum.spring.io/forum/spring-projects/batch/…Greco

1 Answers

0
votes

In my case the problem was that the configuration of tasklet was

@Bean
@StepScope
public MyTasklet createTasklet(@Value("#{jobExecutionContext}") ExecutionContext executionContext)

and what solved the problem was changing it to

@Bean
@StepScope
public MyTasklet createTasklet(@Value("#{stepExecution.jobExecution.executionContext}") ExecutionContext executionContext)

It's a late answer, but hope it helps someone in the future.

Spring components versions used:

<springframework.version>5.2.12.RELEASE</springframework.version>
<spring-integration.version>5.3.4.RELEASE</spring-integration.version>
<spring-batch-admin.version>1.3.1.RELEASE</spring-batch-admin.version>
<spring-batch.version>4.2.5.RELEASE</spring-batch.version>