Following is a sample code from my program which queries the database and results are copied to different files in a directory. What I want to achieve is following code should run in 15 minutes interval so that Files are updated with the new data.
public class CountryLogtoCSV {
static Connection con = null;
static ResultSet rs = null;
public static void main(String... argv)
{
FileWriter filewriter=null;
File countryHits=new File("countryhits.csv");
filewriter=new FileWriter(countryHits);
query = "SELECT countryID, count(*) as total FROM mobileCountryLog"
+ " WHERE aHitType='ALL' AND aDate>'2012-11-06' GROUP BY countryID";
rs = Database.getResult(connection,query)
while (rs.next()) {
//Writing result to File, FileWriter is used
filewriter.append(rs.getString("countryID"));
filewriter.append(rs.getString("total"));
filewriter.flush();
}
File countryUnique=new File("countryunique.csv");
filewriter=new FileWriter(countryUnique);
query = "SELECT countryID, count(*) as total FROM mobileCountryLog"
+ " WHERE (aHitType='UNIQUE'AND aDate>'2012-11-06' GROUP BY countryID;
rs = Database.getResult(connection,query)
while (rs.next()) {
//Writing Result to File, FileWriter is used
filewriter.append(rs.getString("countryID"));
filewriter.append(rs.getString("total"));
filewriter.flush();
}
rs.close();
}
}
How to run this java class in every 15 minutes??
Thanks,
If you are running on Unix type OS, then you can do this with cron:
Add this to the crontab:
You can also do it in Java using the Executor package, but that means you will have to have that code running at all time or else it won’t execute. With cron, your code only needs to run every 15 minutes (or whatever you set the time to be). This means that if your server reboots or your code crashes at one time, it will try again at the next cycle. Much more stable and easier to manage.