RMI 远程调用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值