春季批处理:ItemReader在@Value(“#{stepExecutionContext [filename]}”)中有问题

时间:2019-04-08 14:42:01

标签: java spring spring-batch

我正在使用分区程序。这项工作正常,但是当我尝试使用stepExecutionContext从Partitioner类获取数据时,我在ItemReader中将其获取为null:

@Bean
@StepScope
public FlatFileItemReader<ClassName> Reader(
        @Value("#{stepExecutionContext['fileResource']}") String file) {
    FlatFileItemReader<ClassName> fileReader=new FlatFileItemReader<ClassName>();
    fileReader.setResource(new ClassPathResource(file));
    return fileReader;
}

Partnerner类如下:

public class PartitionerClass implements Partitioner {

   private String rep;

   @Override
   public Map<String, ExecutionContext> partition(int gridSize) {
    Map<String, ExecutionContext> partitionMap = new HashMap<String, ExecutionContext>();
    File dir = new File(rep);
    if (dir.isDirectory()) {
         File[] files = dir.listFiles();
           for (File file : files) {
               if(file.isFile()) {
                 ExecutionContext context = new ExecutionContext();
                 context.putString("fileResource", file.getName());
                 partitionMap.put(file.getName(), context);
                        }
                    }
                }
                return partitionMap;
            }
     public String getRep() {
      return rep;
       }

     public void setRep(String rep) {
     this.rep = rep;
        }
}

呼叫分区程序类:

@Bean
    public FilesPartitioner partitioner() {
        PartitionerClass partitioner = new PartitionerClass();
        partitioner.setRep("MyDirectory");
        return partitioner;
    }

0 个答案:

没有答案