Reputation: 2626
Hi I'm developing a java application wherein I need to call Restful api again and again after a specific time interval (say 10 seconds) and this would continue for a few days. (I'm using Apache HttpClient
library to call the service.)
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(rest-URL);
HttpResponse response = client.execute(getRequest);
Which is the most efficient way to achieve this?
Upvotes: 0
Views: 2909
Reputation: 834
Schedulers or Timers could be used to make the call every so often. You could also put that code inside a while loop and check System.currentTimeMillis(), run a modulus operation on it to return the time and drop into your code
Working from this answer.
while(true){
long milliseconds = System.currentTimeMillis();
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
if( /* time is right */ ){
// REST calls here
}
}
Upvotes: 1
Reputation: 4888
Take a look at Quartz Scheduler. For the restful api, I think Jersey framework is much easier to use than Apache HttpClient.
Upvotes: 0