当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Hello World on RMI

发表于: 2011-08-04   作者:cutesunshineriver   来源:转载   浏览:
rmi
摘要: 1、首先定义服务器上要发布出去的接口,要继承Remote接口。 package org.autumn.rmi.client; import java.rmi.Remote; import java.rmi.RemoteException; public interface IHello extends Remote { String say() throws Remot
1、首先定义服务器上要发布出去的接口,要继承Remote接口。
package org.autumn.rmi.client;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IHello extends Remote {

	String say() throws RemoteException;

}


2、在服务器上实现这个接口,要继承UnicastRemoteObject类。
package org.autumn.rmi.server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.autumn.rmi.client.IHello;

public class HelloImpl extends UnicastRemoteObject implements IHello {

	private static final long serialVersionUID = -22963382911437689L;

	public HelloImpl() throws RemoteException {
		super();
	}

	@Override
	public String say() throws RemoteException {
		return "hello from remote server";
	}

}


3、在服务器上开启目录服务,并注册该实现类。
package org.autumn.rmi.server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import org.autumn.rmi.client.IHello;

public class HelloServer {

	public static void main(String[] args) {
		System.setProperty("java.security.policy", "F:\\Server.policy");
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		try {
			LocateRegistry.createRegistry(1099);
			IHello hello = new HelloImpl();
			Naming.rebind("rmi://192.168.0.193:1099/hello", hello);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

}


Server.policy文件内容如下:
grant  
{  
 permission java.net.SocketPermission  
      "*:1000-9999","accept,connect,listen,resolve";  
}; 


4、将服务器对外发布的接口拷贝到客户端,包括包名。

5、客户端调用程序。
package org.autumn.rmi.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

public class HelloClient {

	public static void main(String[] args) {
		System.setProperty("java.security.policy", "F:\\Client.policy");
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		try {
			IHello hello = (IHello) Naming.lookup("rmi://192.168.0.193:1099/hello");
			System.out.println(hello.say());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}

}


Client.policy文件内容如下:
grant  
{  
 permission java.net.SocketPermission  
      "*:1000-9999","accept,connect,listen,resolve";  
}; 


5、跑HelloServer类,启动服务器服务。跑HelloClient类,进行客户端RMI调用。

   输出:hello from remote server

Hello World on RMI

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号