RMI 实例
程序员文章站
2022-06-13 08:10:45
...
RMI 实例
package rmi.train; import java.io.Serializable; public class Animal implements Serializable, Cloneable { private static final long serialVersionUID = -1858518369668584532L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package rmi.train; import java.io.Serializable; import java.rmi.Remote; import java.rmi.RemoteException; public interface IAnimalService extends Remote, Serializable { String getMonkeyName() throws RemoteException; Animal getAnimal() throws RemoteException; }
package rmi.train; import java.rmi.RemoteException; public class AnimalServiceImp implements IAnimalService { /** * */ private static final long serialVersionUID = -4070602245046749074L; public AnimalServiceImp() { } @Override public String getMonkeyName() throws RemoteException { return "I'm Jacky"; } public Animal getAnimal() throws RemoteException { Animal animal = new Animal(); animal.setUsername("admin"); animal.setPassword("admin"); return animal; } }
package rmi.train; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class AnimalServer { final static int port = 1099; //绑定的端口号 final static String serviceName = "animalService"; //服务名称 /** * @param args */ public static void main(String[] args) { try { IAnimalService obj = new AnimalServiceImp(); /** * 1 未测成功 */ IAnimalService stub = (IAnimalService) UnicastRemoteObject.exportObject(obj, port); //端口绑定远程对象 Registry registry = LocateRegistry.getRegistry(port); registry.bind(serviceName, stub); //注册服务地址 System.out.println("Server Start..."); /** * 2 测试成功 */ // LocateRegistry.createRegistry(port); // Naming.rebind(serviceName, obj); System.out.println("Hello Server is ready."); Thread.sleep(1000 * 60 *2); System.out.println("Hello Server is stop."); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
package rmi.train; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class AnimalClient { /** * @param args */ public static void main(String[] args) { Registry registry = null; final String host = "127.0.0.1"; final String serviceName = "animalService"; //服务名称 try { /** * 1 */ System.out.println("registry"); registry = LocateRegistry.getRegistry(host, AnimalServer.port); //获取远程对象联机注册 //获取动态代理类 IAnimalService stub = (IAnimalService) registry.lookup(serviceName); //远程调用 System.out.println("monkey name: " + stub.getMonkeyName()); stub = null; /** * 2 */ System.out.println("Naming"); String url="rmi://127.0.0.1/" + AnimalServer.serviceName; stub = (IAnimalService)Naming.lookup(url); System.out.println("Animal: " + stub.getAnimal()); } catch (Exception e) { e.printStackTrace(); } } }
上一篇: DB2中like使用 locate
下一篇: 大胆小鬼