Hi @Mayukh007,
I'd suggest you create a java scheduler and call your service from that. That way it'd be easy for you to maintain/debug the code. Now coming to the implementation:
1. You need to write a java Scheduler and specify the scheduler.expression when you want it to run(http://www.cronmaker.com/). Inside the run() method, you can call your service implementation methods
2. You need to write a Service class where you'd specify the methods(like fetchGetResponse(), writeDataToJcrNodes() etc.).
3. You need to implement all these methods in your Service Impl class.
Above three java classes are the minimum number of java classes you need to write in order to complete your requirement.
---------------------------
Logic to process the response and convert it to JCR nodes:
public void testWriteToJCR(Session session) {
String[] pages = {
"page=1",
"page=2"
};
if (session.isLive()) {
try {
if (session.itemExists("/content/mySite/test-rest")) {
LOG.debug("Removing existing node: {}", "/content/mySite/test-rest");
session.getNode("/content/mySite/test-rest").remove();
session.refresh(true);
session.save();
}
Node jobsRootNode = JcrUtil.createPath("/content/mySite/test-rest",
JcrResourceConstants.NT_SLING_ORDERED_FOLDER, session);
for (String page: pages) {
HttpClient client = HttpClientBuilder.create().build();
String apiUrl= "https://reqres.in/api/users?" + page;
HttpGet get = new HttpGet(apiUrl);
ResponseHandler < String > responseHandler = new BasicResponseHandler();
String response = client.execute(get, responseHandler);
JSONObject jsonResponse = new JSONObject(response);
JSONArray jsonArray = jsonResponse.toJSONArray(jsonResponse.names()).optJSONArray(3);
Node pageNode = jobsRootNode.addNode(page, NodeType.NT_UNSTRUCTURED);
session.save();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jobObject = jsonArray.getJSONObject(i);
Node jobNode = pageNode.addNode(jobObject.get("id").toString(), NodeType.NT_UNSTRUCTURED);
Iterator < String > keys = jobObject.keys();
while (keys.hasNext()) {
String nextKey = keys.next().toString();
jobNode.setProperty(nextKey, jobObject.get(nextKey).toString());
}
}
session.save();
if (session.hasPendingChanges()) {
session.refresh(true);
session.save();
}
LOG.debug("RST Response converted to JCR nodes successfully");
}
} catch (RepositoryException | IOException | JSONException e) {
e.printStackTrace();
}
}
}
Thanks,
Bilal.