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:
- Run my application at startup, and handle everything myself;
- Use a system service.
- 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:
- Are there other options?
-
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?
Here’s what I ended up implementing:
Usage is simple. Schedule a new alarm using:
Trigger an alarm like this:
Tested on Mac OS X.6 and Windows Vista. The class
Lis an helper toSystem.out.printlnandGholds my global constants (here, my JNLP file on my server used to launch my application).