1. 实现远程接口,生成远程对象,存根(Stub)和框架(Skeleton)
实现远程接口,远程接口告诉JVM:实现了该接口的对象可以远程调用及有哪些方法可以调用。
本例子中定义了sayHello()。由于远程调用会涉及到网络通讯,因此这些方法都要抛出RemoteException.
远程接口和远程对象可以由A开发,并把远程接口(Hello)d打包分给Client端开发者B。
建立f:
mi_iiop目录,把Hello.java和HelloImpl.java拷贝到该目录中。
// Hello.java
package jdeveloper.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
生成远程对象.
// HelloImpl.java
package jdeveloper.rmi_iiop;
import javax.naming.*;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
//import java.rmi.server.UnicastRemoteObject;
import javax.rmi.PortableRemoteObject;
public class HelloImpl extends PortableRemoteObject
implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello() {
return "Hello World!";
}
public static void main(String args[]) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
Hello obj = new HelloImpl();
// Bind this object instance to the name "HelloServer"
//***** old code for rmi
// Naming.rebind("HelloServer", obj);
//***** new code for rmi-iiop
Context initialNamingContext = new InitialContext();
initialNamingContext.rebind("HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
存根(Stub)和框架(Skeleton)
f:
cd
mi_iiop
set classpath=.;%classpath%
javac -d . Hello.java
javac -d . HelloImpl.java
rmic -iiop -d . jdeveloper.rmi_iiop.HelloImpl
这一步将生成<_Interface>_stub.class,<_InterfaceImpl>_Tie.class:
_Hello_Stub.class和_HelloImpl_Tie.class
2. 实现Client端程序
// HelloClient.java
package jdeveloper.rmi_iiop;
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
public class HelloClient {
public static void main(String args[]) throws Exception{
System.setSecurityManager(new RMISecurityManager());
Context initialNamingContext = new InitialContext();
Hello RemoteObj = (Hello) PortableRemoteObject.narrow(
initialNamingContext.lookup("iiop://"+ args[0] +"/HelloServer"),
Hello.class
);
System.out.println(RemoteObj.sayHello());
}
}
把HelloClient.java拷贝到目录f:
mi_iiop中。
f:
cd
mi_iiop
javac -d . HelloClient.java
3. 运行程序
启动DOS窗口
运行 tnameserv
把 server.bat 和 policy 放到f:
mi_iiop
启动新的DOS窗口
运行 server
启动新的DOS窗口
把 client.bat 放到f:
mi_iiop
运行 client hostname
server.bat
set CP=%classpath%
set classpath=.;%classpath%
java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
-Djava.naming.provider.url=iiop://hjc:900 -Djava.security.policy=policy jdeveloper.rmi_iiop.HelloImpl
set classpath=%CP%
client.bat
set CP=%classpath%
set classpath=.;%classpath%
java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory
-Djava.naming.provider.url=iiop://hjc:900 -Djava.security.policy=policy jdeveloper.rmi_iiop.HelloClient %1
set classpath=%CP%
policy
grant {
// Allow everything for now
permission java.security.AllPermission;