lundi 20 mars 2023

Can static list shared anywhere in spring boot application

Can I use static list initialized by @PostConstruct and get updated in every 4 secs, in other controllers, here I am trying to avoid multiple db calls. Is it correct approach? Please suggest. If not, What will be the consequences later as currently is working fine. Latest version of list not required its ok if any of the controller using old version of list and data insertion order is also not required that is why using parallelstream.

public class SpringBootApplication{

public static List<RefData> refDataList = null;

public static void main(String[] args) {
    
     SpringApplication.run(SpringBootApplication.class, args);
}



@PostConstruct
@Scheduled(fixedRate = 4000)
public void refDataUpdate() {

    refDataList =refDataService.findAllRefData();
    log.info(Ref Data Updated");

}

Controller 1

public class DataController1 {
     
@Scheduled(fixedRate = 10000)
public void updateRefDataByApi1() {
    List<RefData> list = SpringBootApplication.refDataList.parallelStream()
        .collect(Collectors.toList());
     
          <----Some Logics on Ref data list------->

        refDataService.saveAllRefData(list);
 }

Controller 2

public class DataController2 {
 
 @Scheduled(fixedRate = 10000)    
 public void updateRefDataByApi2() {
    List<RefData> list = SpringBootApplication.refDataList.parallelStream()
        .collect(Collectors.toList());
     
          <----Some Logics around Ref data list------->

        refDataService.saveAllRefData(list);
 }

Controller 3

public class DataController3 {

@Scheduled(fixedRate = 10000)
public void updateRefDataByApi3() {
    List<RefData> list = SpringBootApplication.refDataList.parallelStream()
        .collect(Collectors.toList());
    
    List<ResultList> resultList= new ArrayList<>();
         
     <----Some Logics around Ref data list------->

  
        otherService.saveAll(resultList);
 }
   

Aucun commentaire:

Enregistrer un commentaire