Reputation: 684
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,
Upvotes: 3
Views: 9370
Reputation: 3279
If you do not want to use the schedulers and do not want to change the time limits, most basic solution is to use a simple thread, and sleep for (15*90*1000) miliseconds... Helpful and effective if you do not want to use 3rd party s/w.
Upvotes: 0
Reputation: 502
Better you can go with quartz scheduler to execute your code periodically. try with the below reference
http://www.mkyong.com/java/quartz-scheduler-example/
Upvotes: 0
Reputation: 3411
In you case you need to bundle up your code something like this.
import static java.util.concurrent.TimeUnit.*;
public class CountryLogtoCSV{
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void logtoCSV() {
final Runnable logger= new Runnable() {
//You application logic as shown in the question
};
final ScheduledFuture<?> loggerHandle =
scheduler.scheduleAtFixedRate(logger, 15, 15, MINUTES );
//Incase you want to kill this after some time like 24 hours
scheduler.schedule(new Runnable() {
public void run() { loggerHandle.cancel(true); }
}, 24, HOURS );
}
}
Hope this helps
Upvotes: 1
Reputation: 172618
You can use the ScheduledExecutorService for this
Here is an example:-
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
Upvotes: 2
Reputation: 21912
If you are running on Unix type OS, then you can do this with cron:
Add this to the crontab:
*/15 * * * * /yourpath-to-jdk/bin/java -cp yourclasspath CountryLogtoCSV
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.
Upvotes: 4