Reputation: 1436
Is It Possible to achieve Singleton for any given class on two JVM Machine.? Here My requirement is to achieve Singleton on two JVM machine lets say code is deployed on two machines
Upvotes: 0
Views: 1453
Reputation: 6167
No You cannot implement it. totally agree with Davide Lorenzo. if you try o do so you will have to either use reflection(a bad way though) or you will break the singleton property.
Upvotes: 0
Reputation: 1226
Typically no, but you can implement it in logical way using some kind of distributed caching. E.g. Distributed Ehcache (http://ehcache.org/documentation/2.6/get-started/about-distributed-cache)
Here you instantiate the singleton in one JVM and immediately put it in distributed cache which serializes it across all JVMs. So all JVMs have same copy of the singleton (now its no more singleton in theoretical way but logically it is the same object across all JVMs).
Upvotes: 2
Reputation: 26926
No. The singleton pattern is appliable only for a single JVM.
You can use other approaches (not singleton) to have a single object instance for a number of JVM greater than one. For that you need a central repository to store information on wich JVM is storing the single instance of your object. This could be a centralized database or a centralized file.
Upvotes: 5