RMI-IIOP
外观
< Java 编程
导航 并发编程 主题: ) |
一位维基教科书用户建议将本书或章节合并到 Java 编程/远程方法调用 中。 请在 讨论页面 上讨论是否应该进行此合并。 |
RMI-IIOP (通过 IIOP 的 RMI) 与 Java SDK 一起提供。它将远程方法调用 (RMI) 技术与 Internet 互操作轨道协议 (IIOP) 相结合,为 Java 平台提供 CORBA。Java 开发人员无需提供 IDL 即可提供 CORBA 功能。
代码清单 7.1:HowdyInterface.java
import java.rmi.Remote;
public interface HowdyInterface extends java.rmi.Remote {
public void sayHowdy() throws RemoteException;
}
|
上面的代码定义了一个名为 HowdyInterface 的远程接口,它将定义远程客户端可以在服务器上调用的内容。所有操作都必须抛出 RemoteException。该接口必须扩展 java.rmi.Remote。
代码清单 7.2:HowdyImpl.java
import javax.rmi.PortableRemoteObject;
import javax.rmi.RemoteException;
public class HowdyImpl implements HowdyInterface {
public HelloImpl() throws java.rmi.RemoteException {
PortableRemoteObject.exportObject(this); // Initializes the remote object
}
public void sayHowdy() throws RemoteException {
System.out.println("Weeee doggies! Howdy!!");
}
}
|
实现类允许对象进行 ORB 初始化。它还实现了要调用的远程操作。实现可以扩展 PortableRemoteObject
,在这种情况下,构造函数中的 exportObject
调用将被删除。更好的方法似乎是像上面代码中那样。
代码清单 7.3:HowdyServer.java
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject ;
import com.sun.corba.se.internal.POA.POAORB;
import org.omg.PortableServer.*;
import java.util.*;
import org.omg.CORBA.*;
import javax.rmi.CORBA.Stub;
import javax.rmi.CORBA.Util;
public class HowdyServer {
public HowdyServer(String[] args) {
try {
Properties p = System.getProperties();
// add runtime properties here
p.put("org.omg.CORBA.ORBClass",
"com.sun.corba.se.internal.POA.POAORB");
p.put("org.omg.CORBA.ORBSingletonClass",
"com.sun.corba.se.internal.corba.ORBSingleton");
ORB orb = ORB.init( args, p );
POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");
Policy[] tpolicy = new Policy[3];
tpolicy[0] = rootPOA.create_lifespan_policy(
LifespanPolicyValue.TRANSIENT );
tpolicy[1] = rootPOA.create_request_processing_policy(
RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY );
tpolicy[2] = rootPOA.create_servant_retention_policy(
ServantRetentionPolicyValue.RETAIN);
POA tPOA = rootPOA.create_POA("MyTransientPOA", null, tpolicy);
tPOA.the_POAManager().activate();
HowdyImpl howdyImpl = new HowdyImpl();
_HowdyImpl_Tie tie = (_HowdyImpl_Tie)Util.getTie( howdyImpl );
String howdyId = "howdy";
byte[] id = howdyId.getBytes();
tPOA.activate_object_with_id( id, tie );
Context initialNamingContext = new InitialContext();
initialNamingContext.rebind("HowdyService",
tPOA.create_reference_with_id(id,
tie._all_interfaces(tPOA,id)[0]) );
System.out.println("Howdy Server: Ready...");
orb.run();
}
catch (Exception e) {
System.out.println("Error running HowdyServer: " + e);
e.printStackTrace();
}
}
public static void main(String args[]) {
new HowdyServer( args );
}
}
|
代码清单 7.4:HelloClient.java
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import javax.rmi.*;
import java.util.Vector;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.naming.Context;
public class HelloClient {
public static void main( String args[] ) {
Context ic;
Object objref;
HelloInterface hi;
try {
ic = new InitialContext();
} catch (NamingException e) {
System.out.println("failed to obtain context" + e);
e.printStackTrace();
return;
}
try {
objref = ic.lookup("HowdyService");
System.out.println("Client: Obtained a reference to Howdy server.");
} catch (NamingException e) {
System.out.println("failed to lookup object reference");
e.printStackTrace();
return;
}
try {
hi = (HowdyInterface) PortableRemoteObject.narrow(
objref, HowdyInterface.class);
hi.sayHowdy();
} catch (ClassCastException e) {
System.out.println("narrow failed");
e.printStackTrace();
return;
} catch( Exception e ) {
System.err.println( "Exception " + e + "Caught" );
e.printStackTrace( );
return;
}
}
}
|
客户端代码使用名称服务查找服务器并进行远程调用。