在春季批处理中从Service方法读取值

时间:2020-08-30 11:58:30

标签: java angular spring-data-jpa spring-batch spring-restcontroller

这是我的第一批春季用户。我想每小时获取通过引用和id设计为List >的产品的库存,以使用有角度的google图表绘制该产品的演变图,这是我的服务方法

@Override
public Long getStockByRef(String ref, Long id) {
    Iterable<Produit> MyProducts = storeService.getAllProductOfStore(id);
    Long stock = 0L;
    for (Produit produit : MyProducts) {
        if(produit.getReference().equals(ref)) {
            stock =  produit.getStock();
        }
    }
    return stock;
}

这是我的RestController

@GetMapping("products/{id}/stock/{ref}")
public Long getStockByRef(@PathVariable Long id, @PathVariable String ref) {
    Long stock = produitService.getStockByRef(ref, id);
    return stock;
}

我的Spring批处理类配置

@Configuration
@EnableBatchProcessing
public class SpringBatchConfig {

    @Autowired private ProduitService produitService;
    @Autowired private JobBuilderFactory jobBuilderFactory;
    @Autowired private StepBuilderFactory stepBuilderFactory;
    @Autowired private ItemReader<Long> itemStockReader;
    @Autowired private ItemWriter<List<Map<Date,Long>>> itemStockWriter;
    @Autowired private ItemProcessor<Long, List<Map<Date,Long>> > itemStockProcessor;

     @Bean
     public Job myJob() {
         Step step1 = stepBuilderFactory.get("step-get-stock")
                .<Long,List<Map<Date,Long>>>chunk(1)
                .reader(itemStockReader)
                .processor(itemStockProcessor)
                .writer(itemStockWriter)
                .build();
        
           return jobBuilderFactory.get("data").start(step1).build();
    }
}

如何编写我的ItemReader ItemProcessor和我的Itemwrite以返回List >并通过@GetMapping发送它?

0 个答案:

没有答案