Spring批量读取器文件

时间:2015-05-11 13:06:49

标签: spring mongodb spring-batch

我正在使用spring boot和spring批处理框架开发Spring webapp。

我们有一套复杂的&不同的json文件,我们需要:

  1. 阅读每个文件
  2. 略微修改其内容
  3. 最后将它们存放在mongodb中。
  4. 问题:使用spring batch执行此任务是否合理?正如我在教程示例等中看到的那样,spring批处理是逐行处理的正确工具,但是文件按文件怎么样?

    我没有写作者(MongoItemWritter)和处理器的问题,但我不知道如何实现读者。

    谢谢!

1 个答案:

答案 0 :(得分:2)

您可以绝对使用Spring Batch。 您的阅读器的项目可以是File

 public class CustomItemReader implements InitializingBean{

    private List<File> yourFiles= null;

    public File read() {
        if ((yourFiles!= null) && (yourFiles.size() != 0)) {
            return yourFiles.remove(0);
        }
        return null;
    }

    //Reading Items from Service
    private void reloadItems() {
        this.yourItems= new ArrayList<File>();
       // populate the items
     }


    @Override
    public void afterPropertiesSet() throws Exception {
        reloadItems();
    }
}

自定义处理器:

public class MyProcessor implements ItemProcessor<File, File> {
@Override
public File process(File arg0) throws Exception {
   // Apply any logic to your File before transferring it to the writer
 return arg0;
 }
}

一位自定义作家:

  public class MyWriter{
      public void write(File file) throws IOException {
         }
   }
相关问题