如何使用Spring Batch Tasklet下载文件并读取以插入DB

时间:2019-12-19 18:40:37

标签: java spring spring-batch

我创建了一个Tasklet,该Tasklet连接到sftp服务器并下载文件,但是当传递给itemreader时,我的代码仍然不起作用,因为它告诉我“无法初始化阅读器”,使我的IDE可视化,文件显示在路径中在执行结束时,“输入资源必须存在(阅读器处于'严格'模式):类路径资源[hi.csv]“

我的Tasklet类:

=SORT(FILTER('Ark1'!A2:C999; 'Ark1'!D2:D999=1); 5; 1)

我的物品阅读器:

public class giveMeTheFile implements Tasklet {


    private static final String USERNAME = "seenisftp";
    private static final String HOST = "172.17.56.250";
    private static final String PASSWORD = "shenlong";
    private static final String PATH = "upload/test.csv";
    private static final String LOCALDIR = "src/main/resources/";
    private static final Logger LOG = LoggerFactory.getLogger(giveMeTheFile.class);
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(giveMeTheFile.class);


    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {


        SSHClient client = new SSHClient();
        client.addHostKeyVerifier(new PromiscuousVerifier());
        LOG.info("Connecting to server..." + HOST);
        client.connect(HOST);

        LOG.info("verify.." + USERNAME);
        LOG.info("verify.." + PASSWORD);
        client.authPassword(USERNAME, PASSWORD);
        LOG.info("successful connection");
        SFTPClient sftpClient = client.newSFTPClient();
        LOG.info("searching file....");
        sftpClient.get(PATH, LOCALDIR + "hi.csv ");
        LOG.info("file get successfully" + LOCALDIR + sftpClient);
        sftpClient.close();
        client.disconnect();


        return RepeatStatus.FINISHED;
    }
}


我调用任务集的步骤:

    @Bean
    public FlatFileItemReader<Pagare> fileItemReader(BeanWrapperFieldSetMapper<Pagare> beanWrapperFieldSetMapper)  {

        FlatFileItemReader<Pagare> fileItemReader = new FlatFileItemReader<>();
        fileItemReader.setLinesToSkip(1);
        fileItemReader.setResource(new ClassPathResource("hi.csv"));
        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
        delimitedLineTokenizer.setNames("numeroPagare", "numeroContrato", "numeroOperacion", "rutCliente", "fechaOtorgamiento", "marca", "vehiculo", "version", "modelo", "montoFinanciar", "impuesto");
        DefaultLineMapper<Pagare> defaultLineMapper = new DefaultLineMapper<>();
        defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
        defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);
        fileItemReader.setLineMapper(defaultLineMapper);
        return fileItemReader;
    }

我的工作调用步骤:

   @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(new giveMeTheFile())
                .build();
    }

如果我通过连接和下载文件完成任务小程序,我不明白我在做什么错...因为在任务小程序的末尾下载未完成?根据官方文档,小任务用于执行之前的任务

0 个答案:

没有答案