Jump to content
Wikibooks The Free Textbook Project

RMI-IIOP

From Wikibooks, open books for an open world

This is the current revision of this page, as edited by DannyS712 (discuss | contribs) at 05:58, 16 April 2020 (Update syntaxhighlight tags - remove use of deprecated <source> tags). The present address (URL) is a permanent link to this version.

Revision as of 05:58, 16 April 2020 by DannyS712 (discuss | contribs) (Update syntaxhighlight tags - remove use of deprecated <source> tags)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A Wikibookian suggests that this book or chapter be merged into Java Programming/Remote Method Invocation .
Please discuss whether or not this merge should happen on the discussion page.

RMI-IIOP (RMI over IIOP) is provided with the Java SDK. It combines Remote Method Invocation (RMI) technology with the Internet Inter-Orb Protocol (IIOP) to provide CORBA to the Java platform. Java developers do not have to provide IDL in order to provide CORBA capabilities.

The Remote Interface

[edit | edit source ]
Computer code Code listing 7.1: HowdyInterface.java
importjava.rmi.Remote;
publicinterface HowdyInterfaceextendsjava.rmi.Remote{
publicvoidsayHowdy()throwsRemoteException;
}

The above code defines a remote interface called HowdyInterface that will define what the remote client may call on the server. All of the operations must throw a RemoteException. The interface must extend java.rmi.Remote.

The Implementation Class

[edit | edit source ]
Computer code Code listing 7.2: HowdyImpl.java
importjavax.rmi.PortableRemoteObject;
importjavax.rmi.RemoteException;
publicclass HowdyImplimplementsHowdyInterface{
publicHelloImpl()throwsjava.rmi.RemoteException{
PortableRemoteObject.exportObject(this);// Initializes the remote object
}
publicvoidsayHowdy()throwsRemoteException{
System.out.println("Weeee doggies! Howdy!!");
}
}

The implementation class allows for the object to be ORB-initialized. It also implements the remote operation to be called. The implementation could have extended PortableRemoteObject, in which case the exportObject call in the constructor would be removed. The better approach would seem to be as coded above.

The Server Class

[edit | edit source ]
Computer code Code listing 7.3: HowdyServer.java
importjavax.naming.InitialContext;
importjavax.naming.Context;
importjavax.rmi.PortableRemoteObject;
importcom.sun.corba.se.internal.POA.POAORB;
importorg.omg.PortableServer.*;
importjava.util.*;
importorg.omg.CORBA.*;
importjavax.rmi.CORBA.Stub;
importjavax.rmi.CORBA.Util;
publicclass HowdyServer{
publicHowdyServer(String[]args){
try{
Propertiesp=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");
ORBorb=ORB.init(args,p);
POArootPOA=(POA)orb.resolve_initial_references("RootPOA");
Policy[]tpolicy=newPolicy[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);
POAtPOA=rootPOA.create_POA("MyTransientPOA",null,tpolicy);

tPOA.the_POAManager().activate();
HowdyImplhowdyImpl=newHowdyImpl();
_HowdyImpl_Tietie=(_HowdyImpl_Tie)Util.getTie(howdyImpl);
StringhowdyId="howdy";
byte[]id=howdyId.getBytes();
tPOA.activate_object_with_id(id,tie);
ContextinitialNamingContext=newInitialContext();
initialNamingContext.rebind("HowdyService",
tPOA.create_reference_with_id(id,
tie._all_interfaces(tPOA,id)[0]));
System.out.println("Howdy Server: Ready...");

orb.run();
}
catch(Exceptione){
System.out.println("Error running HowdyServer: "+e);
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
newHowdyServer(args);
}
}

The Client Class

[edit | edit source ]
Computer code Code listing 7.4: HelloClient.java
importjava.rmi.RemoteException;
importjava.net.MalformedURLException;
importjava.rmi.NotBoundException;
importjavax.rmi.*;
importjava.util.Vector;
importjavax.naming.NamingException;
importjavax.naming.InitialContext;
importjavax.naming.Context;
publicclass HelloClient{
publicstaticvoidmain(Stringargs[]){
Contextic;
Objectobjref;
HelloInterfacehi;
try{
ic=newInitialContext();
}catch(NamingExceptione){
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(NamingExceptione){
System.out.println("failed to lookup object reference");
e.printStackTrace();
return;
}
try{
hi=(HowdyInterface)PortableRemoteObject.narrow(
objref,HowdyInterface.class);
hi.sayHowdy();
}catch(ClassCastExceptione){
System.out.println("narrow failed");
e.printStackTrace();
return;
}catch(Exceptione){
System.err.println("Exception "+e+"Caught");
e.printStackTrace();
return;
}
}
}

The client code uses the name service to look up the server and make the remote invocation.


[画像:Clipboard]

To do:
Add some exercises like the ones in Variables

AltStyle によって変換されたページ (->オリジナル) /