rmi例子:
远程对象接口:
package com.cicro.iexchange.common;
import java.rmi.*;
/**
* 远程接口必须扩展接口java.rmi.Remote
*/
public interface DBInfoInterface extends Remote
{
/**
* 远程接口方法必须抛出 java.rmi.RemoteException
*/
public String say() throws RemoteException;
public void setDbInfos(String dbInfos) throws RemoteException;
}
远程接口实现类:
package com.cicro.iexchange.common;
import java.rmi.*;
import java.rmi.server.*;
/**
* 扩展了UnicastRemoteObject类,并实现远程接口 HelloInterface
*/
public class DBInfo extends UnicastRemoteObject implements DBInfoInterface
{
private String message;
/**
* 必须定义构造方法,即使是默认构造方法,也必须把它明确地写出来,因为它必须抛出出RemoteException异常
*/
public DBInfo() throws RemoteException {
super();
}
/**
* 远程接口方法的实现
*/
public String say() throws RemoteException
{
System.out.println("Called by HelloClient");
return message;
}
@Override
public void setDbInfos(String dbInfos) throws RemoteException{
TestDbInfoRmi test=new TestDbInfoRmi();
System.out.println("正在执行服务器端方法:;;;;;;;;;;;;;;;");
test.testRmi(dbInfos);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
测试接受的client信息的类:
package com.cicro.iexchange.common;
public class TestDbInfoRmi {
public void testRmi(String dbinfo){
System.out.println("测试rmi,client信息:"+dbinfo);
}
}
rmi服务类:
package com.cicro.iexchange.common;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class DBInfoServer implements Runnable {
/**
* 启动 RMI 注册服务并进行对象注册
*/
private void rmiStart(){
try
{
//启动RMI注册服务,指定端口为1099 (1099为默认端口)
//也可以通过命令 $java_home/bin/rmiregistry 1099启动
//这里用这种方式避免了再打开一个DOS窗口
//而且用命令rmiregistry启动注册服务还必须事先用RMIC生成一个stub类为它所用
LocateRegistry.createRegistry(1099);
//创建远程对象的一个或多个实例,下面是hello对象
//可以用不同名字注册不同的实例
DBInfoInterface dbinfo = new DBInfo();
//把hello注册到RMI注册服务器上,命名为Hello
Naming.rebind("dbinfo", dbinfo);
//如果要把hello实例注册到另一台启动了RMI注册服务的机器上
//Naming.rebind("//192.168.1.105:1099/Hello",hello);
System.out.println("Hello Server is ready.");
}
catch (Exception e)
{
System.out.println("Hello Server failed: " + e);
}
}
@Override
public void run() {
rmiStart();
}
}
启动rmi服务类的serverlet:
package com.cicro.iexchange.common;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StartRmi extends HttpServlet {
/**
* Constructor of the object.
*/
public StartRmi() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* 启动rmi服务
*/
public void init() throws ServletException {
DBInfoServer dbinfo=new DBInfoServer();
Thread t = new Thread(dbinfo);
t.start();
}
}
serverlet配置:
<servlet> <servlet-name> initRmi </servlet-name> <servlet-class> com.cicro.iexchange.common.StartRmi </servlet-class> <load-on-startup> 3 </load-on-startup> </servlet>
rmi客户端:
package com.cicro.iexchange.common;
import java.rmi.Naming;
public class DBInfoClient
{
/**
* 查找远程对象并调用远程方法
*/
public static void main(String[] argv)
{
try
{
//DBInfoInterface dbinfo = (DBInfoInterface) Naming.lookup("dbinfo");
//如果要从另一台启动了RMI注册服务的机器上查找hello实例
DBInfoInterface dbinfo = (DBInfoInterface)Naming.lookup("//192.168.1.50:1099/dbinfo");
//调用远程方法
dbinfo.setDbInfos("表空间已满!请停止服务端相关操作!");
}
catch (Exception e)
{
System.out.println("HelloClient exception: " + e);
}
}
}