How to control number of instance running?

HiI wonder hot to make my app to do not allow more then one instance to run at the same time.
[121 byte] By [NenadDj] at [2007-9-30 7:08:16]
# 1
This question keeps on surfacing all the time. The discussions ususally boil down to the suggestion to use an application-specific port to listen on. If that port is taken by an instance, the next one can not make a ServerSocket on it.
BIJ001 at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 2
I want to control number of instances for simple console app, not a web app.
NenadDj at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 3
If I understand correctly, your application needs to access console, that should be synchronized. If this is true, why don't you approach Singleton concept.
rathsb at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 4

I wrote a simple app that runs on some time interval (ex. every 1 min.). That app get and put some files from a some FTP, connect to the Oracle DB etc.. I put that app on a Linux cron table and setup to run every minute. I don't wont cron to start app again before previous instance is finished. That is why I need to test if the previous instance is over before start the new one.

NenadDj at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 5
What is wrong with using a ServerSocket for this purpose as described above? You don't have to accept any connection on it.
BIJ001 at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 6
I suggest singleton pattern. You can many docs on net. It allows you to create only one instance of your main class. That solves the problem
Upendrach at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 7
What if several JVMs run on a given box, each of them having an instance of your singleton?
BIJ001 at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...
# 8

> What if several JVMs run on a given box, each of them

> having an instance of your singleton?

Amen. Singleton will absolutely not work to solve the OPs question. Regardless of whether the app is a "server" or not, it is still allowed to open a listener on a socket.

The only caveat here is that if you are running on a windows laptop that is not connected to the network, the server socket will fail (nice, eh?).

Another option is to use the nio API to create a lock file.

- K

trumpetinc at 2007-7-1 23:17:35 > top of Java-index,Administration Tools,Sun Connection...