我最终要做的(我是新人)是创建一个 Tasklet,并让它还实现 StepExecutionListener 接口。像魅力一样工作。它逐行读取以逗号分隔的文件,并取出第二列。我为我的 ExecutionContext 映射键创建了一个“枚举”。基本上,如下:
public class ProcessTabcPermitsTasklet implements Tasklet, StepExecutionListener {
private Resource resource;
private int linesToSkip;
private Set<String> permits = new TreeSet<String>();
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
BufferedReader reader = new BufferedReader((new FileReader(resource.getFile())));
String line = null;
int lines = 0;
while ((line = reader.readLine()) != null) {
if (++lines <= linesToSkip)
continue;
String[] s = StringUtils.commaDelimitedListToStringArray(line);
permits.add(s[TABC_COLUMNS.PERMIT.ordinal()]);
}
return RepeatStatus.FINISHED;
}
/**
* @param file
* the file to set
*/
public void setResource(Resource resource) {
this.resource = resource;
}
/**
* @param linesToSkip
* the linesToSkip to set
*/
public void setLinesToSkip(int linesToSkip) {
this.linesToSkip = linesToSkip;
}
public ExitStatus afterStep(StepExecution stepExecution) {
stepExecution.getExecutionContext().put(EXECUTION_CONTEXT.TABC_PERMITS.toString(), permits);
return ExitStatus.COMPLETED;
}
}