[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

How to guarantee one singleton instance in a cluster

I want only one instance of a class running on one server in a cluster.  I then want  to be able to call this object from other servers in the cluster and execute its methods.

To do this I would need to make this a remote rmi object and then syncronise access to this object from the client tier.

My initial thought is to implement the rmi as ejbs and have the ejb acess a singleton class, The singleton class would have a reference to a hashtable which is required to store data, a hashtable which by default is thread-safe.  This is important as whilst one thread updates the hashtable I dont want other threads to block.

Please provide guidance how to do this as this is giving me alot of grief.
0
inzaghi
Asked:
inzaghi
  • 5
  • 4
  • 4
  • +1
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Are you sure you want to use EJBs? If so, you would need an EJB Container too.... if you want to have only a singleton instance of a class, you can achieve it this way:

public final class Singleton
{
  private static Singleton instance ;
  // declare any other data-members you want to keep

  private Singleton () // the constructor is given private access so that the class is not instantiated from outside
  {
    // initialize any instance data-members

  } // end of constructor ()

  public static Singleton getInstance ()
  {
    if ( instance == null )
      instance = new Singleton () ; // end if

    return instance ;

  } // end of getInstance ()

  // provide getters for your data-members

} // class definition over
0
 
cjjcliffordCommented:
I guess that by "cluster" you mean a clustered container/application server? If this is the case, using standard singletons will not work as each JVM in the cluster will get a singleton.

Do you really need a singleton? Would it be possible to use part of the clustered container - e.g. you could represent this data as an entity bean, backed by a table in a database - this would then be managed by the container, and then have all access through a session bean... Then just restrict the Session Bean to limit the number of such entity beans to the number of entries you actually want to store...

If you don't like entity beans, then there are several clustered cache libraries out there (never used any, so I can't recommend one), that should work for you...

Cheers,
C.
0
 
aozarovCommented:
mayankeagle,  you probably want to add synchronized to your getInstance method.
Also, this will not be a singleton if loaded from different unrelated class loaders.

cjjclifford, there are application servers which alow you to define a singlton service per cluster (W.L and Jboss among them)
W.L -> http://e-docs.bea.com/wls/docs81/cluster/best.html#1039384
JBoss -> http://www.onjava.com/lpt/a/4087
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cjjcliffordCommented:
aozarov, the singleton service looks very interesting, we use JBoss, but not yet in a clustered environment (at least, not the part that JBoss is involved with...)

However, I think there would still have to be some underlying persistence, or sharing, since the author wants to manage a hashtable in the singleton.

I was just wondering if a singleton is the best approach, especially in J2EE clustered environment...

Cheers,
C.
0
 
aozarovCommented:
Singleton service does make sensce in cases where it is not only data that needs to be shared amont the nodes in the cluster but you also
want to centrelized the logic that process this data.
This is why all major application server vendores came up with a solution for it (cluster strategies is not part of the J2EE spec so far).
0
 
cjjcliffordCommented:
I Agree, it definitely does make sense (I've worked on different clustered systems (proprietary mostly) so I am well aware of the need to do this kind of work.

however, since the requirement seems to be providing a controlled, managable, interface to modifications to some data (i.e. that "stored in the hash table"), while allowing read access on other "threads" I was just wondering if a singleton was really necessary...

And also, with singleton services sitting on top of some application data, there would have to be application logic to persist this data across instantiations of the singleton, etc...

Cheers,
C.
0
 
inzaghiAuthor Commented:
I have read the weblogic documentation as mentioned above, it does not mention how to implement this?  Any ideas would be greatfully appreciated.
0
 
inzaghiAuthor Commented:
Pinning the application to one node provides only single point of fail over. Problem here is if this server goes down.
0
 
cjjcliffordCommented:
Hi,

The application server in question "should" provide a mechanism for automatically moving the singleton service to a secondary node on the cluster - at least that's what I gleamed from the JBoss doc. linked above, I'm guessing its similar with other servers (it makes little sense otherwise...)

Cheers,
C.
0
 
inzaghiAuthor Commented:
IS this provided in weblogic 8.1
0
 
aozarovCommented:
Yes, "Configure Migratable Targets for Pinned Services"
0
 
inzaghiAuthor Commented:
To implement this solution as pinned service would this be implemented as a
RMI solution where this would reside on one of the managed servers in the cluster.
I am new to pinned services, can the rmi stub be pinned down as a migrated service. I read the documentation regarding weblogic migrating pinned services and it mentions that JTA and JMS can be pinned down.  Can an RMI stub be pinned down as well?

0
 
aozarovCommented:
Using W.L RMI you can create pinned RMI stubs http://e-docs.bea.com/wls/docs81/rmi/rmi_api.html#1000013975
unfortunatly for the 8.1 release migration is supported only for JMS servers and the JTA transaction recovery services.
Where JBoss actually does.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now