Windows Scheduler Vs. Java TaskTimer - java

I have a .bat file in a Windows machine that starts our program by calling a main class of a Java executable(.Jar)
Now I need to run this every 30 mins.
I gone through several ways of doing it, but unable to decide which is better.
Scheduling through Windows scheduler or Using Java Timer. Which one to choose?
I want only one instance of the process running. If the previous process doesnt complete within 30min, i could wait.
Please let me know what to go for, based on my use case.
Thanks in advance.

You're better off using the Windows Scheduler. If there's a real risk of the process taking too long, you can create a file, or open a socket while the process is running and when another one tries to start up, it can detect that and simply quit. This would make it "miss" the 30m window (i.e. if the first job started at 12 and finished at 12:35, the next job would not start until 1).
But this way you don't have to worry at all about setting up long running processes, starting and stopping the java service, etc. The Windows scheduler just makes everything easier for something like this.

TimerTask is not a scheduling system, it is a library that provides tools for in-app scheduling. Seems that for your use-case you need a the system: you need it to run whether or not your app is running, you need reporting, etc. Windows Scheduler (or cron on unix/linux) is more appropriate for your needs.

Related

Creating a background process using java

I want run a background process at a specific time. I want that process to be run on the server even without running the application from End-User side. The application is made using Spring. Please suggest how to approach for it.
Thanks and regards
Souvik
I depends highly on what platform you are working on, and what you want to achieve.
If it is a simple application, that you simply want to invoke that on specific time, then you can use scheduling tools available on your platform, for example, crontab for Unix, or scheduled task (at) for Windows.
If you want the application to be run as a daemon process, and the application itself will handle the scheduling, then you need to solve two problem: 1. create a daemon process (aka system service), and 2. doing scheduling in Java.
For problem 1, there are already answer for it. Just have a search on Google on "Java System Service" will give you some other useful tools too, like Java Service Wrapper
For problem 2, there are a lot of way to perform scheduling in Java. You can do it by built-in Timer etc, or using scheduling library like Quartz

Schedule task to run once per week in java

What would be the best way to schedule an event to run every Monday at 00:00?
Note though the app is running 24/7. So OS based schedulers such as cron are not needed.
You could embed a Quartz Scheduler in your Java app if it's a long running app. Quartz is a very flexible/powerful scheduler that can be configured in many ways.
Alternatively, if you simply want to run a Java process at a particular time then you should look at cron.
Edit: Given that your app is 24/7 (noting comments above), Quartz is definitely the way forward.
I think cron4J may be your best bet. You can then configure it to run at ceratin times.
Us the OS scheduler; either cron, Windows task scheduler or similar

Ensure program runned via Runtime.exec() will die together with the java app

in my java program I am calling external program via Runtime.exec and calling Process.waitFor to wait for its completion. This is some long-running script. I want to ensure that if anything goes wrong with my java app (e.g. gets killed from outside for example, which btw. really happens from time to time in my case) the external running script will die as well.
I had a look on the Runtime.addShutdownHook which might be appropriate for this, but it clearly states that for example on SIGKILL no guarantee can be made whether the shutdownhook will or won't run.
Is there any other way how to ensure an external program runned from Java will die together with the calling java process?
Thanks
If "Runtime.addShutdownHook" is not sufficien I think from the java side you are out of luck. Some ideas:
your script can test if the java app is still running and terminating if needed
let your java app update a file with a timestamp and let your script check periodically if the timestamp is to old (and terminate)
Edit: launch another process which only monitors the java app and the script and kills the script is the java app is gone (of course if THIS process is killed you are out of luck again)

Controlling maximum Java standalone running in Linux

We've developed a Java standalone program. We've configured in our Linux (RedHat ES 4) cron
schedule to execute this Java standalone every 10 minutes. Each standalone
may sometime take more than 1 hour to complete, or sometime it may complete
even within 5 minutes.
My problem/solution I'm looking for is, the number of Java standalones executing
at any time should not exceed, for example, 5 process. So, for example,
before even a Java standalone/process starts, if there are already 5 processes running,
then this process should not be started; otherwise this would indirectly start
creating OutOfMemoryError problems. How do I control this? I would also like to make this 5 process limit configurable.
Other Information:
I've also configured -Xms and -Xmx heap size settings.
Is there any tool/mechanism by which we can control this?
I also heard about Java Service Wrapper. What is this all about?
You can create 5 empty files (with names "1.lock",...,"5.lock") and make the app to lock one of them to execute (or exit if all files are already locked).
First, I am assuming you are using the words "thread" and "process" interchangably. Two ideas:
Have the cron job be a script that will check the currently running processes and count them. If less than threshold spawn new process, otherwise exit, here threshold can be defined in your script.
Have the main method in your executing java file check some external resource (a file, database table, etc) for a count of running processes, if it is below threshold increment and start process, otherwise exit (this is assuming the simple main method will not be enough to cause your OOME problem). You may also need to use an appropriate locking mechanism on the external resource (though if your job is every 10 minutes, this may be overkill), here you could defin threshold in a .properties, or some other configuration file for your program.
Java Service Wrapper helps you set up a java program as a Windows service or a *nix daemon. It doesn't really deal with the concurrency issue you are looking at--the closest thing is a config setting that disallows concurrent instances if its a Windows service.

Virtual Dedicated Server Question

I have a Virtual Dedicated Server, how can I run an application periodically (ie: every Monday - 19:00). I mean, the application starts automatically at a given time.
Is there any way to do it with Java, Ajax, PHP? (sorry, I'm noob)
You can create scheduled tasks in windows and cron jobs in linux/*nix like systems. If you can run programs on the server and want to use java you can use timers to schedule your task - here is a simple example.
I'm assuming your server is linux.
You may be able to do this with webmin.
You will need to use command line to install webmin and setting up a crontab from command line is really not that hard to do.
Here is a tutorial for crontab.
http://www.linuxweblog.com/crotab-tutorial
What OS does the server run?

Categories