When should I implement java.io.Serializable in RMI?

I am just starting Java RMI and have some problems with when to use java.io.Serializable, so can anyone give me an example of RMI that java.io.Serializable should implement.

Thanks!!!


UPDATE: I made a simple example, however, I think that there are still problems, since the conclusion is incorrect. User interface

package server import java.rmi.Remote; import java.rmi.RemoteException; import 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; } 

Character execution

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

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

Server

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

Client

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

The result obtained:

  D: \ java -Djava.security.policy = d: \ Client \ policy \ client.policy client.Client
 Person: myName age: 0 address: server.Address@1d6776d
 Person: myNewName age: 0 address: server.Address@10a2d64

The address is not printed correctly PS: As seen from the import of the Customer class

 import server.PersonInterface; import server.Address; 

I had a copy of PersonInterface.class and Address.class on the client side to compile Client.


Final: So stupid !!! Add the following code to Address.java
 public String toString(){ return addre1+ " " + addre2; } 

OK, the problems are resolved! :)

+4
source share
1 answer
 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; } } 

you will need a Serializable interface to report that your class can be sent over the network

server code

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

Client code

 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 } } 
+2
source

Source: https://habr.com/ru/post/1303034/


All Articles