基于CORBA技术开发一个考试成绩查询系统

主要包括:服务器部分:实现查询和录入服务;客户端部分包括录入和查询部分


IDL:

interface DataService
{
    void insert(in string sid,in string sname,in float score);
    string check(in string sid);
};

用以下命令生成存根与框架:

idlj -fall DataService.idl

生成这些文件:
生成的

以下是核心代码:
编写服务端服务实现,并写main函数以启动corba服务
DataServiceServer.java

package cn.nwafulive.zq.server;

import cn.nwafulive.zq.db.DBmanager;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

/**
 * @Author ZhangQiong nwsuafzq@hotmail.com
 * @Date 2017/4/1
 * @Time 22:02.
 */

class DataServiceImpl extends DataServicePOA {  //具体的服务实现
    private ORB orb;

    public void setOrb(ORB orb_val) {

        this.orb = orb_val;
    }

    @Override
    public void insert(String sid, String sname, float score) {
        /**
         * @methodName insert
         * @param [sid, sname, score]
         * @return void
         * 2017/4/1 23:13
         */

        DBmanager.insert(sid,sname,score);
    }

    @Override
    public String check(String sid) {
        /**
         * @methodName check
         * @param [sid]
         * @return java.lang.String
         * 2017/4/1 23:13
         */

        String checkResult;
        checkResult = DBmanager.check(sid);
        return checkResult;
    }


}
public class DataServiceServer { 
    public static void main(String args[]) {
        try{
            System.out.println("创建和初始化 ORB ");

            ORB orb = ORB.init(args, null);

            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

            rootpoa.the_POAManager().activate();

            System.out.println("创建服务对象并将其向 ORB 注册 ");
            DataServiceImpl dataServiceImpl = new DataServiceImpl();

            dataServiceImpl.setOrb(orb);

            //System.out.println(orb.object_to_string(sysProImpl));

            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            NameComponent[] path = {new NameComponent("SysProp", "")};

            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(dataServiceImpl);
            DataService href = DataServiceHelper.narrow(ref);
            System.out.println(orb.object_to_string(href));

            System.out.println(ncRef.getClass().toString());
            ncRef.rebind(path, href);

            System.out.println("DataServiceServer ready and waiting ...");

            orb.run();

        } catch (Exception e) {
            System.err.println("Error: " + e);
            e.printStackTrace(System.out);
        }
    }
}

实现Corba客户端:

package cn.nwafulive.zq.client;

import cn.nwafulive.zq.bean.Student;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;

/**
 * @Author ZhangQiong nwsuafzq@hotmail.com
 * @Date 2017/4/2
 * @Time 15:18.
 */
public class DataServiceClient {
    public static void main(String[] args) {
        try {
            String SetInfo, ReturnInfo, ref;
            org.omg.CORBA.Object objRef;
            DataService dataServiceRef;
            ORB orb = ORB.init(args, null);
            if (args.length>=1){
                ref=args[0];
            }
            else{
                System.out.println("aaaaaaaaa");
                return ;
            }
            //下面一条语句得到的是一个NamingContext对象,并非DataService对象
            objRef = orb.string_to_object(ref);
            dataServiceRef = DataServiceHelper.narrow(objRef);
            /*objRef = orb.resolve_initial_references("NameService");
            System.out.println(orb.object_to_string(objRef));
            NamingContext ncRef = NamingContextHelper.narrow(objRef);
            NameComponent nc =new NameComponent("DataService","");
            NameComponent path[] = {nc};
            dataServiceRef = DataServiceHelper.narrow(ncRef.resolve(path));*/
            if (args.length>1)
            {
                SetInfo=args[1];
            }
            else
            {
                SetInfo="java.home";
            }

            System.out.println("开始调用");

            Student student1 = new Student();
            student1.setSid("1");
            student1.setSname("zq");
            student1.setScore(100f);

            dataServiceRef.insert(student1.getSid(),student1.getSname(),student1.getScore()); //插入

            student1.setSid("1");
            System.out.println("查的是:"+student1.getSid()+"号");

            String ckresult;
            ckresult=dataServiceRef.check(student1.getSid()); //查询
            System.out.println("result:"+ckresult);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

启动服务端前,需要先启动命名服务器,然后再启动具体服务
首先:开始–运行,输入/bin/tnameserv

启动客户端,要加参数:
idea运行带参数设置如下:
这里写图片描述
这里写图片描述

cd java_client\bin
java SysPropClient IOR:**************************************(具体看服务器产生什么样的IOR)************** java.home

运行结果:
服务端
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值