malteser
malteser

Reputation: 485

Automatically run Java methods every hour

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

Upvotes: 0

Views: 3368

Answers (3)

Muhammad Suleman
Muhammad Suleman

Reputation: 2922

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

Upvotes: 1

javajon
javajon

Reputation: 1698

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."

Upvotes: 2

John3136
John3136

Reputation: 29266

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).

Upvotes: 3

Related Questions