重写您的代码。而不是
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); } }}
注意: 这是从我的头顶输入的,未经测试。但是应该提供一些指导。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)