I've got a question about Service Locator in distributed application. Well, Service Locator is a singleton so it has a static field that actually contains its instance. Now, there is a rule that we should NOT use static variables unless they are final from EJB because non-final static field will NOT work in clusters. Hence is the question, how are we supposed to use Service Locator in distributable application if it has a static which breaks the rule about not using static field for distributable applications???
I wonder that EJB (or better the EJB spec.) does say anything about Clusters. This is out of scope of the EJB spec and is always vendor dependent.
Can you show the link where this rule come from?
If you want a Service Locator for a remote EJB then this Locator class does not go with you other EJB stuff to the EJB container but should be included in your Client JAR file.
SCJP, SCJD, SCWCD, SCBCD
Joined: Jun 14, 2006
Here area a few links for your information. They all say that static variables should NOT be used either in ejb or servlets. If they are talking about servlets it still applies to the nature of my questions since Service Locator is intended to be used from both servlet and ejb. If you do not want to read the whole thing here is quotes from sources:
http://www.onjava.com/pub/a/onjava/excerpt/java_servlets_ch12/index.html?page=2 "...to be deployed in a distributed environment. Consider that different instances of the servlet may exist on each different JVM and/or machine. Therefore, instance variables and static variables should not be used to store state. Any state should be held in an external resource such as a database or EJB (the servlet's name can be used in the lookup)." http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html 126.96.36.199 Distributable Servlet Restrictions "...Don't store application state in static or instance variables--Web containers are not required to maintain static or instance variable values when a session migrates. Code that depends on state stored in such variables will likely not operate properly after session migration. Such state should be stored either as a session attribute, in an enterprise bean, or in a database"
So with all that , the question remains unanswered.
Joined: Aug 21, 2004
When they say, instance variables and static variables should not be used to store state, I don't see where it hurts you.
Since your Service Locator does not cache or handle any application state you don't need to care about that sentence. You still can implement your Locator as a Singleton be it for a local or remote lookup.
I don't know the context in which you use your Service Locator exactly but it should be like in following image:
For a much better description look at Core J2EEPatterns, 2nd Edition - Service Locator