首页 > 编程 > Java > 正文

Java CORBA入门

2019-11-17 06:28:13
字体:
来源:转载
供稿:网友

Below is a simple example of a CORBA PRogram
download the source file 

<b>1. prodUCe a idl file like this</b>
   hello.idl
   module HelloApp {
     interface Hello    {         
         string sayHello();
    };
  };

<b>2. produce stub and skeleton files through idltojava.exe</b>
   idltojava hello.idl
   idltojava is now named as idlj.exe and is included in the JDK. 

<b>3. write a server program like this </b>

// HelloServer.java 
  
import HelloApp.*;

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

import java.io.*;
class HelloServant extends _HelloImplBase 
{
    public String sayHello()
    {
       return "Hello world !!"; 
    }   
  
}

public class HelloServer {

    public static void main(String args[])
    {
try{
    // create and initialize the ORB
    ORB orb = ORB.init(args, null);

    // create servant and register it with the ORB
    HelloServant helloRef = new HelloServant();
    orb.connect(helloRef);

    // get the root naming context
    org.omg.CORBA.Object objRef = 
orb.resolve_initial_references("NameService");
    NamingContext ncRef = NamingContextHelper.narrow(objRef);

    // bind the Object Reference in Naming
    NameComponent nc = new NameComponent("Hello", "");

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表