package demo.rmi;
import java.rmi.*;
public interface Echo extends Remote {
public String echo(String msg) throws RemoteException;
}
package demo.rmi;
import java.rmi.*;
import java.rmi.registry.*;
public class EchoClient {
public static void main(String [] args) {
// System.setSecurityManager(new RMISecurityManager());
try {
try{
System.out.println("开始建立连接。。。。。。");
Echo hi=(Echo)Naming.lookup("rmi://127.0.0.1:1099/EchoServer");
System.out.println("连接成功,开始使用服务。。。。。。");
for(int i=0;i<10;i++){
System.out.println(hi.echo(i+"s"));
}
}
catch(Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package demo.rmi;
import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class EchoServer extends UnicastRemoteObject implements Echo {
//默认构件器,也要“掷”出RemoteException违例
/**
*
*/
private static final long serialVersionUID = 1L;
public EchoServer() throws RemoteException {
super();
}
public String echo(String msg) throws RemoteException {
return "Echo: 恭喜你成功了" + msg;
}
public static void main(String [] args) {
/*创建和安装一个安全管理器,令其支持RMI。作为Java开发包的一部分,适用于RMI唯一一个是RMISecurityManager.*/
// System.setSecurityManager(new RMISecurityManager());
try {
/*创建远程对象的一个或多个实例,下面是EchoServer对象*/
Registry r = LocateRegistry.getRegistry(1099);
if ( r != null) r = LocateRegistry.createRegistry(1099);
EchoServer es = new EchoServer();
java.rmi.Naming.rebind("EchoServer", es);
System.out.println("Ready to provide echo service...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
还要生成的根 bat
set path=C:/Program Files/Java/jdk1.5.0_09/bin
rmic -d E:/eclipse3.0/workspace/javalearn/src demo.rmi.EchoServer
rmiregistry.bat
echo "这个是启动rmiregistry服务的命令"
set path=C:/Program Files/Java/jdk1.5.0_09/bin
cd e:
cd E:/eclipse3.0/workspace/javalearn/classes
rmiregistry
pause
首先,你需要有一个 RMIRunner
package com.your.rmi;
import java.rmi.*;
import java.rmi.registry.*;
import org.apache.log4j.Logger;
public class RMIRunner implements Runnable {
private static Logger log = Logger.getLogger( RMIRunner.class );
// ***********************
private void rmiStart() {
try {
Registry r = LocateRegistry.getRegistry(RMIExecutor.ServerPort);
if ( r != null) r = LocateRegistry.createRegistry(RMIExecutor.ServerPort);
RMIServer s = new RMIServerImpl();
Naming.rebind(RMIServer.ServerName, s);
log.info("rmiStart successful.");
} catch (Exception e) {
log.error("During rmiStart", e);
}
}
public void run() {
rmiStart();
}
}
然后,要写一支 servlet 来调用它
package com.your.web.RMIInit;
import javax.servlet.*;
import javax.servlet.http.*;
import com.your.rmi.RMIRunner;
public class RMIInit extends GenericServlet {
public void init( ServletConfig config ) throws ServletException {
RMIRunner rmiRunner = new RMIRunner();
Thread t = new Thread(rmiRunner);
t.start();
}
public void service( ServletRequest req, ServletResponse res ) throws ServletException, IOException { }
public void destroy() { }
}
最后,在 web.xml 中配置该 servlet 为 load-on-startup 的
....
<servlet>
<servlet-name>rmiInit</servlet-name>
<display-name>RMI Server Init</display-name>
<servlet-class>com.your.web.RMIInit</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
....
搞定
Top
4 楼jackyz(J.)回复于 2003-08-01 10:47:20 得分 0 上诉代码有误,应为:
private void rmiStart() {
try {
Registry r = LocateRegistry.getRegistry(RMIServer.Port);
if ( r != null) r = LocateRegistry.createRegistry(RMIServer.Port);
RMIServer s = new RMIServerImpl();
Naming.rebind(RMIServer.Name, s);
log.info("rmiStart successful.");
} catch (Exception e) {
log.error("During rmiStart", e);
}
}