gpt4 book ai didi

java - 将 lineNumber 存储在 stepExecutionContext 中并访问它

转载 作者:行者123 更新时间:2023-11-30 04:04:30 33 4
gpt4 key购买 nike

我试图通过将其存储在executionContext 中来跟踪csv 文件中每个项目的行号。 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 存储为“name”。但是当我从处理器调用该值时,名称具有空值。处理器代码:

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>

异常(exception)情况:

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]

最佳答案

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

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 ;不完全相同,但(也许)可以满足您的需求。

关于java - 将 lineNumber 存储在 stepExecutionContext 中并访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087115/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com