【问题标题】:Store lineNumber in stepExecutionContext and access it在 stepExecutionContext 中存储 lineNumber 并访问它
【发布时间】:2014-01-13 08:53:56
【问题描述】:

我正在尝试通过将其存储在 executionContext 中来跟踪 csv 文件中每个项目的 lineNumber。 Reader 是一个带有修改后的 lineMapper 的 FlatFileItemReader。这是它的代码:

public class MyLineMapper<T> implements LineMapper<T>, InitializingBean {

private LineTokenizer tokenizer;

private FieldSetMapper<T> fieldSetMapper;

public T mapLine(String line, int lineNumber) throws Exception {
    try{

        ExecutionContext value = new ExecutionContext();
        value.putInt("name", lineNumber);


        return fieldSetMapper.mapFieldSet(tokenizer.tokenize(line));
    }
    catch(Exception ex){
        throw new FlatFileParseException("Parsing error at line: " + lineNumber + 
                ", input=[" + line + "]", ex, line, lineNumber); 
    }
}

public void setLineTokenizer(LineTokenizer tokenizer) {
    this.tokenizer = tokenizer;
}

public void setFieldSetMapper(FieldSetMapper<T> fieldSetMapper) {
    this.fieldSetMapper = fieldSetMapper;
}

public void afterPropertiesSet() {
    Assert.notNull(tokenizer, "The LineTokenizer must be set");
    Assert.notNull(fieldSetMapper, "The FieldSetMapper must be set");
}

}

我只是创建一个 ExecutionContext 并将 lineNumber 存储为“名称”。但是当我从处理器调用这个值时,名称有一个空值。处理器代码:

public class CustomItemProcessor implements ItemProcessor<Report, Report> {

private int field;


public int getField() {
    return field;
}


public void setField(int field) {
    this.field = field;
}


@Override
public Report process(Report item) throws Exception {

    System.out.println("Processing " + item.getId() + "in line " + field);
    return item;
}

}

我的配置:

<bean id="report" class="com.example.model.Report" scope="prototype" />
<bean id="itemProcessor" class="com.example.CustomItemProcessor" scope="step">
    <property name="field" value="#{stepExecutionContext[name]}"></property>
</bean>

<batch:job id="helloWorldJob">
    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter"
                processor="itemProcessor" commit-interval="10">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

    <property name="resource" value="classpath:cvs/input/report.csv" />

    <property name="lineMapper">
        <bean class="com.example.MyLineMapper">
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="id,sales,qty,staffName,date" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="com.example.ReportFieldSetMapper"/>
            </property>
        </bean>
    </property>

</bean>

<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <property name="resource" value="file:xml/outputs/report.xml" />
    <property name="marshaller" ref="reportMarshaller" />
    <property name="rootTagName" value="report" />
</bean>

<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.example.model.Report</value>
        </list>
    </property>
</bean>

还有例外:

10:34:48.146 [main] DEBUG o.s.b.core.step.tasklet.TaskletStep - Rollback for RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.itemProcessor' defined in class path resource [spring/batch/jobs/job-hello-world.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'null' to required type 'int' for property 'field'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [int] for property 'field': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned
inappropriate value of type [null]
10:34:48.155 [main] DEBUG o.s.t.support.TransactionTemplate - Initiating transaction rollback on application exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.itemProcessor' defined in class path resource [spring/batch/jobs/job-hello-world.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'null' to required type 'int' for property 'field'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [int] for
property 'field': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type [null]

【问题讨论】:

    标签: java spring spring-batch


    【解决方案1】:

    让您的MyLineMapper 实现StepExecutionListener 并在StepExecutionListener.beforeStep()` 中绑定当前步骤执行上下文,而不是创建一个新的。

    public class MyLineMapper<T> implements LineMapper<T>, InitializingBean, StepExecutionContext {
      ExecutionContext stepExecution;
    
      public void beforeStep(ExecutionContext stepExecution) { this.stepExecution = stepExecution;}
    
      public T mapLine(String line, int lineNumber) throws Exception {
        this.stepExecution.putInt("name", lineNumber);
        // rest of your code..
      }
    }
    

    你也可以看看ItemCountAware interface;不完全相同,但(也许)可以满足您的需求。

    【讨论】:

    • 感谢@bellabax,它成功了。我不得不进行一些细微的更改:将 stepExecution 设置为 StepExecution 类型,然后使用 getExecutionContext().putInt() 添加 lineNumber。最后在 step1 中添加 MyLineMapper 作为监听器
    • 出现了一个问题:虽然ExecutionContext是从MyLineMapper用当前行更新的,但是Processor只保留第一个值可能是因为step作用域。有没有办法更新Processor中的executionContext?
    • 使用 int[] 或 List 或自定义 T 对象来存储行号(类似于 ItemCountAware intf
    【解决方案2】:

    尝试在 stepExection 中更新 ExitStatus 中的计数:

    @Override
        public ExitStatus afterStep(StepExecution arg0) {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多