简体   繁体   中英

how to put wait time between rest-api calls based on condition

i has to call an API which returns users list and their status. for example:

[
  {"user":"ram",
    "status":"inprogress"
  },
  {"user":"abdullah",
    "status":"inprogress"
  },
  {"user":"jhon",
    "status":"completed"
  },
  {"user":"guru",
    "status":"inprogress"
  }
] 

if status is "inprogress" for any user, after "(number of users *2)seconds" has to call same API again.

if still status is "inprogress" for any tenant then has to throw exception

i am thinking to try with

    wait(number of users *2000) 
        or
   Thread.sleep(number of users *2000) 

is there any best way to achieve it other than these two ?

int no_of_users_status_is_inProgress=0;
ResponseEntity<UserAndStatusData> response=new RestTemplate.exchange(rl,HttpMethod.GET,reqEntity,UserAndStatusData.class,new Object[] {});
'
'
====updating no_of_users_status_is_inProgress====
'
'
if(no_of_users_status_is_inProgress>0){

   wait(no_of_users_status_is_inProgress*2000);

   ResponseEntity<UserAndStatusData> response2=new RestTemplate.exchange(rl,HttpMethod.GET,reqEntity,UserAndStatusData.class,new Object[] {});
   '
   '
   ====updating no_of_users_status_is_inProgress====
   '
   '
   if(no_of_users_status_is_inProgress>0){
      throw customException("timeout");
   }
}

You can use ScheduledExecutorService of java which has schedule method and option to set delay. ScheduledExecutorService will keeps checking on user status. If you want to shutdown the service you can do it. Below is Javadoc link for the same: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM