Where to start a system that performs an hourly task

Asked

Viewed 948 times

2

I’m a little confused, where can I start a system that performs a task every hour? Have some framework?

The system will be desktop and will run on a Windows Server 2003 server. The idea is that every 1 hour the system makes a search in the database and with the returned data generate a file xml for my web application to read this xml and load the data into a DataTable

  • I think I got something, but just ask me a question. public static final long TEMPO = (1000 * 60 *60 ); that equals 1 hour?

  • 1

    That’s an hour in milliseconds.

  • 2

    But why don’t you create a program that does the task once, and schedule it on the server to run hourly? See http://answall.com/questions/41275/tarefas-agendadas-na-web

1 answer

1


There are several ways to solve this problem by considering a system 100% back end.

Use the system scheduler

Windows has a very flexible and powerful Task Scheduler. It may be simpler to configure it to call your program via command line at scheduled times.

Using the Java scheduler

If you want to schedule via Java yourself, use an API class that already does this for you. No need to count milliseconds, seconds, minutes and hours.

See, for example, the documentation of ScheduledExecutorService. There is everything and even a functional example, which I adapted in the code below:

public class BeeperControl {

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {

        //beep task
        final Runnable beeper = new Runnable() {
            public void run() {
                System.out.println("beep");
            }
        };

        //beep each hour
        final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.HOURS);

        //cancel beep task
        final Runnable canceler = new Runnable() {
            public void run() {
                beeperHandle.cancel(false);
            }
        };

        //stop beep after 1 day
        scheduler.schedule(canceler, 1, TimeUnit.DAYS);
    }

    public static void main(String[] args) {
        new BeeperControl().beepForAnHour();
    }

}

Let’s look at the code above:

  1. The line ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1) calls a method Factory to create a scheduled service performer (translation of class name).
  2. Within the method beepForAnHour, the first block declares an implementation of Runnable calling for beeper. This is the same API that implements threads. This one specifically prints beep on the console.
  3. The command scheduler.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.HOURS) is the most interesting here. It schedules the execution of our beeper (beeper) at regular intervals of one hour (parameters 3 and 4), the first execution being immediate (second parameter).
  4. The next section creates another Runnable called canceler. This will be responsible, when executed, for canceling the periodic schedule previously made.
  5. The command scheduler.schedule(canceler, 1, TimeUnit.DAYS); schedule a single execution of our canceler to exactly one day later. Then, the next day, the beep will stop.

Critical scenario

In the case of scheduled tasks that are critical for the business, it is interesting to replicate the program on multiple servers. If one of them fails, the others will be able to perform the critical routine. However, if the processing is heavy, it is not convenient for it to run redundantly, that is, it runs multiple times on these different servers.

For such cases, there are scheduling libraries like the Quartz which have specific settings for clusters servers. That way, you can do the deploy application on several servers and, in this example, Quartz will manage the execution, ensuring that your task will be executed properly once.

Even with a single server, Quartz can ensure that a lost run when the server is in service will resume once the server is available.

  • 1

    Thank you very much for the answer, clarified my question. Now I just have to analyze which of the options applies best in my case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.