与https请求多线程Spring的jpa

与https请求多线程Spring的jpa,第1张

与https请求线程Spring的jpa

重写您的代码。而不是

List<Bulk_repository>
返回
Stream<Bulk_repository>
。这将懒惰地从数据库中加载记录,而不是尝试立即执行所有 *** 作。

然后使用

TaskExecutor
来为每个线程执行不同的请求,只给它一个任务,它将在有空闲线程时执行。

@SpringBootApplicationpublic class AccessingDataJpaApplication implements CommandLineRunner {    private static final Logger logger = LoggerFactory.getLogger(AccessingDataJpaApplication.class);    @Autowired    private Bulk_repositoryRepository bulk_repositoryRepository;    @Autowired    private AsyncTaskExecutor executor;    @Autowired    private RestTemplate rest;    public static void main(String[] args) {        SpringApplication.run(AccessingDataJpaApplication.class);    }    @Override    public void run(String... args) throws Exception {        Date currentDate = new Date();        Stream< Bulk_repository> results = Bulk_repository churnss : bulk_repositoryRepository.findAllByStatusAndCampTypeAndCampStartDateLessThanEqualAndCampEndDateGreaterThanEqual(0,2,currentDate,currentDate);        results.forEach(it -> executor.submit(this.process(it)));        Thread.sleep(1000);    }    private void process(RestTemplate rest, Bulk_repository churnss) {      AddOfferRequest AddOffer = new AddOfferRequest("113", churnss.getMsisdn(),churnss.getParam1());      HttpEntity<AddOfferRequest> entity = new HttpEntity<AddOfferRequest>(AddOffer,headers);      try {        ResponseEntity<String> responseEntity = restTemplate.exchange(  "api link", HttpMethod.POST, entity, String.class);         if(responseEntity.getStatusCode() == HttpStatus.OK){String response = responseEntity.getBody();churnss.setStatus(1);churnss.setProcessDate(new Date());churnss.setFulfilment_status(response);bulk_repositoryRepository.save(churnss);         }else {logger.warn("Record Id: {}, Http Failed Response: {}",churnss.getId(), responseEntity.getStatusCode());     }      } catch (RestClientException rce) {          logger.warn("Record Id: {} Http Failed. ", churnss.getId(), rce);      }        }}

注意: 这是从我的头顶输入的,未经测试。但是应该提供一些指导。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5152961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存