`
csbison
  • 浏览: 154898 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

复习一下原始的rmi

阅读更多

复习一下原始的rmi:

 

1:文件Service.java ServiceImpl.java ServiceServer.java ServiceClient.java  client.policy

 

结构

 src

    net.loocky.rmi/ *.java

    client.policy

 

文件如下:

Service.java是个interface

package net.loocky.rmi;

 

import java.rmi.Remote;

import java.rmi.RemoteException;

 

public interface Service extends Remote {

 

 public String getName() throws RemoteException;

 

 public void setName(String name) throws RemoteException;

 

}

 

 

 

 

ServiceImpl.java Service的实现

package net.loocky.rmi;

 

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject; 

 

public class ServiceImpl extends UnicastRemoteObject implements Service {

 private static final long serialVersionUID = 1L; 

 

 private String name; 

 

 public ServiceImpl(String name) throws RemoteException {

  this.name = name;

 } 

 

 public String getName() throws RemoteException {

  return name;

 } 

 

 public void setName(String name) throws RemoteException {

  this.name = name; 

 

 } 

 

}

 

 

 

 

ServiceServer

package net.loocky.rmi; 

 

import java.rmi.Naming; 

 

public class ServiceServer { 

 

 /**

  * @param args

  */

 public static void main(String[] args) {

  try { 

 

   ServiceImpl s1 = new ServiceImpl("my name is john");

   ServiceImpl s2 = new ServiceImpl("my name is loocky");

   Naming.rebind("john", s1);

   Naming.rebind("loocky", s2);

   System.out.println("waiting for clients...");

  } catch (Exception e) {

   e.printStackTrace();

  } 

 

 } 

 

}

 

 

 

 

ServiceClient

package net.loocky.rmi; 

 

import java.rmi.Naming;

import java.rmi.RMISecurityManager; 

 

public class ServiceClient { 

 

 /**

  * @param args

  */

 public static void main(String[] args) {

  System.setSecurityManager(new RMISecurityManager());

  String url = "rmi://127.0.0.1:1099/";

  try {

   Service s1 = (Service) Naming.lookup(url + "john");

   Service s2 = (Service) Naming.lookup(url + "loocky");

   System.out.println(s1.getName());

   s2.setName("my name is not loocky");

   System.out.println(s2.getName()); 

 

  } catch (Exception e) {

   e.printStackTrace();

  }

 } 

 

}

 

 

 

 

client.policy

grant

{

  permission java.security.AllPermission;

// permission java.net.SocketPermission "127.0.0.1:1099","connect";

// permission java.net.SocketPermission "127.0.0.1:80","connect";

};

 

 

 

 

 

有了这些文件下面要做的事情是

 

1:进入bin/  rmic net.loocky.rmi.ServiceImpl 生成STUB

2dos下面 rmiregistry

3java net.loocky.rmi.ServiceServer

4: java -Djava.security.policy=client.policy net.loocky.rmi.ServiceClient

5:查看控制台的结果吧!

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics