• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Just One Instance

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I enforce only one instance of my app running at once ?

Jerry.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27540
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Two processes can open the same file for writing.

That surprises me, are u sure?
 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic