java ICE简单DEMO

在pom文件中加入ice的jar包

 
            com.zeroc
            ice
            ${ice-version}
        

        
            com.zeroc
            icebox
            ${ice-version}
        

1、编写ice文件

[["java:package:com.qn.rain"]] // 定义java包名
module demo
{
    interface MyIce
    {
        string queryMyIce(string request);
    }
}

2、用ice客户端运行之后(版本3.7.0),得到4个java文件

java ICE简单DEMO_第1张图片

3、服务端的骨架类MyIceI

package com.qn.rain.demo.impl;

import com.alibaba.fastjson.JSON;
import com.qn.model.Request;
import com.qn.rain.demo.MyIce;
import com.zeroc.Ice.Current;

/**
 * Created by win 10 on 2018/6/11.
 */
public class MyIceI implements MyIce {
    @Override
    public String queryMyIce(String request, Current current) {
        System.out.println("接收到消息:" + request);
        return this.getResponse(request);
    }
}

4、搭建服务端

package com.qn.server;

import com.qn.rain.demo.impl.MyIceI;
import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.Object;
import com.zeroc.Ice.ObjectAdapter;
import com.zeroc.Ice.Util;

/**
 * 启动iceServer
 * Created by win 10 on 2018/6/4.
 */
public class QueryServer {
    public static void main(String[] args) {
        int state = 0;
        Communicator communicator = null;
        try {
            // 初始化ICE通信器communicator,可以使用args传入ice初始化的参数,如:超时时间、线程池的大小等
            communicator = Util.initialize(args);
            // 撞见一个名为queryEmployeeAdapter的适配器 并且默认使用tcp协议 部署在10.0.0.1机器上 服务开启10006监听端口
            ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints("queryServer", "default -h 127.0.0.1 -p 10006");
            // 创建服务端代码实现实现类
            Object object = new MyIceI();
            // 将服务端实现类与ice对象标识符简历映射关系,并添加到ice对象适配器中
            adapter.add(object, Util.stringToIdentity("QUER_Controller"));
            // 激活对象适配器
            adapter.activate();
            System.out.println("queryServer已经激活");
            // 服务在退出之前一直保持监听状态
            communicator.waitForShutdown();
        } catch (Exception e) {
            state = 1;
            System.out.println(e);
            e.printStackTrace();
        } finally {
            if (communicator != null) {
                communicator.destroy();
            }
        }
        System.out.println("state:" + state);
    }
}

5、搭建客户端

package com.qn;

import com.qn.model.Request;
import com.qn.rain.demo.MyIcePrx;
import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.ObjectPrx;
import com.zeroc.Ice.Util;

/**
 * Created by win 10 on 2018/6/4.
 */
public class QueryEmployeeClient {
    public static void main(String[] args) throws Exception{
        Communicator communicator = null;
        try {
            // 模拟传参
            Request request = new Request();
            request.setBody("我是测试request");

            // 初始化ice通信器 设置超时时间
            communicator = Util.initialize();
            // 传入远程服务单元的ice对象标识符
            ObjectPrx objectPrx = communicator.stringToProxy("QUER_Controller:default -h 127.0.0.1 -p 10006");
            // 检验是否连接上
            MyIcePrx myIcePrx = MyIcePrx.checkedCast(objectPrx);
            if(myIcePrx==null){
                System.err.println("未连接上");
                throw new Exception("没有链接成功");
            }
            String response = myIcePrx.queryMyIce(request.getBody().toString());
            System.out.println("接收成功:"+response);

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

先启动服务端再启动客户端就可看到Holly world消息

你可能感兴趣的:(java)