使用Gradle的MongoDB Spring Batch作业

时间:2018-10-22 17:12:24

标签: spring mongodb gradle spring-batch

我想使用Gradle为Spring批处理作业创建项目读取器,写入器和处理器。我在几件事上遇到麻烦。 delimited()部分给我一个错误。我现在尝试读取两个字段:rxFnamerxLname

这是我的代码:

@Configuration
@EnableBatchProcessing
public class PaymentPortalJob {
    private static final Logger LOG = 
    LoggerFactory.getLogger(PaymentPortalJob.class);


    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;


    @Bean
    MongoItemReader <PaymentAudit> fileReader(){
        return new MongoItemReaderBuilder <PaymentAudit>()
                .name("file-reader")
                .targetType(PaymentAudit.class)
                .delimited().delimiter(",").names(new String [] {"rxFname" , "rxLname"})
                .build();
    }

    @Bean
    public ItemProcessor<PaymentAudit, PaymentAudit> processor() {
        return new PaymentAuditItemProcessor();
    }

    @Bean
    public ItemWriter<PaymentAudit> writer() {
         return  new MongoItemWriterBuilder()<PaymentAudit>();
         try {
             writer.setTemplate(mongoTemplate());
         }catch (Exception e) {
             LOG.error(e.toString());
         }
         writer.setCollection("paymentAudit");
         return writer;
    }

    @Bean
    Job job (JobBuilderFactory jbf,
            StepBuilderFactory sbf, 
            ItemReader<? extends PaymentAudit> ir, 
            ItemWriter<? super PaymentAudit> iw) {

        Step s1 = sbf.get("file-db")
                .<PaymentAudit, PaymentAudit>chunk(100)
                .reader(ir)
                .writer(iw)
                .build();

        return jbf.get("etl")
                .incrementer(new RunIdIncrementer())
                .start(s1)
                .build();
    }

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(), "db-name");
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }

}

0 个答案:

没有答案