我什么时候应该在 RMI 中实现 java.io.Serializable?

use*_*340 6 java rmi

我刚刚开始使用 Java RMI,并且在何时使用 java.io.Serializable 时遇到了一些问题,所以任何人都可以给我一个必须实现 java.io.Serializable 的 RMI 示例。

谢谢!!!


更新:我做了一个简单的例子,但是,我认为仍然存在问题,因为输出不正确。人机界面

包服务器;导入 java.rmi.Remote; 导入 java.rmi.RemoteException; 导入 java.rmi.server.UnicastRemoteObject;

public interface PersonInterface extends Remote  
{
    public void setName(String name) throws RemoteException;
    public String getPerson() throws RemoteException;
    public void setAddress(Address address) throws RemoteException;
}
Run Code Online (Sandbox Code Playgroud)

个人实施

package server;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.Remote;

class Person extends UnicastRemoteObject implements PersonInterface
{
    private String name;
    private int age;
    private Address address;


    Person() throws RemoteException {super();}
    Person(String name,int age, Address address) throws RemoteException {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public void setName(String name) throws RemoteException {
        this.name = name;
    }
    public void setAddress(Address address) throws RemoteException{
        this.address = address;
    }

    public String getPerson() throws RemoteException {
        return "Person : " + name + " age : " + age + " address : " + address;
    }
}
Run Code Online (Sandbox Code Playgroud)

地址类

package server;
import java.io.Serializable;

public class Address implements Serializable
{
    private static final long serialVersionUID = 227L;
    private String addre1;
    private String addre2;

    public Address() {}
    public Address(String addre1,String addre2){
        this.addre1 = addre1;
        this.addre2 = addre2;   
    }
}
Run Code Online (Sandbox Code Playgroud)

服务器

package server;
import java.rmi.Naming;

class Server 
{
    public static void main(String[] args) 
    {
        try{
        //create an instance of the RemoteDatabaseServer
            Person person = new Person();
            //rmi://[host][:port]/object
            String namePerson = "rmi://localhost:9999/person";

            //bind this instance to localhost port999 with name database
            Naming.bind(namePerson, person);
            System.out.println("Server is running...");
        }catch(Exception ex){
            System.out.println("Server Exception...");
            ex.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

客户

package client;
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import server.PersonInterface;
import server.Address;


class Client
{
    public static void main(String[] args) 
    {
        try{
            System.setSecurityManager(new RMISecurityManager());
            String namePerson = "rmi://localhost:9999/person";
            PersonInterface person = 
                (PersonInterface)Naming.lookup(namePerson);

            person.setName("myName");
            System.out.println(person.getPerson());
            person.setName("myNewName");
            Address address = new Address("123","123");
            person.setAddress(address);
            System.out.println(person.getPerson());
        }catch(Exception ex){
            System.out.println("Client failure...");
            ex.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client
人:myName 年龄:0 地址:server.Address@1d6776d
人员:myNewName 年龄:0 地址:server.Address@10a2d64

地址打印不正确 PS:从Client类导入可以看到

import server.PersonInterface;
import server.Address;
Run Code Online (Sandbox Code Playgroud)

我已将 PersonInterface.class 和 Address.class 复制到客户端以进行客户端编译。


最后:太蠢了!!!将以下代码添加到 Address.java

public String toString(){
    return addre1+ " " + addre2;
}
Run Code Online (Sandbox Code Playgroud)

OK,问题解决了!!:)

ant*_*ony 1

interface MyInterface extends Remote {
  MyClass f(MyClass x) throws RemoteException;
}

class MyClass implements Serializable {
   private int value;
   public MyClass(int value) {
       this.value = value;
   }

   public int getValue() {
       return value;
   }
}
Run Code Online (Sandbox Code Playgroud)

你需要 Serialized 接口来告诉你的类可以通过网络发送

服务器代码

class Service extends UnicastRemoteObject  implements MyInterface {
   public Service() {
   }
   public MyClass f(MyClass v) throws RemoteException {
       return new MyClass(v.getValue() + 1)
   }

   public static void main(Strint arg[]) {
      Registry r = LocateRegistry.createRegistry(1099);
      r.rebind("service", new Service());
   }
}
Run Code Online (Sandbox Code Playgroud)

客户代码

class Client {
       public static void main(Strint arg[]) {
          Registry r = LocateRegistry.getRegistry("localhost", 1099);
          MyInterface service = (MyInterface)r.lookup("service");

          MyClass result = service.f(new MyClass(123));
          System.out.println(result.getValue());  //print 124 here
       }
  }
Run Code Online (Sandbox Code Playgroud)

  • @antony:“你需要可序列化的接口来告诉你的类可以通过网络发送”——这是模糊的并且掩盖了一些重要的细节。你能详细说明一下吗? (2认同)