CompositeReader实现在spring批处理中失败

时间:2017-10-05 20:43:55

标签: java spring spring-batch

我需要执行3个不同的查询并将结果合并发送并进行处理,最后将结果写入平面文件。

我已提到链接CompositeReader Example

以同样的方式实现了我的复合阅读器。

My Composite Reader课程。

public class FI008CreationCompositeReader
    implements ItemStreamReader<List<T>>, ItemReadListener<List<T>> {

/** Registered ItemStreamReaders. */
private List<AbstractCursorItemReader<?>> cursorItemReaders;

@Value("#{jobParameters}")
private Map<String, JobParameter> jobParameters;

@Autowired
private DaoHelper daoHelper;

@Override
public List<T> read()
        throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    // read from all registered readers
    final List<T> items = new ArrayList<>();
    int recCount = 0;
    for (final AbstractCursorItemReader<?> cursorItemReader : this.cursorItemReaders) {
        T t = new T();
        while (null != t) {
            t = (T) cursorItemReader.read();
            items.add(t);
            recCount++;

        }
    }
    final JobParameter jobParameter = new JobParameter(Integer.toString(recCount));
    this.jobParameters.put(BatchConstants.TOT_REC_CNT, jobParameter);
    return items;
}

@Override
public void open(final ExecutionContext executionContext) throws ItemStreamException {
    for (final ItemStream itemStream : this.cursorItemReaders) {
        itemStream.open(executionContext);
    }
}

@Override
public void update(final ExecutionContext executionContext) throws ItemStreamException {
    for (final ItemStream itemStream : this.cursorItemReaders) {
        itemStream.update(executionContext);
    }

}

@Override
public void close() throws ItemStreamException {
    for (final ItemStream itemStream : this.cursorItemReaders) {
        itemStream.close();
    }

}

public void setCursorItemReaders(final List<AbstractCursorItemReader<?>> cursorItemReaders) {
    this.cursorItemReaders = cursorItemReaders;
}

@Override
public void beforeRead() {

}

@Override
public void afterRead(final List<Fi008ActvityTxnDtlsVO> item) {
}

@Override
public void onReadError(final Exception ex) {
    final JobParameter eventIdParam = this.jobParameters.get(BatchConstants.EVENT_ID);
    final String eventId = (String) eventIdParam.getValue();
    // log error and update the event table
    this.daoHelper.updateActionEvent(eventId, BatchConstants.FAILURE,
            "Failed to read the records from DB for Fi008 processing.");

}

}

以下是我的XML配置

<bean id="fi008CreationCompositeReader" class="com.ent.giftcards.batch.readers.FI008CreationCompositeReader" scope="step">
    <property name="cursorItemReaders">
        <list>
            <ref bean="promoTxnReader" />
            <ref bean="overallTxnReader" />
            <ref bean="promoTxnReader" />                 
        </list>
    </property>
</bean>

当我运行我的代码时,我得到了堆栈跟踪

    [StepExecution: id=1, version=2, name=fi08step1, status=FAILED, exitStatus=FAILED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=0, exitDescription=org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.fi008CreationCompositeReader' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'cursorItemReaders'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy39 implementing org.springframework.beans.factory.InitializingBean,org.springframework.batch.item.ItemStreamReader,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.batch.item.database.AbstractCursorItemReader' for property 'cursorItemReaders[0]': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
    at com.sun.proxy.$Proxy31.open(Unknown Source)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)
    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.ent.giftcards.batch.SvcmpFI008Batch.runJob(SvcmpFI008Batch.java:58)
    at com.ent.giftcards.main.SvcmpFI008BatchApp.main(SvcmpFI008BatchApp.java:37)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'cursorItemReaders'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy39 implementing org.springframework.beans.factory.InitializingBean,org.springframework.batch.item.ItemStreamReader,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.batch.item.database.AbstractCursorItemReader' for property 'cursorItemReaders[0]': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:605)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:617)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1577)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1536)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    ... 23 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy39 implementing org.springframework.beans.factory.InitializingBean,org.springframework.batch.item.ItemStreamReader,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.batch.item.database.AbstractCursorItemReader' for property 'cursorItemReaders[0]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:574)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:220)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
    ... 29 more
]

有没有人知道这里出了什么问题。

更新

我已经尝试将列表更改为列表,但现在我正处于异常之下。

    [StepExecution: id=1, version=2, name=fi08step1, status=FAILED, exitStatus=FAILED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=0, exitDescription=org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
       at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
       at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
       at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
       at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
       at com.sun.proxy.$Proxy39.open(Unknown Source)
       at com.ent.giftcards.batch.readers.FI008CreationCompositeReader.open(FI008CreationCompositeReader.java:59)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
       at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
       at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
       at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
       at com.sun.proxy.$Proxy31.open(Unknown Source)
       at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
       at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
       at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)
       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.ent.giftcards.batch.SvcmpFI008Batch.runJob(SvcmpFI008Batch.java:58)
       at com.ent.giftcards.main.SvcmpFI008BatchApp.main(SvcmpFI008BatchApp.java:37)
Caused by: java.lang.IllegalStateException: Stream is already initialized.  Close before re-opening.
       at org.springframework.util.Assert.state(Assert.java:70)
       at org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:402)
       at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
       ... 40 more
]

0 个答案:

没有答案