`

RMI简单实现

JDK 
阅读更多
	/**
	 * @param args
	 */
	// TODO Auto-generated method stub
	public static void main(String[] args) throws RemoteException, Exception {
		// 通过JNDI查找远程服务
		Sev sev = (Sev) Naming.lookup("rmi://192.168.1.135:2000/fdf");
		// 调用远程方法
		System.out.print(sev.helloWorld("yeeku"));
	}


public interface Sev extends Remote {
	String helloWorld(String name) throws RemoteException;
}


public class SevImpl extends UnicastRemoteObject implements Sev {

	// 远程服务类必须拥有构造器,且构�?�器必须抛出RemoteException异常
	protected SevImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 实现Remote接口必须实现的方�?
	public String helloWorld(String name) throws RemoteException {
		// TODO Auto-generated method stub
		return "你好:" + name;
	}

	// 下面是服务类的本地方法,不会“暴露�?�为远程服务�?
	public void info() {
		System.out.print("我是本地方法");
	}

	// 下面提供程序入口,将远程类实例绑定为本机的服务�??
	public static void main(String[] args) throws Exception {
		// 创建远程服务类实�?
		Sev imp = new SevImpl();
		// 注册远程服务的端�?
		LocateRegistry.createRegistry(2000);
		// 将远程服务实例绑定为远程服务�?
		Naming.rebind("rmi://192.168.1.135:2000/fdf", imp);
		System.out.print("Serv Started!!!");
	}

}

 

*注意:倘若报错有可能是RMI的编译环境没设设置好

右键项目->Properties->RMI Compiler Properties->Use non-standart tools.jar location勾上,然后Browse你的jdk中lib中的tools.jar。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics