2010-03-03 3 views
4

Java RMI를 시작하고 java.io.Serializable을 사용할 때 문제가 발생하므로 java.io.Serializable을 구현해야하는 RMI 예제를 누군가에게 제공 할 수 있습니다.언제 RMI에서 java.io.Serializable을 구현해야합니까?

감사합니다 !!!


업데이트 : 간단한 예제를 만들었지 만 출력이 올바르지 않아서 문제가 여전히 있다고 생각합니다. 개인 인터페이스

패키지 서버; 가져 오기 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; 
} 

사람 구현

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; 
    } 
} 

주소 클래스

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; 
    } 
} 

서버

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(); 
     } 
    } 
} 

클라이언트

당신은 내가 복사 PersonInterface.class 및 주소를했다

import server.PersonInterface; 
import server.Address; 

클라이언트 클래스 가져 오기에서 볼 수 으로 :

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(); 
     } 
    } 
} 

내가 가진 출력은

 
D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client 
Person : myName age : 0 address : [email protected] 
Person : myNewName age : 0 address : [email protected] 

이 주소가 제대로 PS를 인쇄되지 않습니다이다. 클래스를 클라이언트 측에 연결하여 클라이언트를 컴파일합니다.


결승 : 어리석은 !!!

Address.java

public String toString(){ 
    return addre1+ " " + addre2; 
} 
에 코드 OK 다음 추가는 문제가 해결된다! :)

답변

2
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; 
    } 
} 

당신은 당신의 클래스가 네트워크를 통해 전송 될 수 있음을 알려 Serializable 인터페이스를 필요로

서버 코드

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()); 
    } 
} 

클라이언트 코드

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 
     } 
    } 
+0

감사합니다, 그래서 당신을 MyClass Serializable을 만들어 클라이언트 측에서 MyClass 객체를 생성 할 수 있다는 의미입니까? 따라서 MyInterface를 구현하는 원격 객체 (예 : myRemoteObject)는 메소드 f를 호출하고 MyClass 객체를 매개 변수로 취할 수 있습니다. MyInterface myRemoteObject = (MyInterface) Naming.lookup (name); .... myRemoteObject.f (new MyClass()); 이게 맞습니까? – user200340

+1

위대한 !! 한가지 더 궁금한 점이 있습니다. MyInterface.class 또는 MyInterface와 마찬가지로 MyClass.class를 클라이언트 패키지에 복사해야합니까?클래스는 Remote를 확장하면서 MyClass를 어떤 형식으로 캡슐화합니다. – user200340

+1

그것 없이는 컴파일되지 않기 때문에 클라이언트 측에서 MyClass가 필요합니다. 일반적으로 MyClass는 인터페이스가 될 수 있으며 클라이언트 측은 구현 (서버에서만 정의 됨)이 아닌이 인터페이스 만 필요로합니다. – antony