本文主要内容涉及:
- CORBA基本架构
- IDL文件编写
- POA示例实现
- POA+TIE示例实现
- ImplBase示例实现
- ImplBase+TIE示例实现
- Persistent示例实现
HiPersistent.idl
module HiCorba
{
interface HiPersistent
{
string sayHiTo(in string someone);
long add(in long numa, in long numb);
oneway void shutdown();
};
};
JDK提供了工具,可以直接生成stubs及skeletons接口代码:
idlj -fall HiPersistent.idl
编写server端代码:
import java.util.Properties;
import org.omg.CORBA.Object;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CORBA.Policy;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.*;
import org.omg.PortableServer.Servant;
import HiCorba.*;
class HiPersistentImpl extends HiPersistentPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
// implement sayHiTo() method
public String sayHiTo(String someone) {
return "\nHi, "+someone+" !"+"\n";
}
// implement add() method
public int add(int numa, int numb) {
return numa+numb;
}
// implement shutdown() method
public void shutdown() {
orb.shutdown(false);
}
}
public class HiPersistentServer {
public static void main( String args[] ) {
try {
// Step 1: Instantiate the ORB
ORB orb = ORB.init(args, null);
// Step 2: Instantiate the servant
HiPersistentImpl impl = new HiPersistentImpl();
impl.setORB(orb);
// Step 3 : Create a POA with Persistent Policy
// Step 3-1: Get the rootPOA
POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
// Step 3-2: Create the Persistent Policy
Policy[] persistentPolicy = new Policy[1];
persistentPolicy[0] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
// Step 3-3: Create a POA by passing the Persistent Policy
POA persistentPOA = rootPOA.create_POA("childPOA", null, persistentPolicy );
// Step 3-4: Activate HiPersistentPOA's POAManager, Without this
// All calls to HiPersistent Server will hang because POAManager
// will be in the 'HOLD' state.
persistentPOA.the_POAManager().activate( );
// Step 4: Associate the servant with HiPersistentPOA
persistentPOA.activate_object( impl );
// Step 5: Resolve RootNaming context and bind a name for the
// servant.
// NOTE: If the Server is persistent in nature then using Persistent
// Name Service is a good choice. Even if ORBD is restarted the Name
// Bindings will be intact. To use Persistent Name Service use
// 'NameService' as the key for resolve_initial_references() when
// ORBD is running.
org.omg.CORBA.Object obj = orb.resolve_initial_references( "NameService" );
NamingContextExt rootContext = NamingContextExtHelper.narrow( obj );
NameComponent[] nc = rootContext.to_name( "HiPersistent" );
rootContext.rebind( nc, persistentPOA.servant_to_reference( impl ) );
// Step 6: We are ready to receive client requests
orb.run();
} catch ( Exception e ) {
System.err.println( "Exception in Persistent Server Startup " + e );
}
}
}
编写client端代码:
import java.util.Properties;
import org.omg.CORBA.ORB;
import org.omg.CORBA.OBJ_ADAPTER;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NameComponent;
import org.omg.PortableServer.POA;
import HiCorba.*;
public class HiPersistentClient {
public static void main(String args[]) {
try {
// Step 1: Instantiate the ORB
ORB orb = ORB.init(args, null);
// Step 2: Resolve the PersistentHelloServant by using INS's
// corbaname url. The URL locates the NameService running on
// localhost and listening on 1900 and resolve
// 'PersistentServerTutorial' from that NameService
org.omg.CORBA.Object obj = orb.string_to_object("corbaname::localhost:1900#HiPersistent");
HiPersistent hiPersistent = HiPersistentHelper.narrow( obj );
// Step 3: Call the sayHello() method every 60 seconds and shutdown
// the server. Next call from the client will restart the server,
// because it is persistent in nature.
while( true ) {
System.out.println( "Calling HiPersisten Server.." );
System.out.println("Message From HiPersisten Server: " + hiPersistent.sayHiTo("neohope") );
System.out.println("70 + 80 = " + hiPersistent.add(70, 80) );
System.out.println( "Shutting down HiPersistent Server.." );
hiPersistent.shutdown( );
Thread.sleep(2000);
}
} catch ( Exception e ) {
System.err.println( "Exception in HiPersistentClient.java..." + e );
e.printStackTrace( );
}
}
}
编译代码:
javac *.java HiCorba/*.java
测试,在三个shell或cmd窗口中依次运行:
#shell01
orbd -ORBInitialPort 1900 -serverPollingTime 200
#shell02
servertool -ORBInitialPort 1900
>>欢迎使用 Java IDL 服务器工具
>>请在提示处输入命令
> register -server HiPersistentServer -applicationName s1 -classpath .
#shell03
java HiPersistentClient
>>Calling HiPersisten Server..
>>Message From HiPersisten Server:
>>Hi, neohope !
>>70 + 80 = 150
>>Shutting down HiPersistent Server..
>>
>>Calling HiPersisten Server..
>>Message From HiPersisten Server:
>>Hi, neohope !
>>70 + 80 = 150
>>Shutting down HiPersistent Server..