jpa with https request multithreading spring

后端 未结 3 1640
长发绾君心
长发绾君心 2020-12-12 05:58

I\'m working with spring JPA and HTTP post request, fetching the data row by row then post the data into HTTP request to API and its worked fine wi

相关标签:
3条回答
  • 2020-12-12 06:31

    Rewrite your code. Instead of a List<Bulk_repository> return a Stream<Bulk_repository>. This will lazily load the records from the database, instead of trying to do everything at once.

    Then use the TaskExecutor to execute the different requests per thread, just give a task to it and it will be executed when there is a free thread.

    @SpringBootApplication
    public 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);
          }               
        }
    
    }
    

    NOTE: This was typed from the top of my head and isn't tested. However should provide some guidance.

    0 讨论(0)
  • 2020-12-12 06:32

    Try with Batch Insert/Update with Hibernate/JPA. Here is a nice tutorial

    spring.jpa.properties.hibernate.jdbc.batch_size=500

    0 讨论(0)
  • 2020-12-12 06:44

    Using @Async annotations to implement mutithread in spring. It can help you. https://spring.io/guides/gs/async-method/ https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html

    0 讨论(0)
提交回复
热议问题