首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

RMI学习札记-创建第一个RMI应用

2012-08-21 
RMI学习笔记-创建第一个RMI应用?学习使用RMI框架,抄写了一段例子代码:? ? ?代码如下:package com.you.rmi

RMI学习笔记-创建第一个RMI应用

?学习使用RMI框架,抄写了一段例子代码:

? ? ?代码如下:

package com.you.rmi;import java.rmi.Remote;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.server.UnicastRemoteObject;import java.util.Date;import javax.naming.Context;import javax.naming.InitialContext;public class SimpleServer  {public static void main(String[] args) {try {HelloService service1 = new HelloServiceImpl("service1");HelloService service2 = new HelloServiceImpl("service2");Context namingContext = new InitialContext();LocateRegistry.createRegistry(1099);namingContext.rebind("rmi://localhost:1099/HelloService1", service1);//rebind注册对象,把对象与一个名字        namingContext.rebind("rmi://localhost:1099/HelloService2", service2);//绑定,如果改名字已经被绑定,不过抛出异常System.out.println("服务器注册了两个HelloService对象");} catch (Exception e) {e.printStackTrace();}}}interface HelloService extends Remote{   public String echo(String msg) throws RemoteException;   public Date getTime() throws RemoteException;}class HelloServiceImpl extends UnicastRemoteObject implements HelloService{     /** *  */private static final long serialVersionUID = 1L;private String name;     public HelloServiceImpl(String name) throws RemoteException{     this.name = name;     }@Overridepublic String echo(String msg) {System.out.println(name + "调用echo()方法");return "echo:" + msg + "from" + name;}@Overridepublic Date getTime() {System.out.println(name + "调用getTime()方法");return new Date();}}
?package com.you.rmi;?
import java.rmi.RemoteException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NameClassPair;import javax.naming.NamingEnumeration;import javax.naming.NamingException;public class SimpleClient {public static void showRemoteObjects(Context namingContext) throws Exception {NamingEnumeration<NameClassPair> e = namingContext.list("rmi:");while(e.hasMore())System.out.println(e.next().getName());}public static void main(String[] args) {try {String url = "rmi://localhost/";Context namingContext = new InitialContext();//获得远程对象的存根对象HelloService service1 = (HelloService) namingContext.lookup(url + "HelloService1");HelloService service2 = (HelloService) namingContext.lookup(url + "HelloService2");//测试存根对象所属的类Class stubClass = service1.getClass();System.out.println("service1 是" + stubClass.getName() + "的实例");Class[] interfaces = stubClass.getInterfaces();for(int i = 0; i < interfaces.length; i++) {System.out.println("存根类实现了" + interfaces[i].getName());System.out.println(service1.echo("hello"));System.out.println(service1.getTime());System.out.println(service2.echo("hello"));System.out.println(service2.getTime());showRemoteObjects(namingContext);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

?java进阶网在例子中没有LocateRegistry.createRegistry(1099);这句代码,一开始运行的时候,总是报错,报服务器拒绝连接的错误,加上这句话就运行成功了,虽然rmi的默认端口是1099,但是需要告诉服务器一声,不然服务器不会得到通知,拒绝连接??。

?

热点排行