Ice的HelloWorld(Java)

Ice是一种面向对象的中间间平台,入门ice,简单的HelloWorld是必不可少的。

转载请注明http://www.cnblogs.com/zrtqsk/p/3745286.html,谢谢。

一、写一个ice定义

  ice用来定义接口、程序骨架的,方便远程调用。首先我们要使用ice专属的slice语言写一个ice定义。如下:

file_name:HelloWorld.ice

module myHelloWorld{
    interface HelloWorld{
        void say(string s);
    };
};

module相当于java中的包,是ice定义中必不可少的。

二、编译这个ice定义  

  要编译这个ice定义,首先要安装一个ice服务器,将其中的bin文件夹加入环境变量。跟jdk配置类似。然后使用其中的slice2java命令编译。

  例如HelloWorld.ice文件放在我F盘的test文件夹,运行,就可以在test目录出现如下文件:

  

  myHelloWorld文件夹名是ice定义中的module名。

三、编写Servant类

  servant类是ice服务器端提供操作调用的行为制品,它提供的是ice对象的实质内容。说白了,就是ice所定义的接口,在服务器端的实现类。

  按照管理,servant类名是ice接口名加一个“I”后缀,如下:

  

package client;

import Ice.Current;
import myHelloWorld._HelloWorldDisp;

public class HelloWorldI extends _HelloWorldDisp{

    private static final long serialVersionUID = 1L;

    @Override
    public void say(String s, Current __current) {
        System.out.println("Hello World!"+" the string s is " + s);
    }
}

如上,servant类需要继承_HelloWorldDisp这个slice2java编译生成的抽象类。实现方法参数是原本的形参加一个Ice的Current对象。

四、编写远程的服务器类

package client;
public class Server {

    public static void main(String[] args)
    {
        int status = 0;
        // Communicator实例,是ice run time的主句柄
        Ice.Communicator ic = null;
        try
        {
            // 调用Ice.Util.Initialize()初始化Ice run time
            System.out.println("初始化ice run time...");
            ic = Ice.Util.initialize(args);
            
            
            // 创建一个对象适配器,传入适配器名字和在10000端口处接收来的请求
            System.out.println("创建对象适配器,监听端口10000...");
            Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
            
            // 实例化一个PrinterI对象,为Printer接口创建一个servant
            System.out.println("为接口创建servant...");
            Ice.Object object = new HelloWorldI();

            // 调用适配器的add,告诉它有一个新的servant,传递的参数是刚才的servant,这里的“SimplePrinter”是Servant的名字
            System.out.println("对象适配器加入servant...");
            adapter.add(object, Ice.Util.stringToIdentity("SimplePrinter"));
            
            //调用适配器的activate()方法,激活适配器。被激活后,服务器开始处理来自客户的请求。
            System.out.println("激活适配器,服务器等待处理请求...");
            adapter.activate();
            
            //这个方法挂起发出调用的线程,直到服务器实现终止为止。或我们自己发出一个调用关闭。
            ic.waitForShutdown();
        } catch (Ice.LocalException e)
        {
            e.printStackTrace();
            status = 1;
        } catch (Exception e)
        {
            e.printStackTrace();
            status = 1;
        } finally
        {
            if (ic != null)
            {
                ic.destroy();
            }
        }
        System.exit(status);
    }
}

这个服务器类其实就寥寥几行代码,注释已经很清楚了。

五、编写客户端类

package client;
import myHelloWorld.HelloWorldPrx;
import myHelloWorld.HelloWorldPrxHelper;

public class Client {

    public static void main(String[] args)
    {
        int status = 0;
        // Communicator实例
        Ice.Communicator ic = null;
        try
        {
            // 调用Ice.Util.Initialize()初始化Ice run time
            ic = Ice.Util.initialize(args);

            // 获取远地打印机的代理
            Ice.ObjectPrx base = ic.stringToProxy("SimplePrinter:default -p 10000");

            // 将上面的代理向下转换成一个Printer接口的代理
            HelloWorldPrx helloWorld = HelloWorldPrxHelper.checkedCast(base);

            // 如果转换成功
            if (helloWorld == null)
            {
                throw new Error("Invalid proxy");
            }

            // 调用这个代理,将字符串传给它
            helloWorld.say("bar");

        } catch (Ice.LocalException e)
        {
            e.printStackTrace();
            status = 1;
        } catch (Exception e)
        {
            e.printStackTrace();
            status = 1;
        } finally
        {
            if (ic != null)
            {
                ic.destroy();
            }
        }
        System.exit(status);
    }
}

六、运行

  先运行服务器,服务器线程一直运行等待,如下:  

初始化ice run time...
创建对象适配器,监听端口10000...
为接口创建servant...
对象适配器加入servant...
激活适配器,服务器等待处理请求...

  运行了客户端后,显示如下:

初始化ice run time...
创建对象适配器,监听端口10000...
为接口创建servant...
对象适配器加入servant...
激活适配器,服务器等待处理请求...
Hello World! the string s is bar

  将客户端改一下 helloWorld.say("foo");,再运行,显示如下:

初始化ice run time...
创建对象适配器,监听端口10000...
为接口创建servant...
对象适配器加入servant...
激活适配器,服务器等待处理请求...
Hello World! the string s is bar
Hello World! the string s is foo

(部分参考《Ice分布式程序设计》)

转载于:https://www.cnblogs.com/zrtqsk/p/3745286.html

猜你喜欢

转载自blog.csdn.net/weixin_33860737/article/details/93248524
ice