zoukankan      html  css  js  c++  java
  • java rmi的一个简单实例

    参考网站  http://www.cnblogs.com/leslies2/archive/2011/05/20/2051844.html

    实体类PersonEntity

    package net.cs30.rmi;
    
    import java.io.Serializable;
    
    /**
     * Created by caochenghua on 2017/4/4.
     */
    public class PersonEntity implements Serializable {
        private int id;
        private String name;
        private int age;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

      服务接口PersonService

    package net.cs30.rmi;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.util.List;
    
    /**
     * Created by caochenghua on 2017/4/4.
     */
    public interface PersonService extends Remote {
        public List<PersonEntity> GetList() throws RemoteException;
    }
    

      服务接口实现PersonServiceImpl

    package net.cs30.rmi;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * Created by caochenghua on 2017/4/4.
     */
    public class PersonServiceImpl extends UnicastRemoteObject implements PersonService {
        protected PersonServiceImpl() throws RemoteException {
            super();
        }
    
        @Override
        public List<PersonEntity> GetList() throws RemoteException {
            System.out.println("Get Person Start!");
            List<PersonEntity> personEntityList=new LinkedList<>();
            PersonEntity personEntity=new PersonEntity();
            personEntity.setAge(10);
            personEntity.setId(0);
            personEntity.setName("cao");
            personEntityList.add(personEntity);
    
            PersonEntity personEntity2=new PersonEntity();
            personEntity2.setAge(10);
            personEntity2.setId(123);
            personEntity2.setName("horizon");
            personEntityList.add(personEntity2);
    
            return personEntityList;
        }
    }
    

      服务器端测试代码Program

    package net.cs30.rmi;
    
    import java.rmi.Naming;
    import java.rmi.registry.LocateRegistry;
    
    /**
     * Created by caochenghua on 2017/4/4.
     */
    public class Program {
        public static void main(String[] args) {
            try {
                PersonService personService=new PersonServiceImpl();
                //注册通讯端口
                LocateRegistry.createRegistry(6600);
                //注册通讯路径
                Naming.rebind("rmi://127.0.0.1:6600/PersonService",personService);
                System.out.println("Service Start!");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

      客户端测试代码ProgramClient

    package net.cs30.rmi;
    
    import java.rmi.Naming;
    import java.util.List;
    
    /**
     * Created by caochenghua on 2017/4/4.
     */
    public class ProgramClient {
        public static void main(String[] args) {
            try {
                PersonService personService=(PersonService) Naming.lookup("rmi://127.0.0.1:6600/PersonService");
                List<PersonEntity> personEntityList=personService.GetList();
                for (PersonEntity personEntity:personEntityList){
                    System.out.println("ID:"+personEntity.getId()+" Age:"+personEntity.getAge()+" Name:"+personEntity.getName());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    运行结果

    服务器端输出

    客户端输出

  • 相关阅读:
    长进程后用电脑发送提示消息到手机微信
    python-openCV 绘制图形
    python调用C++实例:用C++对numpy执行BFS(广度优先搜索)
    numpy中多维数组的绝对索引
    python调用C++ DLL 传参技巧
    React 中常见的动画实现方式
    H5和android原生APP之间的区别
    【转发】三层架构、MVC以及与SSM架构的对应关系(通俗易懂)
    VS code 设置中文后没有反应仍然是英文
    从数据库中获取信息显示在select下拉框中并实现联动
  • 原文地址:https://www.cnblogs.com/caochenghua/p/6666795.html
Copyright © 2011-2022 走看看