0

I have these methods in Java which should run every Hour for example:

public static void main(String[] args) throws Exception{    
    readLog(); // method to read log file
    sortByDate(); //sort punch in data by day
    checkPeopleIn(); // check people who are in, add and remove to list accordingly 
    output_details(); //output final details
    System.out.print("_____________________________________");      
}

How would I add this functionality to this program? In Processing I used to use millis() method to create a timer, but I cannot find the equivalent.... Is it available?

Thanks in advance

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
malteser
  • 485
  • 2
  • 10
  • 27

3 Answers3

3

If this is part of a long running app like a daemon or a service then the solution of using a Timer is ok.

If you want to run your java app every hour, you need an OS specific solution (like cron).

John3136
  • 28,809
  • 4
  • 51
  • 69
  • I understand what you mean, makes sense. This is application to check who's in the building according to a log file generated by a hand scanner. – malteser Jul 14 '14 at 07:02
  • So does it sit there running 24x7 or do you just want to fire it up once per hour? – John3136 Jul 14 '14 at 07:03
  • The hand scanner polls out data to a log file every 1 hour, so after it polls, I would need to read this log file, and output an updated file. – malteser Jul 14 '14 at 07:19
  • I'm upvoting this one because of the cron ('OS specific solution') suggestion. Having to let a Java runtime wait for the most part of the day just to be able to execute something at an interval is just wasted effort when the OS already has facilities to do that. – Gimby Jul 14 '14 at 07:33
2

Look at the Java Executor framework.

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        readLog(); // method to read log file
        sortByDate(); //sort punch in data by day
        checkPeopleIn(); // check people who are in, add and remove to list accordingly
        output_details(); //output final details
        System.out.print("_____________________________________");
    }
}, 0, 1, TimeUnit.HOURS);
service.shutdown();

From this thread: "If you can use ScheduledThreadExecutor instead of Timer, do so."

Community
  • 1
  • 1
javajon
  • 1,660
  • 16
  • 18
  • 1
    And be sure to either shutdown the executor or make it a daemon thread, if you want a gracefully stop your process. – lyomi Jul 14 '14 at 07:04
  • 1
    It probably makes more sense to use `TimeUnit.HOURS` instead of calculating the number of seconds in an hour. – Keppil Jul 14 '14 at 07:06
  • I implemented this code, and replaced it with the main method. But I got errors, only.... Don't know if I'm implementing it correctly though. Thanks for your help – malteser Jul 14 '14 at 07:22
  • Modified code in example based on good catches @lyomi and Keppil – javajon Jul 14 '14 at 07:32
  • After doing more reading and research if you are still stuck post your modified and executable code in a [MVCE](http://stackoverflow.com/help/mcve) format here. – javajon Jul 14 '14 at 07:36
1

Check out quartz-scheduler. It is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33