I have a requirement like Every 45 minute value has to be updated after making http call.
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class TokenManagerRunnable implements Runnable{
String token;
public String fetchToken() {
return this.token;
}
@Override
public void run() {
String result = "";
HttpPost post = new HttpPost("https://login.microsoftonline.com/{{TenentId}}/oauth2/token");
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
urlParameters.add(new BasicNameValuePair("client_id", "some client id"));
urlParameters.add(new BasicNameValuePair("client_secret", "some secret id"));
urlParameters.add(new BasicNameValuePair("resource", "https://database.windows.net"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post)){
result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectNode node;
try {
node = new ObjectMapper().readValue(result, ObjectNode.class);
System.out.println(result);
if (node.has("access_token")) {
result = node.get("access_token").toString();
}
System.out.println(result);
System.out.println(result.substring(1, result.length()-1));
//updating the token
this.token = result.substring(1, result.length()-1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And here is my main function
SQLServerDataSource ds = new SQLServerDataSource();
TokenManagerRunnable tokenManagerRunnable = new TokenManagerRunnable();
ScheduledExecutorService sches = Executors.newScheduledThreadPool(1);
sches.scheduleWithFixedDelay(tokenManagerRunnable, 0, 45, TimeUnit.MINUTES);
System.out.println("fetching the token ---- "+tokenManagerRunnable.fetchToken());
ds.setAccessToken(tokenManagerRunnable.fetchToken());
try {
Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(" select * from [dbo].[CLIENT]");
System.out.println("You have successfully logged in");
while(rs.next()) {
System.out.println(rs.getString(1));
}
}catch(Exception ex) {
ex.printStackTrace();
}
tokenManagerRunnable.fetchToken() brings null as the TokenManagerRunnable class is not yet executed.
How can we achieve wait till ScheduledExecutorService complete the task so we can get the value and set the new value in Datasource after every 45 minutes from tokenManagerRunnable.fetchToken() instead of null?
Any thoughts?