• Post Reply Bookmark Topic Watch Topic
  • New Topic

Just One Instance  RSS feed

 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I enforce only one instance of my app running at once ?

Jerry.
 
peter coster
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this means you would have 2 different JVM's I would think there is no 'java way' of doing this.

First method that pops in my mind is using a "LOCK" file, on which your program holds on to while it's running. If it's not free when your program starts, it will terminate itself. But I'm pretty shure there are better ways for this.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check for a lock file on startup.
if it exists: then do not start.
if it does not exist: write the file.

make sure file is deleted on shutdown of your app.
make sure the file is deleted on crashes.


pascal
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possible solution is to use a socket instead of a file.

Pro: the socket gets closed automatically when the program exits, even when the VM crashes or something.

Con: the socket could be used by something else.
 
Paul Clapham
Sheriff
Posts: 22719
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you don't object to two instances running on two different computers? If you do, the task becomes much more difficult.
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program could listen on a port and reply back to a second instance that attempts to start. This would work across JVM's, but would make the zonealarm/kerio, etc go nuts-o. This is more reliable than the existance of a lock file because if you have a crash, your lock file remains and then you are SOL.

--Dale--
 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lock file idea sounds ok. I am using a properties file in my app. How can I create a lock on it and how do I check for a lock on it? Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is that the file itself serves as the lock. If it is present at startup time, then the second instance of the app should quit right away. So it must be a file that is created at startup, not one that is always present. That's also what creates a problem with the file approach - if the JVM crashes, the app or the JVM doesn't have a chance to delete the file, thus preventing the app from starting up after that.
 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 processes cannot open the same file for writing (and reading?) I will try to avoid using the temporary file and use the properties file.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not about reading or writing the file - the existence of it serves as the lock. The contents are irrelevant, and it can be empty.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jerry Crothers:
2 processes cannot open the same file for writing (and reading?) I will try to avoid using the temporary file and use the properties file.


Two processes can open the same file for writing.
 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Two processes can open the same file for writing.

That surprises me, are u sure?
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think u need to use singleton design pattern ,
by using this only one instance can be created.
:roll:
can i know why u need to enforce single instance???
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is about instances of applications, not instances of objects, so the singleton pattern doesn't help.

Saikrishna, what do you think the meaning of the smiley in your post is? We were discussing that particular smiley in this thread; maybe you wouldn't mind sharing your impression of it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!