This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Service Locator in distributed application

 
Alena Al
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

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???

Many thanks in advance
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alena,

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.
 
Alena Al
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 4.4.8.3 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.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 J2EE Patterns, 2nd Edition - Service Locator

Or get a copy of



Regards,
Darya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic