J2EE RMI搭建

阅读更多

RMI接口:

package com.ynitil.sinfo.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Map;

/**
 * 远程RMI 页面采集接口
 * 
 * 
 */
public interface IPageCollectRMI extends Remote {
	public String test(String str)throws RemoteException;
}

 

 

接口实现类:

package com.ynitil.sinfo.rmi;

 /**
 * 远程RMI 页面采集接口实现类
 * 
 * @author 张飞
 * 
 */
 
public class PageCollectRMIImpl extends UnicastRemoteObject implements
		IPageCollectRMI {
	public PageCollectRMIImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	private static final long serialVersionUID = 1L;
	 public String test(String str) throws RemoteException{
		// TODO Auto-generated method stub
		return  "打印:"+str;
	}
	

}

 

RMI服务器启动:

try {
			//启动RMI服务 
			LocateRegistry.createRegistry(7890);//rmi端口
			PageCollectRMIImpl collectRMIImpl=	new PageCollectRMIImpl();
			Naming.rebind("//localhost:7890/pageCollectRMI", collectRMIImpl);//绑定
			System.out.println("RMI[PageCollectRMIImpl]启动成功");
		} catch (Exception e) {
			System.out.println("RMI[PageCollectRMIImpl]启动失败");
			e.printStackTrace();
		}

 

客户端:

 

public class AllTests {

 
	 public static void main(String[] args) 
	    { 
	        try 
	        { 
	        	IPageCollectRMI hello=(IPageCollectRMI)Naming.lookup("//192.168.3.189:7890/pageCollectRMI"); 
	            System.out.println(hello.test("隨便打印")) ; 
	        } catch (MalformedURLException e) 
	        { 
	            e.printStackTrace(); 
	        } catch (RemoteException e) 
	        { 
	            e.printStackTrace(); 
	        } catch (NotBoundException e) 
	        { 
	            e.printStackTrace(); 
	        } 
	    } 
}

 

你可能感兴趣的:(rmi)