相父

匆匆,那些年,那些人,那些事,珍惜身边的她

RMI例子分以下几步:

1)定义一个接口.

package rmi;

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

/**
 * 定义一个远程接口,这个接口应该继承Remote
 * @author Dirac
 */
public interface PerfectTime extends Remote{
    /**
     * 因为这个方法是远程调用的,所以它应该抛出RemoteException
     * @param param
     * @return
     * @throws RemoteException
     */
    String getPerfectTime(String param) throws RemoteException;
}

 

2)接口的具体实现:

package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PerfectTimeImpl extends UnicastRemoteObject implements PerfectTime{
    private static final long serialVersionUID = 1793177637034935906L;
    String name = "";
    public PerfectTimeImpl(String name) throws RemoteException{
        this.name = name;
    }
    /**
     * 返回的字符串中包含了服务器端的时间
     */
    public String getPerfectTime(String param) throws RemoteException{
        return param + ": " + name + " is " + new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒").format (new Date(System.currentTimeMillis()));
    }
}

 

3)RMIC 命令生成stub

 

rmic rmi.PerfectTimeImpl

4)

服务器端绑定对象。

package rmi;

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

/**
 * @author Dirac
 */
public class PerfectTimeServer
{
    public static void main(String [] agrs) throws RemoteException
    {
        try
        {
            //服务器端注册远程对象
            PerfectTimeImpl p1 = new PerfectTimeImpl ("Black Stone");
            PerfectTimeImpl p2 = new PerfectTimeImpl ("Anders Heijlsberg");
            Naming.rebind ("p1", p1);
            Naming.rebind ("p2", p2);
            //获得注册的名字列表,并打印出来
            String[] list = Naming.list("");
            for(int j = 0; j < list.length; j++)
            {
                System.out.println("list[" + (j + 1) + "]=" + list[j]);
            }
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace ();
        }
    }
}

 

5)客户端调用代码:

package rmi;

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

/**
 * @author Dirac
 */
public class PerfectTimeClient
{
    public static void main(String [] args)
    {
        try
        {
            //客户端获取远程接口
            PerfectTime p1 = (PerfectTime)Naming.lookup ("rmi://localhost:1099/p1");
            PerfectTime p2 = (PerfectTime)Naming.lookup ("rmi://localhost:1099/p2");
            //调用远程接口的方法
            System.out.println (p1.getPerfectTime ("p1"));
            System.out.println (p2.getPerfectTime ("p2"));
        }
        catch(RemoteException re)
        {
            re.printStackTrace ();
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace ();
        }
        catch(NotBoundException nbe)
        {
            nbe.printStackTrace ();
        }
    }
}

 

6)设置classpath:

set CLASSPATH=D:/workspace/test/bin

7)启动RMI注册机:

start rmiregistry

 

8)启动服务器注册程序

start java rmi.PerfectTimeServer

9)启动客户端调用程序

java rmi.PerfectTimeClient

10)server.policy文件不可缺少,放在 class路径中:

grant{
    permission java.security.AllPermission;
}

posted on 2011-03-27 00:44  相父  阅读(140)  评论(0)    收藏  举报