I am trying to fetch the data and store it in database.
Created a Get mapping for invoking the data from url and storing it in database using service class .
@GetMapping("/")
public String root(Model model) throws IOException {
model.addAttribute("test1","Hello user");
service.populate();
return "mainTemplate";
}
my populate method in service class add data to database.
public void populate() throws IOException{
URL url = new URL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-01-2021.csv");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
int res= connection.getResponseCode();
CSVReader reader=null;
if(res==200) {
log.info("Connected to github");
try {
BufferedReader readurl = new BufferedReader(new InputStreamReader(connection.getInputStream()),8192);
reader=new CSVReader(readurl);
String[] line;
int i=0;
while((line=reader.readNext())!=null) {
if(i==0) {
i++;
continue;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Corona corona = new Corona();
corona.setLastupDate(LocalDateTime.parse(line[4],formatter));
corona.setConfirmed(Long.valueOf(line[7]));
corona.setRecovered(Long.valueOf(line[9]));
corona.setActive(Long.valueOf(line[10]));
corona.setCombinedKey(line[11]);
log.info(corona.toString());
repo.save(corona);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CsvValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if(reader!=null) {
try {
reader.close();
} catch (IOException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
else {
log.info("URL is wrong");
}
}
Everything is working fine when i hit the resource url ,but i have to wait for some time to show my webpage , until all data does not get stored in database.
I want to show "Data is being added" in mainTemplate.html as soon as i hit the url. So that my populate method runs in background and i don't have to wait for completion of method to show my mainTemplate .
I tired to add @Async method annotation but that does not seem to be worked .