我试图通过将它存储在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存储为“name”。但是当我从处理器调用此值时,name的值为null。处理器代码:
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]
答案 0 :(得分:2)
让你的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;并不完全相同,但(可能)对您的需求有用。
答案 1 :(得分:0)
尝试在stepExection中更新ExitStatus中的计数:
@Override
public ExitStatus afterStep(StepExecution arg0) {
}