Classical Singleton Pattern allows to have only one instance of class per JVM. In J2EE environment usually there are more then one JVM, so if we have to have one instance of class among many JVMs, classical approach doesnt work.
Solution is to use RMI and JNDI: you create outside RMI Object and register it in J2EE application server's JNDI tree. Then, inside Servlet or EJB container you have to look up previously registered RMI object (actually youll find RMI Stub) and call some method any it.
Steps to implement J2EE Singleton Pattern for different application servers are described here.
1. Create Remote service interface and Remote service implementation class.
Create Remote service interface that extends java.rmi.Remote and implementation class that extends javax.rmi.PortableRemoteObject.
public interface RemoteService extends java.rmi.Remote {
}
public class RemoteServiceImpl extends javax.rmi.PortableRemoteObject implements RemoteService {
}
Make sure that all method declared in RemoteService throws java.rmi.RemoteException.
2. Create RMI Server that will host RemoteService.
Find one of possible implementations of RMI Server below:
public class RMIServer {
// Properties to obtain application servers initial context. Possible values describes below after Java source.
public static final INITIAL_CONTEXT_FACTORY = factory class//;
public static final PROVIDER_URL = JNDI URL//;
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
// creates object
RemoteService remoteService = new RemoteServiceImpl();
Properties props = new Properties();
// gets initial context
props.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
props.put(Context.PROVIDER_URL, PROVIDER_URL);
// gets initial context
InitialContext context = new InitialContext(props);
// bind remote service to naming context
context.rebind("RemoteServiceJNDI", someRemoteService);
} catch (Exception e) {
e.printStackTrace();
}
}
Possible values of INITIAL_CONTEXT_FACTORY and PROVIDER_URL properties for Weblogic 7.0, Sun One 7.0 and JRun 4.0 application servers:
Weblogic 7.0: weblogic.jndi.WLInitialContextFactory, t3://host:7001
Sun One 7.0: com.sun.jndi.cosnaming.CNCtxFactory, iiop://host:3700
JRun 4.0: jrun.naming.JRunContextFactory, host:2909
Port numbers indicated here are just possible default values.
3. Compile Remote service classes and generate Stub and Skeleton.
Using rmic create Skeleton and CORBA IIOP compatible Stub:
rmic RemoteServiceImpl
rmic -iiop RemoteService
4. Create the client application and deploy it to J2EE application server.
Package RemoteService and _RemoteService_Stub generated by rmic into your J2EE component (war or ejb-jar). In client code InitailContext should be obtain in the same way as in RMI Server:
Properies props = new Properties();
// should be the same as in RMI server class.
props.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
props.put(Context.PROVIDER_URL, PROVIDER_URL)
InitialContext context = new InitialContext(props);
Object obj = context.lookup(RemoteServiceJNDI);
RemoteService service = (RemoteService)PortableRemoteObject.narrow(obj,RemoteService.class);
Note, that Weblogic generates its own stub just after binding. So there is no need to package stub compiled by rmic if you are using Weblogic.
Also note, initial context being obtained as
Context ctx = new InitialContext();
can not be used for lookup of remote object registered from outside.
5. Start up System
Start up the Application Server to make servers JNDI context available.
Start up the RMI Server. RMI Server will register RemoteService in application servers JNDI tree and stay available for remote calls.
Now any part of application could legally lookup and makes a remote call to RemoteServiceImpl.
-
J2EE Singleton (37 messages)
- Posted by: Vadim Gurov
- Posted on: August 08 2003 06:54 EDT
Threaded Messages (37)
- J2EE Singleton by eisen hower on August 13 2003 20:53 EDT
- J2EE Singleton by eisen hower on August 13 2003 20:55 EDT
-
J2EE Singleton by Vadim Gurov on August 14 2003 12:12 EDT
-
J2EE Singleton by Vadim Gurov on August 14 2003 12:13 EDT
-
J2EE Singleton by Suresh Rangan on August 14 2003 06:31 EDT
- J2EE Singleton by Vadim Gurov on August 15 2003 03:40 EDT
-
J2EE Singleton by Suresh Rangan on August 14 2003 06:31 EDT
-
J2EE Singleton by Vadim Gurov on August 14 2003 12:13 EDT
-
J2EE Singleton by Vadim Gurov on August 14 2003 12:12 EDT
- J2EE Singleton and Failover in Bea WLS by Timo Weber on August 22 2003 06:12 EDT
-
So, why do you need a singleton? by Argyn K on August 22 2003 10:12 EDT
-
So, why do you need a singleton? by Timo Weber on August 22 2003 10:51 EDT
-
so, what for is teh singleton in your problem? by Argyn K on August 22 2003 11:16 EDT
-
so, what for is teh singleton in your problem? by Timo Weber on August 22 2003 01:30 EDT
-
I promise by Argyn K on August 22 2003 05:03 EDT
-
I promise by Vadim Gurov on September 01 2003 09:01 EDT
-
what alternatives to singletons are there for sharing resources? by Benjamin Nice on September 11 2003 06:51 EDT
-
Do you really need it? by Oleg Lebedko on September 16 2003 02:15 EDT
- Do you really need it? by maria veera on September 16 2003 03:03 EDT
- Do you really need it? by Vadim Gurov on September 19 2003 10:36 EDT
-
Do you really need it? by Oleg Lebedko on September 16 2003 02:15 EDT
-
what alternatives to singletons are there for sharing resources? by Benjamin Nice on September 11 2003 06:51 EDT
-
I promise by Vadim Gurov on September 01 2003 09:01 EDT
-
I promise by Argyn K on August 22 2003 05:03 EDT
-
so, what for is teh singleton in your problem? by Timo Weber on August 22 2003 01:30 EDT
-
so, what for is teh singleton in your problem? by Argyn K on August 22 2003 11:16 EDT
-
So, why do you need a singleton? by Timo Weber on August 22 2003 10:51 EDT
- Use Entity Beans for Singletons by Charles Medley on August 22 2003 02:33 EDT
-
J2EE Singleton and Failover in Bea WLS by Ajay Amrite on October 21 2003 04:41 EDT
- J2EE Singleton and Failover in Bea WLS by Mikhail Shnayderman on October 24 2003 03:11 EDT
- RE: J2EE Singleton and Failover in Bea WLS by Gabor Kertes on July 22 2004 05:12 EDT
-
So, why do you need a singleton? by Argyn K on August 22 2003 10:12 EDT
- J2EE Singleton by vijay kumar on May 24 2005 05:36 EDT
- J2EE Singleton by eisen hower on August 13 2003 20:55 EDT
- J2EE Singleton by Lester Carlos Garc?a Sierra on August 19 2003 17:35 EDT
- J2EE Singleton by Harsh K on August 19 2003 19:11 EDT
- J2EE Singleton by Ravi Kiran Choppalli on August 20 2003 11:42 EDT
- Who needs Singletons? by Argyn K on August 21 2003 11:12 EDT
- Agreed! by Janek Claus on December 12 2003 10:55 EST
- Rod Johnson's J2EE book by Sean Sullivan on August 21 2003 13:26 EDT
- Rod Johnson's J2EE book by Sergey Pomytkin on December 19 2005 18:33 EST
- True Singletons by TOny Xue on January 13 2004 15:14 EST
- h by Dosu Mahat on January 21 2004 22:12 EST
-
Digression: Avoid using singleton for pooling by nick sheen on May 12 2004 01:13 EDT
- Digression: Avoid using singleton for pooling by Swami Chandra on May 25 2004 08:48 EDT
-
Digression: Avoid using singleton for pooling by nick sheen on May 12 2004 01:13 EDT
- h by Dosu Mahat on January 21 2004 22:12 EST
- JBoss Clustered Singleton by Sean Sullivan on January 15 2004 13:37 EST
- MDB usage by maninder batth on October 20 2004 12:44 EDT
- binding handle at server startup by maninder batth on October 20 2004 12:51 EDT
-
J2EE Singleton[ Go to top ]
- Posted by: eisen hower
- Posted on: August 13 2003 20:53 EDT
- in response to Vadim Gurov
it sounds good ! but it does not work! if the server that the instance was created in is down,the program could lookup the instance in the JNDI tree.
in one word, it does not support failover. -
J2EE Singleton[ Go to top ]
- Posted by: eisen hower
- Posted on: August 13 2003 20:55 EDT
- in response to eisen hower
it sounds good ! but it does not work! if the server that the instance was created in is down,the program [b]could not[/b] lookup the instance in the JNDI tree.
in one word, it does not support failover. -
J2EE Singleton[ Go to top ]
- Posted by: Vadim Gurov
- Posted on: August 14 2003 12:12 EDT
- in response to eisen hower
You are right. But I don't see another solution. If you need failover - make you RMI Server code smart enougth to support it :) -
J2EE Singleton[ Go to top ]
- Posted by: Vadim Gurov
- Posted on: August 14 2003 12:13 EDT
- in response to Vadim Gurov
BTW, the better solution is to use another application server instance (as RMI Server) but with only One JVM instance inside :) -
J2EE Singleton[ Go to top ]
- Posted by: Suresh Rangan
- Posted on: August 14 2003 18:31 EDT
- in response to Vadim Gurov
Hi Guys,
I did experience the same problem when i was using some singletons patterns in my weblogic clustered environment. I used MDB to publish the changes across the cluster to all machines.
ie. The MDB's will be deployed in all the members of the cluster subscribed for the Topic listening for messages. Each Servers will refresh their singleton reference accordingly.
Even in the non clustered app server environment we can make the solution easy by using JMS. A specific member class of all JVM will subscribe for the changes in the topic and refresh their data in memory. This is more scalable approach dont you think so??
Suresh -
J2EE Singleton[ Go to top ]
- Posted by: Vadim Gurov
- Posted on: August 15 2003 03:40 EDT
- in response to Suresh Rangan
Yes, your approach works fine if we are tolking about Single Data Instance (you suggest to replicate data using JMS). But if we need Single Resource Instance that can't be replicated (socket, for example, or file handler) your approach will not work. -
J2EE Singleton and Failover in Bea WLS[ Go to top ]
- Posted by: Timo Weber
- Posted on: August 22 2003 06:12 EDT
- in response to eisen hower
Hi,
I'm working with Bea WLS7. I need a (Singleton)-Pool for Backend-Credentials (many many users and only a few backend-credentials). Our application runs in a cluster with 4-16 nodes.
My first approach to the problem was the RMI solution until I found out that WLS' failover for RMI instances only works for stateless ones by broadcasting the instance to all nodes' jndi-trees once. In other words, no synchronizing and no failover for the pool's state.
Now I've made a StatefullSessionBean from my former RMI-Implementation. At startup of the server (after deployment) I create one instance of this bean. I take the handle from it an bind it to the jndi. WLS then propagates this handle to all nodes. Any "credentials-consumer" on every node can now access this handle and so the same bean instance. WLS can take care of the clustering and failover of SFSBs, so most of my problems are solved...
Except for one thing: Concurrent calls to SFBSs are not allowed in J2EE. The default behaviour of a bean that is called more than once at the same time is to throw a RemoteException. Fortunatly WLS has a setting (don't know exactly about other containers) that allows concurrent calls on SFBSs. WLS even synchronizes the calls.
Regards,
Timo -
So, why do you need a singleton?[ Go to top ]
- Posted by: Argyn K
- Posted on: August 22 2003 10:12 EDT
- in response to Timo Weber
I'm working with Bea WLS7. I need a (Singleton)-Pool for Backend-Credentials (many many users and only a few backend-credentials). Our application runs in a cluster with 4-16 nodes.
I don't see why you need a singleton here.
Usage of statefull EJBs is very very questionable in your case, as in almost every other case. -
So, why do you need a singleton?[ Go to top ]
- Posted by: Timo Weber
- Posted on: August 22 2003 10:51 EDT
- in response to Argyn K
i've thought some time, if i should ignore it, but...
thanx for your short feedback... as far as i can see from your previous posts in this thread i couldn't help getting the feeling that you must have a quite trivial solution for our problems, that you, for some reason only you know, don't want to tell us here ;)
> Usage of statefull EJBs is very very questionable in your case, as in almost every other case.
the fact, that there is exactly one SFSB in the whole cluster, makes it unlikely that there will be a negative impact on our application's runtime behaviour. btw categorical statements like these are the ones i like the most. found it in a book? -
so, what for is teh singleton in your problem?[ Go to top ]
- Posted by: Argyn K
- Posted on: August 22 2003 11:16 EDT
- in response to Timo Weber
the fact, that there is exactly one SFSB in the whole cluster, makes it unlikely that there will be a negative impact on our application's runtime behaviour. btw categorical statements like these are the ones i like the most. found it in a book?
I didn't mean to offend you. If it looked like I did then I'm sorry.
I just don't understand why someone needs a singleton for user credentials. What's so special about your system's credentials?
Re: statfull beans - I donm't need a book. I've done some J2EE (4 years), I used statefull beans only once (unnecessarily, btw). So, it's hard for me to come up with a problem, which requires statefull beans. -
so, what for is teh singleton in your problem?[ Go to top ]
- Posted by: Timo Weber
- Posted on: August 22 2003 13:30 EDT
- in response to Argyn K
no problem :))
ok, i got several legacy-backends that have to be integrated into the application. all these backends use authentication by username and password and each of them got a set (~100..500) of technical users they accept. technical users in this context means, that there's no permanent relationship between a real user (we expect about 2000 concurrent sessions with up to 100 transactions per second). so what has to be done is to assign a technical user to a real user when he needs to contact one or more backends and afterwards take it back after usage, so it can be handed out to another user. a couple of the backends are not able to handle concurrent requests from one authenticated user. that means, if one thread on one cluster node authenticates with one user and at the same time another thread (maybe on another node) uses the same credentials, the whole situation will end up in a desaster (worst case: the backend goes down because of an internal error, very nasty but true). besides this it's one of the customer's requirement that for one backend transaction there must be a 1:1 relation between a real and a technical user, so it is possible to track from presentation-layer into the backends what has been done in one transaction.
btw the credentials that are pooled come from different resources like ldap, dbs and even properties-files and it is likely that technical users must be addable at runtime.
the usual solution in a "native" java enviroment (in one jvm) would have been to use a pooling mechanism based on a singleton. an equivalent solution in our application would be a cluster-wide singleton with high availability (failover).
i hope, i managed to explain, what's our problem, i'm no native english speaker ;)
well, finally some "non-singleton" solutions we thought of:
1) collect credentials from the resources, partion them and share them equally between the nodes. this would be a very easy way to implement, but has some drawbacks. one node crashes and (if there were four) one quarter of our credentials are temporily unavailable, while the cluster has to manage the same load. the number of credentials that one node ones may not be sufficient at all times. adding credentials at runtime becomes nearly impossible.
2) use a database as "singleton"-instance in the application. this is a valid solution in j2ee. the database can make synchronization for me and act like a pool, if i want it to. the problem here is, that we must keep the data consistent with the credential-resources. that would take some extra time to implement the functions for this. knowing the infrastructure of the customer, this also could become a real performance problem.
ok, that's it. if you can think of a better solution i'd be very thankful. i'm not really happy with our solution, as i really don't like messing with the j2ee spec. but, it works fine for now... :)) -
I promise[ Go to top ]
- Posted by: Argyn K
- Posted on: August 22 2003 17:03 EDT
- in response to Timo Weber
ok, i got several legacy-backends that have to be integrated into the application. all these backends use authentication by username and password and each of them got a set (~100..500) of technical users they accept. technical users in this context means, that there's no permanent relationship between a real user (we expect about 2000 concurrent sessions with up to 100 transactions per second).
Now I see you problem. I think that what you call "credential" is not usually called "credential" in this context. That was confusing me.
If I understand you correctly, then your "technical user" is an analog of the connection (e.g. in JDBC). You have a finite number of "technical users", each with its own unique parameters (uname, password,...), and these guys are not thread-safe. I hate to say, singletons :)
I'll think of how to implement this stuff. Entity EJB could be one sub-optimal solution, it's guaranteed to be one istance. -
I promise[ Go to top ]
- Posted by: Vadim Gurov
- Posted on: September 01 2003 09:01 EDT
- in response to Argyn K
I'll think of how to implement this stuff. Entity EJB could be one sub-optimal solution, it's guaranteed to be one istance.
I'm sorry, but you always mix up Data Singletone and Resource (or runtime code) singletone in your posts.
Surely, Entity EJB is guaranteed to be one instance of some Data. But how will you manage such entity as connection? Where will you store it? In any case you will need some code what will be single instanciated and your Entity EJB will be only wrapper for this single instance.
So, as you can see, we have the same problem as in the very beginning.
I want to repeat my statement ones more time:
There are different types of Entities you want to have single instanciated - Serializable and Not Serializable. In J2EE sand-box you can manage only Serializable Entities as singletones. -
what alternatives to singletons are there for sharing resources?[ Go to top ]
- Posted by: Benjamin Nice
- Posted on: September 11 2003 06:51 EDT
- in response to Vadim Gurov
Hi,
I am wondering since so many people think singelton are bad design. How would you design sharing expensive to create resource. Like say JMSSender which shares a JMS Connection yet creates a session for each invoker efficent rather than creating multiple connections and other expensive to create objects. Another shared resource logger better to have a singleton than pass it from class to class? Another program properties so each class can read its properties better than passing the properties object around?I have a registery which is a groups of these shared resources efficently. I would like to here peoples alternatives people to have to singletons for sharing these resources. -
Do you really need it?[ Go to top ]
- Posted by: Oleg Lebedko
- Posted on: September 16 2003 14:15 EDT
- in response to Benjamin Nice
Singleton is always a good candidate to be a bottleneck so you should think twice before you commit to this pattern. In most cases you can and should use Entity beans for 'singleton data' and stateless session beans for shared resources like socket connection.
When I was doing my first EJB project I wanted to implement three EJBs as singletons. Later I found well-working non-singleton solutions for all of them. -
Do you really need it?[ Go to top ]
- Posted by: maria veera
- Posted on: September 16 2003 15:03 EDT
- in response to Oleg Lebedko
Hello,
>In most cases you can and should use Entity beans for 'singleton data'
How did you do this?
Br
VR -
Do you really need it?[ Go to top ]
- Posted by: Vadim Gurov
- Posted on: September 19 2003 10:36 EDT
- in response to Oleg Lebedko
and stateless session beans for shared resources like socket connection.
It's impossible in clustered environment. -
Use Entity Beans for Singletons[ Go to top ]
- Posted by: Charles Medley
- Posted on: August 22 2003 14:33 EDT
- in response to Timo Weber
It should be possible to mimic the behavior of a singleton by using an EntityBean. I think of a singleton as an entity with only *one* valid primary key and design accordingly. -
agree[ Go to top ]
- Posted by: Argyn K
- Posted on: August 22 2003 16:52 EDT
- in response to Charles Medley
Agreed. If I needed singletons (and I don't), then I'd first think of Entity beans. -
J2EE Singleton and Failover in Bea WLS[ Go to top ]
- Posted by: Ajay Amrite
- Posted on: October 21 2003 04:41 EDT
- in response to Timo Weber
Hi Timo Weber,
I am trying to implement a similar solution.
Where in jndi do I store the ejb handle? So that it is accessible from other ejbs.
As far as i know an ejb can access only 'java:comp/env/' in jndi.
Also an ejb is not allowed to make changes to its environment.
Thanks in advance,
Ajay Amrite. -
J2EE Singleton and Failover in Bea WLS[ Go to top ]
- Posted by: Mikhail Shnayderman
- Posted on: October 24 2003 15:11 EDT
- in response to Ajay Amrite
Hi Timo Weber,
>
> I am trying to implement a similar solution.
> Where in jndi do I store the ejb handle? So that it is accessible from other ejbs.
> As far as i know an ejb can access only 'java:comp/env/' in jndi.
> Also an ejb is not allowed to make changes to its environment.
>
> Thanks in advance,
>
> Ajay Amrite.
Hi Ajay,
I think you are right in terms of JNDI usage by EJBS, but I am also
sure that to bind the instance handle you could use somethig that is guaranteed
to preceed your application usage.
An example of such a beast in WebLogic is Startup/Shutdown classes.
What I am not sure is if startup/shutdown classes started after deployment of EJB Modules or bvefore. If after, you could use startup class that does exactly that (binds EJB to a JNDI namespace).
You could then access the instance from youtr other beans, ...
This is just a shot in the dark, but I would investigate this approach if
I had to do what Tim has done.
Later, Mikhail -
RE: J2EE Singleton and Failover in Bea WLS[ Go to top ]
- Posted by: Gabor Kertes
- Posted on: July 22 2004 05:12 EDT
- in response to Timo Weber
It looks that the solution Timo described is suitable to our situation as well. Dit it work, finally? Is there any final conclusion to this thread after your implementation, Timo? -
J2EE Singleton[ Go to top ]
- Posted by: vijay kumar
- Posted on: May 24 2005 05:36 EDT
- in response to eisen hower
We can persist the Singleton Class into the database in bytes and use it as a global -
J2EE Singleton[ Go to top ]
- Posted by: Lester Carlos Garc?a Sierra
- Posted on: August 19 2003 17:35 EDT
- in response to Vadim Gurov
Hi, I did something like this before. My failover solution was to include a dummy method at the RMI Server class. Then I used a second simple singleton as the proxy for the remote one. In case of exception I try to use the dummy method(a very trivial one) and in the case the exception persists I assume the reference is invalid and should be recreatead and rebinded at the JNDI. Of course, the data get lost anyway but you can have some kind of universal singleton.
I have it working in a uniqueID generator for EJB factory based on the Ed Roman's pattern(Sequence Blocks).
Lester -
J2EE Singleton[ Go to top ]
- Posted by: Harsh K
- Posted on: August 19 2003 19:11 EDT
- in response to Vadim Gurov
I think if you are in an environment where Singleton use is prohibitive -- which is practically just about all production J2EE scenarios -- you might consider exposing the singleton as a service.
Your current design, I don't think this qualifies as a pattern in design more so in implementation -- no disrespect intended, would achieve that in effect anyway by providing a lookup thru JNDI.
Exposing this singleton as a service in no way prohibits it's restriction to a single VM and/or a feasible distributed/local approach if the reqs change.
All constructive criticism .. your approach is quite valuable. I am suggesting an alternate modus operandi ... -
J2EE Singleton[ Go to top ]
- Posted by: Ravi Kiran Choppalli
- Posted on: August 20 2003 11:42 EDT
- in response to Harsh K
The best solution would be in the case you are using J2EE is JNDI. You can bind the static instance to a JNDI Name and refer to the same JNDI name across JVM's.
Thanks,
Ravi. -
Who needs Singletons?[ Go to top ]
- Posted by: Argyn K
- Posted on: August 21 2003 11:12 EDT
- in response to Vadim Gurov
I think that if you need a Singleton, then something's wrong with your design.
Can someone explain me when is Singleton required in J2EE application? -
Agreed![ Go to top ]
- Posted by: Janek Claus
- Posted on: December 12 2003 10:55 EST
- in response to Argyn K
Singletons inherently don't support scaling an clustering. So if you need to develop a clustered solution, don't use singletons as they are violating the very rule of a reliable/clusterable/failover architecture! -
Rod Johnson's J2EE book[ Go to top ]
- Posted by: Sean Sullivan
- Posted on: August 21 2003 13:26 EDT
- in response to Vadim Gurov
Rod Johnson discusses Singletons and J2EE in his book "J2EE Design
and Development".
It is definitely worth reading. -
Rod Johnson's J2EE book[ Go to top ]
- Posted by: Sergey Pomytkin
- Posted on: December 19 2005 18:33 EST
- in response to Sean Sullivan
Rod Johnson discusses Singletons and J2EE in his book "J2EE Design
It’s indeed worth reading, but you rather get 101 reason to do not use singleton in J2EE environment and 99 ways to avoid it that 1 solution for singleton in cluster. There are proprietary vendor solution - I believe WebSphere, jBoss offering fault tolerance solution for singleton services, WebSphere have SharedMap as solution for data .. you also can use (or abuse) JNDI for some cases. Generally I’d love to see JSR for it for now we doomed to custom implementations JMS, JNDI or Entity EJB based.
and Development".
It is definitely worth reading. -
True Singletons[ Go to top ]
- Posted by: TOny Xue
- Posted on: January 13 2004 15:14 EST
- in response to Vadim Gurov
We usually use the singletons to hold configuration data. It's pretty easy to gurantee a single instnace in one application, such as when one EAR file is deployed. This is a packaging issue. Different app server will have different class loading hierarchy.
Problem is when there are multiple applicaiton deployed either in one JVM or Multiple JVMs such as in a clustering enviroment, How do we keep multiple instances of the singleton synchronized?
Our soluton is to use the multicast. when the singleton in one node changes state, it needs to send multicast message out to all the nodes. The nodes receives it and update the changed fields accordingly. JavaGroups is a good implementation of multicasting. Even better, the Jboss provided the TreeCache that allows you not evening knowing about multicasting.
About the author's suggested solution: "Solution is to use RMI and JNDI", the performance will be a big problem. -
h[ Go to top ]
- Posted by: Dosu Mahat
- Posted on: January 21 2004 22:12 EST
- in response to TOny Xue
h -
Digression: Avoid using singleton for pooling[ Go to top ]
- Posted by: nick sheen
- Posted on: May 12 2004 01:13 EDT
- in response to Dosu Mahat
This is no answer if there is a genuine need for a singleton such as the one described above by Timo. But in reply to "what alternatives to singletons are there for sharing resources?":
However, if all you want is pooling of some expensive resource, you can hold create or obtain a reference to the resource in the ejbCreate (for stateless SB especially) and keep the reference as an instance field of the ejb. Since the SB is pooled by the container according to the load, you get automatic pooling of the resource according to load. It is also nice and easy to code... -
Digression: Avoid using singleton for pooling[ Go to top ]
- Posted by: Swami Chandra
- Posted on: May 25 2004 08:48 EDT
- in response to nick sheen
I came across a situation once where in I had to design a scheduler (running on a App Server) that should read a bunch of DB tables and invoke an EJB. Leaving the details asidem, the design requirement (limitation??) was to have only one instance of the scheduler program running at any point of time. I think this pattern fitted the best in such a scenario. But not a true fan of this pattern anyway. -
JBoss Clustered Singleton[ Go to top ]
- Posted by: Sean Sullivan
- Posted on: January 15 2004 13:37 EST
- in response to Vadim Gurov
JBoss 3.x offers support for a "Clustered Singleton"
This article explains it all:
http://www.onjava.com/pub/a/onjava/2003/08/20/jboss_clustering.html -
MDB usage[ Go to top ]
- Posted by: maninder batth
- Posted on: October 20 2004 12:44 EDT
- in response to Vadim Gurov
I am highly skeptical of MDB solution to act as an singleton or maybe i did not understand the solution :-|
The mdb solution sounds more like read mostly pattern where create/update/delete operations are propogated to all clusters to keep their copies fresh "after" the operation has taken place...
This solution does not prevent where an operation should be allowed only once. For example, 2 threads enter in 2 entity instance at same time in 2 different machines in cluster to create a certain record. Requirements are one of them should fail as that record should be created only once. Since both threads entered the entity beans in different machines in the cluster at the same time, both will successfully create the same record which defeats the purpose... -
binding handle at server startup[ Go to top ]
- Posted by: maninder batth
- Posted on: October 20 2004 12:51 EDT
- in response to Vadim Gurov
The solution which suggested creating a bean at startup and bind it to jndi, works only because the bean is created at startup :(. Unfortunately, this solution would not work if the "luxury" for creating the bean at startup time is not avilable..... Any suggestions for singletons which would work anytime ....