aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Service Locator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Service Locator" Watch "Service Locator" New topic
Author

Service Locator

roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
Hi all,
I've got another question about the service locator:
If a client invokes a getInstance on the servicelocator, it gets an instance, without any problem.
But what is two clients invoke the getInstance method at the same time?
What does happen?
Could someone help me with this?
Below is a part of the code i use.
public class ServiceLocator {
private InitialContext ic;
private Map cache;
private static ServiceLocator me;
static {
try {
me = new ServiceLocator();
} catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}
private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw new ServiceLocatorException("ne");
} catch (Exception e) {
throw new ServiceLocatorException("e");
}
}
static public ServiceLocator getInstance() {
return me;
}
//...
}


MSc Electronics, ICT
roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
An other question:
what happens if two clients try to access the cache at the same time?
Roul
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Roul Ravashimka:
But what is two clients invoke the getInstance method at the same time?
What does happen?
[/QB]

They both get a reference to the same instance, according to the above code.
What is your doubt?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Roul Ravashimka:
what happens if two clients try to access the cache at the same time?

That depends on how cache access is implemented. Your code doesn't show that.
roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
That depends on how cache access is implemented. Your code doesn't show that.

the cache is implemented as a hashmap or a map.
i thought that two users couldn't access the same cache at the same time. I thought the cache was locked while a user reads the cahce.
If it's locked and a second users tries to read it, it get ???
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Roul Ravashimka:
i thought that two users couldn't access the same cache at the same time. I thought the cache was locked while a user reads the cahce.

This is indeed true since you wrapped the HashMap in a SynchronizedMap. If one thread has the map locked, other threads "block" on their access, meaning they must wait until the first thread completes. As long as you don't store too many items in your map or have a ton of threads, you won't notice a problem.
Another option is to use a more performant synchronization data structure. Doug Lea's concurrent package has some very nice structures. For example, you could use a WriterPreferenceLock to allow multiple threads to *read* the map but only one thread can *write* to it (and no readers). Since you'll be putting objects rarely and doing lookups far more often, this allows more threads to hit the map simultaneously.
roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
so in the ejb-layer, you've got to use a hashmap wrapped in a synchronised map.
while in the presentation layer, when you work with for instance struts, it is not nessecary to do this. because all requests(of the same type) create a new thread.
If two request of the same type arrive, they are handled by the same thread(one after another). --> can not give no accessing problem.
If two requests of a different type arrive, then you've got two threads are created. --> no accessing problem
(If you suppose the scoop is request)
Is this correct?
Roul
roul ravashimka
Ranch Hand

Joined: Mar 16, 2004
Posts: 53
shouldn't the getInstance method be synchronised?
this is the data access code:
public EJBLocalHome getLocalHome(String jndiHomeName)
throws ServiceLocatorException {
EJBLocalHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome) cache.get(jndiHomeName);
}
else {
home = (EJBLocalHome) ic.lookup(jndiHomeName);
cache.put(jndiHomeName, home);
}
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}
public EJBHome getRemoteHome(String jndiHomeName,
Class className)
throws ServiceLocatorException {
EJBHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBHome) cache.get(jndiHomeName);
}
else {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref
, className);
home = (EJBHome) obj;
cache.put(jndiHomeName, home);
}
}
catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Service Locator