0

How to create a real singleton for a whole computer in Java?

I have a console app written in Java. if application is executed for a second time I want it to know that the main application is already running (and executing jobs). The second execution will just allow the user to query data (jobs). I know I can create a file and write everything to it, but then I will have to also support computer resets, etc.

Is there a better way to do it?

to summarize:

  • I start the app. It has jobs and execute them at wanten interval.
  • I start app for a second time. I get message that jobs are already running and I can query data from the first app.
  • Start app for 3rd time and it works the same as the 2nd time.
  • If I stop the first instance and start a new one it works like the first instance.

P.S. I do know this question must have been asked more then once, but google and StackOverflow search give me links only relative to Singleton Design Pattern.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • You do this with socket communiction or with something like [terracotta](http://terracotta.org/) – Boris the Spider Feb 23 '13 at 15:53
  • 1
    Singletons aren't a good idea for a single JVM; I think they're even worse for all JVMs on a machine. – duffymo Feb 23 '13 at 15:53
  • 6
    Have your first app connect to a socket as a server. The others will not be able to bind to that port as a server until the first one dies. As a bonus, the others could then bind as a client and use that socket to communicate between them. – Paul Tomblin Feb 23 '13 at 15:53
  • 1
    You will need *some* kind of inter-process communication, be it files or sockets or even shared memory. What's wrong with using a simple file? – Niklas B. Feb 23 '13 at 15:53
  • @RC Oh come on... Zookeper is way too overkill for a task like this one. – Xion345 Feb 23 '13 at 15:57
  • @duffymo what's wrong with singletons? Do you have a link to a rant somewhere? I'd be interested to read it. – Bohemian Feb 23 '13 at 15:57
  • 1
    Sure, here's what Google thinks about Singletons: http://code.google.com/p/google-singleton-detector/ – duffymo Feb 23 '13 at 19:23
  • but that is about Singleton Design Pattern. its over-used, but I'm talking about real Singleton here. – IAdapter Feb 23 '13 at 21:17

2 Answers2

3

Here is a link that'll do what you want.

This provides an example of code that binds to a socket at startup and then if something is bound at startup then it sends a message

You can use this to communitcate between your processes.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • The example specifies a single socket port explicitly. This can be troublesome if some other application uses the same port. An alternative is to allocate the port dynamically, record the port somewhere well-defined (file,db,registry,...), connect to it and confirm identity. – Andy Thomas Feb 23 '13 at 16:18
2

As @PaulTomblin mentioned in the comments, you should probably bind a server socket to a specific port. This serves 2 purposes. If the port is currently unbound, then the running process becomes the main instance. If the port is already bound, then the running process becomes a client and instead connects to the port in order to communicate with the primary instance.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118