views:

104

answers:

3

Hi,

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?

+8  A: 

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

Ha
+1. You probably also need some kind of system to handle the case when all locks are taken and no more work can take place. Or is it okay to just do nothing?
Thilo
When you say " ... app to lock ...", how do I lock it? Can you share me your idea/approach on locking?
Gnanam
`http://java.sun.com/javase/6/docs/api/java/nio/channels/FileChannel.html#lock(long, long, boolean)`. You can get `FileChannel` by calling `http://java.sun.com/javase/6/docs/api/java/io/FileInputStream.html#getChannel()`.
dimitko
Hi I wrote a small Java program to test this, but I'm getting NonWritableChannelException at runtime. I'm running this on a Windows machine. String file1 = "E:\\Java\\1.lock"; FileInputStream fis1 = new FileInputStream(file1); FileChannel fc1 = fis1.getChannel(); //fc1.lock(0, 1, false); fc1.lock();
Gnanam
To be able to open the channel for writing, you should construct a FileOutputStream and call .getChannel() on that. Read the second to last paragraph in the FileChannel summary javadoc http://java.sun.com/javase/6/docs/api/java/nio/channels/FileChannel.html.
matt b
I simpler solution is just to create the file when it doesn't exist and delete it when the process completes. If all 5 files exist, abort the current process as there are evidently 5 processes running.
Paul Lammertsma
What is Java Service Wrapper all about? Does it help for this purpose?
Gnanam
+1  A: 

First, I am assuming you are using the words "thread" and "process" interchangably. Two ideas:

  1. 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.
  2. 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.
M. Jessup
Hi, Jessup, you're right. I've used those 2 words interchangeably. I've edited the question again with "I also heard about Java Service Wrapper. What is this all about?" Any ideas/comment on this?
Gnanam
@jessup Do you have any simple cron job script to check total running processes currently? I'm not an expert in Linux schell scripting. Or, give me some links on this.
Gnanam
A: 

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.

Matthew Flynn