Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JVM Thread  RSS feed

 
Mark Hibbard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class which runs different threads.

I want there to only ever be one instance of this class running on a machine (i.e. someone can't kick off another jvm running this class)

Anyone got any simple ideas of how to do this (other than writing a file which is checked for existence before laucning the class)

I though i could do something with sockets (not for this forum i know) but did't have any lucj!
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your socket idea should work - you can bind a ServerSocket to a particular port. When the second instance comes up it should not ignore any exceptions from the ServerSocket constructor.

Don't use the ServerSocket constructor that takes an InetAddress - you'll want to bind to the port on all available interfaces.
 
Mark Hibbard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doin as below - but can still start up multiple instances - do i need to do something to the serversocket to stop the constructor working for other instances? (i.e. set it to accpet or bind it or something?)

Integer socketInt = new Integer(port);

ServerSocket testServer = null;
// Try to open a server socket on port 9999
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)

try {
testServer = new ServerSocket(socketInt.intValue());
}
catch (IOException e) {
log.debug("Is the threads already running on this port?");
log.debug("Cannot open socket on port: " + port);
log.debug(e.getMessage());
return;
}
catch (Exception e) {
log.debug("Is the threads already running on this port?");
log.debug("Cannot open socket on port: " + port);
log.debug(e.getMessage());
return;
}
log.debug("Running on port: " + port);
 
Mark Hibbard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no sorry the above works !!!
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you just use RMI and a client server model. That way you can ensure there is only ever 1 server. And each client can just pass control back to the server and die. That is really what RMI is suited for.
 
Mark Hibbard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks - but its cos i wanna keep it as simple as poss! I don't need client / server control - just ensure only one is running thats all!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!