标签:style blog color java os io for 2014
package com.gs.practice.rmi.test; import java.rmi.Naming; import java.util.List; /** * Copyright (C),HTF<br> * 服务customer * * @author muzhongjiang * @date 2014年8月5日 */ public class ClientApp { public static void main(String[] args) { try { // 调用远程对象,注意RMI路径与接口必须与服务器配置一致 PersonService personService = (PersonService) Naming.lookup("rmi://127.0.0.1:1111/PersonService"); List<Person> personList = personService.GetList(); for (Person person : personList) { System.err.println("ID:" + person.getId() + " Age:" + person.getAge() + " Name:" + person.getName()); } } catch (Exception ex) { ex.printStackTrace(); } } }
package com.gs.practice.rmi.test; import java.io.Serializable; /** * Copyright (C),HTF<br> * 实体类 * * @author muzhongjiang * @date 2014年8月5日 */ public class Person implements Serializable { //注意对象必须继承Serializable private static final long serialVersionUID = 6175929251630575130L; private int id; private String name; private int age; public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package com.gs.practice.rmi.test; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; /** * Copyright (C),HTF<br> * 此为远程对象调用的接口(必须继承Remote类) * * @author muzhongjiang * @date 2014年8月5日 */ public interface PersonService extends Remote { List<Person> GetList() throws RemoteException; }
package com.gs.practice.rmi.test; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.LinkedList; import java.util.List; /** * Copyright (C),HTF<br> * 此为远程对象的实现类,须继承UnicastRemoteObject * * @author muzhongjiang * @date 2014年8月5日 */ public class PersonServiceImpl extends UnicastRemoteObject implements PersonService { private static final long serialVersionUID = -8722028618928237236L; public PersonServiceImpl() throws RemoteException { super(); } @Override public List<Person> GetList() throws RemoteException { System.out.println("Get Person Start!"); List<Person> personList=new LinkedList<Person>(); Person person1=new Person(); person1.setAge(25); person1.setId(0); person1.setName("Leslie"); personList.add(person1); Person person2=new Person(); person2.setAge(25); person2.setId(1); person2.setName("Rose"); personList.add(person2); return personList; } }
package com.gs.practice.rmi.test; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; /** * Copyright (C),HTF<br> * 服务端provider * * @author muzhongjiang * @date 2014年8月5日 */ public class ServerApp { public static void main(String[] args) { try { PersonService personService = new PersonServiceImpl(); LocateRegistry.createRegistry(1111);// 注册通讯端口 Naming.rebind("rmi://localhost:1111/PersonService", personService);// 注册通讯路径 System.out.println("Service Start!"); } catch (Exception e) { e.printStackTrace(); } } }
标签:style blog color java os io for 2014
原文地址:http://www.cnblogs.com/muzhongjiang/p/3892474.html