Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

running only one instance of java program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like run only one instance of my program.
if one instance is running, i should get a message it is already running.
now if i can run 'n' number of instances.
like "i have QsFrame class which is main class"
if i run "java QsFrame" then until i exit from this frame i should run another instance.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using singleton pattern.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
won't work.
Singleton prevents multiple instances inside a single JVM but nothing will prevent the starting of multiple JVMs on the same machine.
You will need some means of cross-JVM communication to check whether a specific class (say your application's main class) is loaded inside the other JVMs and if it is stop the application from loading.
Whether such a system exists or is even possible I can't tell, never having tried it.
An alternative might be to set up a shared resource that keeps track of the application being loaded (maybe a simple file or database field).
Major disadvantage of that approach is that if the application is improperly terminated (crashes, gets killed, etc) the resource will still show the application as running and now no instances can be started at all.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simplest thing to do is to have your program try to open a socket - if it suceeds, its the only copy running. See java.net.ServerSocket.
This is not really a servlet question - if I find you have asked this question in a bunch of other forums I shall be very annoyed.
Bill
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good solution. To extend that, if it is on a single OS, any IO source locking can be used for controlling that, besides socket.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic