CORBA简单的例子

1.文件IDL:
/*Message.idl*/ 
module gmit 
{
 interface Message
 {
  string GetMessage();
  oneway void shutdown();
 };
};
 
//CMD 下运行: idlj -fall Message.idl 
生成文件夹 gmit, 以及gmit下文件
 Message.java
 MessageHelper.java
 MessageHolder.java
 MessageOperations.java
 MessagePOA.java
 _MessageStub.java
 
2.编写服务器端 
在gmit目录下 创建文件
/*MessageImpl.java*/
package gmit;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA.*;
public class MessageImpl extends MessagePOA
{
 private ORB orb ;
 public void setORB( ORB orbValue)
 {
   this.orb = orbValue ;
 }
 public String GetMessage()
 {
  return "hello from huan " ;
 }
 public void shutdown()
 {
  orb.shutdown(false);
 }
}
 
/*MessageServer.java*/
package gmit;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.* ;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA.*;
public class MessageServer
{
 public static void main(String[] args ) throws Exception
 {
  ORB orb = ORB.init(args , null );

 //get the reference to the POA and activate the POA manager
 POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA")) ;
 rootPOA.the_POAManager().activate();
 //Create the remote Object and register it with the orb
 MessageImpl messageImpl = new MessageImpl() ;
 //get object ref from the remote object
 org.omg.CORBA.Object ref = rootPOA.servant_to_reference(messageImpl);
 Message href = MessageHelper.narrow(ref);
 //get the root Naming context
 org.omg.CORBA.Object objectRef = orb.resolve_initial_references("NameService");
 NamingContextExt ncRef = NamingContextExtHelper.narrow(objectRef) ;
 //Bind the object
 String name = "DSMessage" ;
 NameComponent path[] = ncRef.to_name(name);
 ncRef.rebind(path, href );
 //start orb and wait
 System.out.println("MessageServer ready...!");
 orb.run();
 }
}
 
3.编写客户端
/*MessageClient.java*/

package gmit ;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.* ;

public class MessageClient
{
 public static void main(String[] args) throws Exception
 {

  //create the initialise the orb
  ORB orb = ORB.init(args, null );


  //Get the root naming context
  org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
  NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef) ;

  //Get a handle on the remote Ojbect
  Message message = MessageHelper.narrow(ncRef.resolve_str("DSMessage"));

  //Invoke remote methods
  System.out.println(message.GetMessage());
  System.out.println("Object ref :" + message);
  message.shutdown();
 }
}


5.运行客户端 和 服务器端。 

1. javac gmit\*.java

2. 

(main window)

>>start 

(window 1)

>> orbd -ORBInitialPort 1050 -ORBInitialHost localhost

(main window)

>>start 

(window 2)

>>java gmit.MessageServer -ORBInitialPort 1050 -ORBInitialHost localhost

(main window)

>>start 

(window 3)

>>java gmit.MessageClient -ORBInitialPort 1050 -ORBInitialHost localhost

看到结果:

hello from huan
Object ref :IOR:000000000000001549444c3a676d69742f4d6573736167653a312e3000000000
000000010000000000000086000102000000000c31302e32382e312e3230330004b3000000000031
afabcb0000000020c3f2ef9d00000001000000000000000100000008526f6f74504f410000000008
00000001000000001400000000000002000000010000002000000000000100010000000205010001
0001002000010109000000010001010000000026000000020002

 

你可能感兴趣的:(java)