package com.myibs.test.service.RMI;
import lombok.Data;
import java.io.Serializable;
@Data
public class PersonSerializable implements Serializable {
private String name;
private int age;
private int id;
private String sex;
public PersonSerializable() {
}
public PersonSerializable(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.myibs.test.service.RMI;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface PersonService extends Remote {
public PersonSerializable getPerson() throws RemoteException;
public String wrtPerson(ArrayList<PersonSerializable> personList) throws RemoteException;
}
package com.myibs.test.service.RMI;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class PersonServiceImp extends UnicastRemoteObject implements PersonService {
private static final long serialVersionUID = 1L;
protected PersonServiceImp() throws RemoteException {
super();
}
@Override
public PersonSerializable getPerson() throws RemoteException {
PersonSerializable person = new PersonSerializable();
person.setId(1);
person.setName("ds");
person.setSex("M");
return person;
}
@Override
public String wrtPerson(ArrayList<PersonSerializable> personList) throws RemoteException {
Integer iLoop=0;
for (PersonSerializable personSerializable : personList) {
iLoop++;
System.out.println(
personSerializable.getAge() + " " +
personSerializable.getName()
);
}
try (ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream("D:\\tmp\\RMIPersonLst.txt"))) {
oos.writeObject(personList);
return "000000:" + iLoop.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "999999:" + iLoop;
}
}
package com.myibs.test.service.RMI;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.Random;
public class RMIClientTest {
public static void getPerson() {
try {
PersonService personService = (PersonService) Naming.lookup("rmi://127.0.0.1:6600/PersonService");
PersonSerializable person = personService.getPerson();
System.out.println("ID:"+person.getId());
System.out.println("Name:"+person.getName());
System.out.println("Sex:"+person.getSex());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void wrtPersonList(Integer number) {
String sRtn;
try {
PersonService personService = (PersonService) Naming.lookup("rmi://127.0.0.1:6600/PersonService");
ArrayList<PersonSerializable> personList = new ArrayList<>();
for (Integer i = 0; i <number; i++) {
Random iRandom=new Random();
PersonSerializable person = new PersonSerializable(
i.toString(), iRandom.nextInt(100));
personList.add(person);
}
sRtn=personService.wrtPerson(personList);
System.out.println("Rtn: " + sRtn);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] arg){
RMIClientTest.getPerson();
RMIClientTest.wrtPersonList(new Random().nextInt(1000));
}
}
package com.myibs.test.service.RMI;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class RMIServerTest {
public static void main(String[] arg){
try {
PersonService personService = new PersonServiceImp();
System.out.println("RMI starting...");
LocateRegistry.createRegistry(6600);
Naming.bind("rmi://127.0.0.1:6600/PersonService", personService);
System.out.println("RMI started...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
RMI 远程调用
最新推荐文章于 2024-03-10 18:00:00 发布