⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remoteservicelocator.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;


import org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import com.cownew.PIS.framework.common.SysConstants;

import com.cownew.PIS.framework.common.services.ILoginService;
import com.cownew.ctk.common.DateUtils;
import com.cownew.ctk.common.StringUtils;

public class RemoteServiceLocator
{
	private final static URL serverURL;

	private static String sessionId;

	public static void initLocator(String sessionId)
	{
		RemoteServiceLocator.sessionId = sessionId;

		//启动心跳计时器,一分钟以后开始,每分钟心跳一次,这样就可以防止线程超时退出
		//设置成监控线程,防止造成程序无法退出
		Timer heartBeatTimer = new Timer(true);
		int oneMin = DateUtils.ONE_MINUTE;
		heartBeatTimer.schedule(new HeartBeatTimerTask(), oneMin, oneMin);
	}

	public static String getSessionId()
	{
		return sessionId;
	}

	/**
	 * 这里有个约定,spring配置文件中bean的名称和接口的名字相同, 比如com.cownew.ITest
	 * 对应的bean名称com.cownew.ITest
	 */
	public static Object getRemoteService(Class serviceIntfClass)

	{
		HttpInvokerProxyFactoryBean proxyFactory = new HttpInvokerProxyFactoryBean();
		CommonsHttpInvokerRequestExecutor reqExecutor = new CommonsHttpInvokerRequestExecutor();
		proxyFactory.setHttpInvokerRequestExecutor(reqExecutor);
		try
		{
			String serviceIntfName = serviceIntfClass.getName();
			String serviceURL = serverURL.toString() + serviceIntfName;
			if (!StringUtils.isEmpty(sessionId))
			{
				StringBuffer sb = new StringBuffer();
				sb.append(serviceURL).append("?")						
				  .append(SysConstants.SESSIONID)
				  .append("=").append(sessionId);
				proxyFactory.setServiceUrl(sb.toString());
			} else
			{
				proxyFactory.setServiceUrl(serviceURL);
			}

			proxyFactory.setServiceInterface(serviceIntfClass);
			proxyFactory.afterPropertiesSet();
			Object service = proxyFactory.getObject();
			return service;
		} catch (MalformedURLException e)
		{
			throw new RemoteServerException(
					RemoteServerException.CONNECTSERVERERROR, e);
		}

	}

	/**
	 * 登出服务器
	 */
	public static void logout()
	{
		ILoginService login = (ILoginService) RemoteServiceLocator
				.getRemoteService(ILoginService.class);
		// 登出服务器
		if (sessionId != null)
		{
			login.logout(sessionId);
		}

		// 清除客户端上下文
		sessionId = null;
	}

	static
	{
		serverURL = ClientConfig.getInstance().getServerURL();		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -