Service Locator Mode
Service Locator Pattern is used when we want to use JNDI queries to locate various services. Considering the high cost of finding JNDI for a service, the Service Locator pattern takes advantage of caching technology. When a service is first requested, the service locator looks up the service in JNDI and caches the service object. When the same service is requested again, the service locator looks in its cache, which can greatly improve the performance of the application. The following are the entities of this design pattern.
- Service - The service that actually processes the request. A reference to this service can be found in the JNDI server.
- Context / Initial Context - The JNDI Context has a reference to the service you are looking for.
- Service Locator - The Service Locator is a single point of contact for services through the JNDI lookup and caching service.
- Cache - Cache references to storage services to reuse them.
- Client - Client is the object that invokes the service through ServiceLocator.
We will create ServiceLocator, InitialContext, Cache, Service as various objects representing entities. Service1 and Service2 represent entity services.
ServiceLocatorPatternDemo, our demo class is here as a client that will use ServiceLocator to demonstrate the service locator design pattern.
Create a service interface Service.
Create an entity service.
Create an InitialContext for a JNDI query.
Create a cache Cache.