您的位置:首页 > 其它

创建第一个RMI应用

2012-05-16 11:00 351 查看
学习使用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;
}
@Override
public String echo(String msg) {
System.out.println(name + "调用echo()方法");
return "echo:" + msg + "from" + name;
}

@Override
public 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 block
e.printStackTrace();
}
}

}


在例子中没有LocateRegistry.createRegistry(1099);这句代码,一开始运行的时候,总是报错,报服务器拒绝连接的错误,加上这句话就运行成功了,虽然rmi的默认端口是1099,但是需要告诉服务器一声,不然服务器不会得到通知,拒绝连接。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: