5

When an Application is Distributed across multiple JVMS my single ton class will have multiple instances at each JVM. I have to generate a Unique ID for this purpose i have to use a singleton pattern class. It is Working fine when in a Standalone Environment. How to make a Singleton pattern in distributed environment so that we can use only one instance ?

EDIT: For my application i need to create userid like

if the name is like Pavan Kumar the userid should be pavankumar if in the system already if this userid exists the userid should be like pavankumar1,pavankumar2 etc......

If the multiple requests for users having same name comes across different servers might result in duplicate id. For this purpose i wanted to use a singleton across distributed environemnt.

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
  • "I have to generate a Unique ID for this purpose" what do you mean by this? Do you want the same id on all signlton classes? – Sap Sep 14 '11 at 04:36
  • in my application for every user i need to generate a unique id..... – Dungeon Hunter Sep 14 '11 at 04:38
  • Maybe describe a bit more about why you need this singleton object - when is it created and accessed, and why is there a problem if there are multiple instances in different machines? – Brian L Sep 14 '11 at 05:05
  • @Sannisetty Pavan Does it mean that there is a class in your app that creates a UUID for each user and you want to make that class a singlton? In such a case what is the problem if this class gets created on all the VMs. If possible post the code in question. – Sap Sep 14 '11 at 05:14
  • i need to create it in Tivoli Directory Server LDAP........ – Dungeon Hunter Sep 14 '11 at 06:21

5 Answers5

4

I have found a solid answer for this question after a long time. It is possible to implement a Singleton in a distributed environment using AKKA toolkit. Details on how to implement can be found from here http://doc.akka.io/docs/akka/2.3.1/contrib/cluster-singleton.html

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
3

You can try out hazelcast [ doc ].This library allows you to have distributed locks and data structures using which you could write your singleton.

Emil
  • 13,577
  • 18
  • 69
  • 108
2

Have you considered using a distributed cache like Java Caching System from Apache Commons or Coherence Cache from Oracle? It may be overkill, but it really depends on what exactly you are wanting to do.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
2

You can use a central place (like database) to generate the ids. Of course you need to do appropriate locking on the resource so that it is modified by only one entity at a time.

Nrj
  • 6,723
  • 7
  • 46
  • 58
2

If you are going to persist the entity to a db, you can rely on the db sequence to generate you a id; in short invert the solution from having multiple jvm's generate unique id to a single db doing that. Apart from that there are solutions like @nicholas mentioned that can be leveraged depending upon your needs and scale.

Scorpion
  • 3,938
  • 24
  • 37