hessian

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.caucho.hessian.client.HessianProxyFactory;


@SuppressWarnings("unchecked")
public class RemoteServiceFactory {

	private static Map<Class, Object> services = new HashMap<Class, Object>();
	private static String timeOut = RemoteServiceConfigUtil.getConfig().get("timeout");
    private static HessianProxyFactory factory = null;
    
    static{
    	factory = new HessianProxyFactory();
    	factory.setOverloadEnabled(true);
    	if(StringUtils.isBlank(timeOut))
            timeOut = "15000";
		factory.setReadTimeout(Integer.parseInt(timeOut));
    }
		
	public static synchronized <T> T getService(Class<T>  classClass) {
		Object service = services.get(classClass);
		if (service == null) {
			String className = classClass.getName();// 取得接口类的全限定名
			String url = RemoteServiceConfigUtil.getConfig().get(className);// 通过接口类的全限定名,到diguInterset.properties文件中,查找到实现类的全限定名
			try {
				service = factory.create(classClass, url);
				services.put(classClass, service);// 将实例化的业务层类缓存起来,以便下一次调用的时候直接取出来使用
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return (T)service;
	}
	
}
posted @ 2012-03-23 19:00  菜园仔  阅读(254)  评论(0编辑  收藏  举报