逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
RMIServer目录的文件:Product.java, ProductImpl.java, ProductServer.java, download/Product.class

// Product.java

import java.rmi.Remote;

import java.rmi.RemoteException;


public interface Product extends Remote {

    String getDescription() throws RemoteException;

}


// ProductImpl.java

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;


public class ProductImpl extends UnicastRemoteObject implements Product {

    private String name;


    public ProductImpl(String name) throws RemoteException {

        this.name = name;

    }


    public String getDescription() {

        return "I am a " + name + ". Please buy me!";

    }

}


 

// ProductServer.java

import java.rmi.RemoteException;


import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;



public class ProductServer {

    public static void main(String[] args) throws RemoteException, NamingException {

        System.out.println("Initialize product implementations...");

        Product p1 = new ProductImpl("Toaester");

        Product p2 = new ProductImpl("Microwave");

        

        System.out.println("Registrying product implementations...");

        Context namingContext = new InitialContext();

        namingContext.bind("rmi:toaster", p1);

        namingContext.bind("rmi:microwave", p2);

        

        System.out.println("Waiting for clients connecting...");

    }

}


// ShowBindings.java, 测试显示ProductServer在RMI注册表中注册好的对象

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NameClassPair;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;



public class ShowBindings {

    public static void main(String[] args) throws NamingException {

        Context namingContext = new InitialContext();

        NamingEnumeration<NameClassPair> e = namingContext.list("rmi:");

        while (e.hasMore()) {

            System.out.println(e.next().getName());

        }

    }

}



_______________________________________________________________________________________

RMIClient目录的文件:Product.java, ProductClient.java, client.policy, download/Product.class

// Product.java与服务器端的Product.java一样。


// ProductClient.java

import java.rmi.RMISecurityManager;


import javax.naming.Context;

import javax.naming.InitialContext;


public class ProductClient {

    public static void main(String[] args) {

        System.setProperty("java.security.policy", "client.policy");

        System.setSecurityManager(new RMISecurityManager());


        try {

            String url = "rmi://localhost/";

            Context namingContext = new InitialContext();

            Product p1 = (Product) namingContext.lookup(url + "toaster");

            Product p2 = (Product) namingContext.lookup(url + "microwave");


            System.out.println(p1.getDescription());

            System.out.println(p2.getDescription());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}


// client.policy

grant {

permission java.net.SocketPermission

"*:1024-65535", "connect";

};


_______________________________________________________________________________________

运行步骤:

1. 启动RMI注册表

进入RMIServer目录,使用命令:rmiregistry &

(退出rmiregistry命令:killall rmiregistry,重新注册对象的时候要重启RMI注册表)

2.1 启动服务器:

进入RMIServer目录,使用命令:java ProductServer &

(退出服务器程序,ctr + c, 但还要使用再使用一次killall java, 否则再次启动服务器会报异常)

[2.2] 测试注册好的对象:

进入RMIServer目录,使用命令:java ShowBindings

3. 启动客户端:

java ProductClient


观察终端输出的结果。

 

 

 

 

posted on 2009-07-03 18:19 逛奔的蜗牛 阅读(383) 评论(0)  编辑 收藏 引用 所属分类: Java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理