Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best solution to perform scheduled tasks in Java, whatever the OS?

I'd like to generate alarms on my Java desktop application :

  • alarms set with a specific date/time which can be in 5 minutes or 5 months
  • I need to be able to create a SWT application when the alarm is triggered
  • I need this to be able to work on any OS. The software users will likely have Windows (90% of them), and the rest Mac OS (including me)
  • the software license must allow me to use it in a commercial program, without requiring to open source it (hence, no GPL)
  • I cannot require the users to install Cygwin, so the implementation needs to be native to Windows and Unix

I am developing using Java, Eclipse, SWT and my application is deployed from my server using Java Web Start. I'm using Mac OS X.6 for developing.


I think I have a few options:

  1. Run my application at startup, and handle everything myself;
  2. Use a system service.
  3. Use the cron table on Unix, and Scheduled Tasks on Windows

Run at startup

I don't really like this solution, I'm hoping for something more elegant.
Refs: I would like to run my Java program on System Startup on Mac OS/Windows. How can I do this?

System service

If I run it as a system service, I can benefit from this, because the OS will ensure that my software:

  • is always running
  • doesn't have/need a GUI
  • restarts on failure

I've researched some resources that I can use:

  • run4j — CPL — runs on Windows only, seems like a valid candidate
  • jsvc — Apache 2.0 — Unix only, seems like a valid candidate
  • Java Service Wrapper — Various — I cannot afford paid licenses, and the free one is a GPL. Hence, I don't want to/can't use this

My questions in the system service options are:

  1. Are there other options?
  2. Is my planned implementation correct:

    • at the application startup, check for existence of the service
    • if it is not installed:
      • escalate the user to install the service (root on Unix, UAC on Windows)
      • if the host OS is Windows, use run4j to register the service
      • if the host OS is Unix, use jsvc to register the service
    • if it is not running, start it

Thus, at the first run, the application will install the service and start it. When the application closes the service is still running and won't need the application ever again, except if it is unregistered.
However, I think I still miss the "run on startup" feature.

Am I right? Am I missing something?

cron / Task Scheduler

On Unix, I can easily use the cron table without needing the application to escalate the user to root. I don't need to handle restarts, system date changes, etc. Seems nice.

On Windows, I can use the Task Scheduler, even in command-line using At or SchTasks. This seems nice, but I need this to be compatible from XP up to 7, and I can't easily test this.


So what would you do? Did I miss something? Do you have any advice that could help me pick the best and most elegant solution?

like image 679
Benoit Duffez Avatar asked Aug 10 '12 09:08

Benoit Duffez


People also ask

How do you schedule a task in Java?

One of the methods in the Timer class is the void schedule(Timertask task, Date time) method. This method schedules the specified task for execution at the specified time. If the time is in the past, it schedules the task for immediate execution.

Which service is used to run scheduled jobs?

A Cron is a time-based job scheduler. It enables our application to schedule a job to run automatically at a certain time or date.

Which one is used to schedule future runnable tasks?

Interface ScheduledExecutorService. An ExecutorService that can schedule commands to run after a given delay, or to execute periodically. The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution.

How do I schedule a Java program to run daily?

Schedule Via JavaUse a ScheduledExecutorService and the method scheduleAtFixedRate with a TimeUnit of Day. Your program will wait a day and then do what it has to do. Of course your computer has to be on. If that is an issue, it might be better do something like this using Google App Engine.


2 Answers

Bicou: Great that you shared your solution!

Note that the "schtasks.exe" has some localization issues, if you want to create a daily trigger with it, on an English Windows you'd have to use "daily", on a German one (for example) you'd have to use "täglich" instead.

To resolve this issue I've implemented the call to schtasks.exe with the /xml-option, providing a temporary xml-file which I create by template.

The easiest way to create such a template is to create a task "by hand" and use the "export"-function in the task management GUI tool.

like image 103
ifot Avatar answered Sep 28 '22 07:09

ifot


Of the available options you have listed, IMHO Option 3 is better. As you are looking only for an external trigger to execute the application, CRON or Scheduled tasks are better solutions than other options you have listed. By this way, you remove a complexity from your application and also your application need not be running always. It could be triggered externally and when the execution is over, your application will stop. Hence, unnecessary resource consumption is avoided.

like image 45
Rajkumar Avatar answered Sep 28 '22 07:09

Rajkumar