本例子包含了共5个文件,分别是helloclient,hello,helloserver,configur,config.properties
——————————————————————————–
/*hello.java*/
/*
* created on 2004-11-10
*
*/
package test.rmi;
import java.rmi.remote;
import java.rmi.remoteexception;
/**
* @author dogcome
*
* <p>远程方法调用接口,定义远程调用方法</p>
*/
public interface hello extends remote {
string sayhello() throws remoteexception;
}
——————————————————————————–
/*helloserver*/
/*
* created on 2004-11-10
*
*/
package test.rmi;
import java.rmi.remoteexception;
import java.rmi.registry.locateregistry;
import java.rmi.registry.registry;
import java.rmi.server.unicastremoteobject;
/**
* @author dogcome
*
*<p>服务器端,实现了hello接口中的方法,用于实现远程调用方法的具体业务逻辑</p>
*/
public class helloserver extends unicastremoteobject implements hello {
string name;
public helloserver(string s) throws remoteexception {
super();
name=s;
}
public string sayhello() throws remoteexception {
// todo auto-generated method stub
return "hello world!";
}
public static void main(string[] args) {
/**下面这句话若要加上,则需要进行权限的认证,即增加.policy文件
* 并且在命令行中使用如下格式
* java -djava.security.policy=java.policy test.rmi.helloserver
*/
/*system.setsecuritymanager(new rmisecuritymanager());*/
registry registry = null;
try {
/**启动注册服务器,使用了这个语句就不再需要在命令行环境中
*启动registry服务了
*/
registry = locateregistry.getregistry();
/* 若没有获得连接,则此句会抛出异常,后面在捕获后进行相关处理 */
registry.list();
system.out.println("register the exist server!"); //$non-nls-1$
} catch (remoteexception re) {
try {
int port = integer.parseint(configur
.getstring("helloserver.registryserverport")); //$non-nls-1$
registry = locateregistry.createregistry(port);
system.out.println("create registry server!"); //$non-nls-1$
} catch (exception e) {
e.printstacktrace();
}
}
try {
helloserver helloserver = new helloserver("hello");
registry
.rebind(
configur.getstring("helloserver.helloservername"), helloserver); //$non-nls-1$
system.out.println("helloserver server start!"); //$non-nls-1$
} catch (exception e) {
e.printstacktrace();
}
}
}
——————————————————————————–
/*helloclient*/
/*
* created on 2004-11-10
*
*/
package test.rmi;
import java.rmi.*;
import java.rmi.registry.locateregistry;
import java.rmi.registry.registry;
/**
* @author dogcome
*
* <p>客户端,需要注册服务器并使用jndi</p>
*/
public class helloclient {
public static void main(string[] args) {
/**下面这句话若要加上,则需要进行权限的认证,即增加.policy文件
* 并且在命令行中使用如下格式
* java -djava.security.policy=java.policy test.rmi.helloserver
*/
/*system.setsecuritymanager(new rmisecuritymanager());*/
try {
/*注册服务器*/
string hostname=configur.getstring("helloserver.registryservername");
int port=integer.parseint(configur.getstring("helloserver.registryserverport"));
registry registry=locateregistry.getregistry(hostname,port);
hello hello=(hello)registry.lookup(configur.getstring("helloserver.helloservername"));
string message=hello.sayhello();
system.out.println(message);
}catch(exception e) {
e.printstacktrace();
}
}
}
——————————————————————————–
/*
* created on 2004-11-12
*
*/
package test.rmi;
import java.util.missingresourceexception;
import java.util.resourcebundle;
/**
* @author dogcome
*
* <p>获取系统运行所需要的配置信息,对应的配置文件名称为config.properties</p>
*/
public class configur {
private static final string bundle_name = "test.rmi.config";
private static final resourcebundle resource_bundle = resourcebundle
.getbundle(bundle_name);
private configur() {
}
/**
* <p>通过key名称获得配置文件的相关信息</p>
* @param key key名称
* @return string 配置文件信息
*/
public static string getstring(string key) {
try {
return resource_bundle.getstring(key);
} catch (missingresourceexception e) {
return ! + key + !;
}
}
}
——————————————————————————–
#config.properties
helloserver.registryserverport=1111
helloserver.helloservername=helloserver
helloserver.registryservername=127.0.0.1
——————————————————————————–
首先使用rmic test.rmi.helloserver命令生成需要的stub及skel两个类文件
然后运行helloserver服务器 java test.rmi.helloserver
最后运行helloclient客户端 java test.rmi.helloclient
屏幕输出helloworld,这样,一个最简单的rmi远程调用成功了
