Service Locator Mode in Design Mode

1. Introduction

In the novice bird tutorial:
Service Locator Pattern is used when we want to locate services using JNDI queries.Given the high cost of finding a JNDI for a service, the service locator mode takes full advantage of caching technology.When a service is first requested, the service locator looks for the service in JNDI and caches the service object.When the same service is requested again, the service locator looks in its cache, which greatly improves the performance of the application.Here are the entities for this design pattern.
Service - The service that actually processes the request.References to this service can be found in the JNDI server.
Context / The initial Context - JNDI Context has a reference to the service to find.
Service Locator - A service locator is a single point of contact for services through JNDI lookup and caching of services.
Cache - Cache stores references to services so they can be reused.
Client - An object that invokes a service through a ServiceLocator.

Obtain service resources from the serviceLocator in the current service locator, and use specific terms to obtain a specific service. (Cached data is returned in the cache, or initContext is used to get the service)

2. Examples

Simulate resource lookup

3. Create interfaces

Content in Service:

/**
 * @description Service Interface
 * @author hy
 * @date 2019-09-03
 */
public interface Service {
	//Service Name
	String getName();
	
	//Service can be invoked
	void execute();
}

4. Create other classes

Content in JNDIService:

/**
 * @description JNDI Service Class
 * @author hy
 * @date 2019-09-03
 */
public class JNDIService implements Service {

	@Override
	public String getName() {
		return "jndi";
	}

	@Override
	public void execute() {
		System.out.println("Access the current JNDIg Service!");

	}

}

Content in DataSourceService:

/**
 * @description Data Source Resource Class
 * @author hy
 * @date 2019-09-03
 */
public class DataSourceService implements Service{

	@Override
	public String getName() {
		return "dataSource";
	}

	@Override
	public void execute() {
		System.out.println("Get data link resources!");
		
	}

}

Content in InitContext:

/**
 * @description Classes that initialize the current context
 * @author hy
 * @date 2019-09-03
 *
 */
public class InitContext {
	public Service lookUp(String name) {
		Service service=null;
		switch (name) {
		case "jndi":
			service=new JNDIService();
			break;
		default:			
			throw new IllegalArgumentException("No, "+name+" Service!");
		}
		return service;
	}
}

Contents in Cache:

public class Cache {
	private Map<String, Service> cache=new HashMap<String, Service>();
	
	//Get the contents of the cache
	public Service getCache(String name) {
		Service service = cache.get(name);
		return service;
	}
	//Add Cache
	public void addCache(String name,Service service) {
		cache.put(name, service);
	}

}

Contents in ServiceLocator:

/**
 * @description Service Finder
 * @author hy
 * @date 2019-09-04
 */
public class ServiceLocator {
	private Cache cache;
	private InitContext initContext;
	public ServiceLocator() {
		initContext=new InitContext();
		cache=new Cache();
	}
	//Get Services
	public Service getService(String name) {
		Service service = cache.getCache(name);
		if(service==null) {
			System.out.println("Get current by initializing resources jndi Resources!");
			service = initContext.lookUp(name);
			cache.addCache(name, service);
			System.out.println("Add the acquired resource to the cache!");
		}
		System.out.println("Acquired jndi Resources!");
		return service;
	}
	
	
}

5. Create test classes

Contents in ServiceLocatorTest:

/**
 * @description Test: Service Location Mode
 * @author hy
 * @date 2019-09-03
 */
public class ServiceLocatorTest {
	
	public static void main(String[] args) {
		ServiceLocator locator=new ServiceLocator();
		Service service = locator.getService("jndi");
		service.execute();
		service = locator.getService("jndi");
		service.execute();
	}
}

Result:

Get the current jndi resource by initializing the resource!
Add the acquired resource to the cache!
jndi resources have been acquired!
Access the current JNDI service!
jndi resources have been acquired!
Access the current JNDI service!

6. Summary

1. Service Locator mode uses caching and initContext to get resources through service location, caches resources for caching, and initContext gets resources through lookup!

The above views are purely personal, if you have any questions, please contact me!

Added by Giddy Rob on Wed, 04 Sep 2019 04:09:56 +0300