ICE java實現helloworld

目錄

一 前言

二 實現

1、目錄結構

2、java實現

3、spring實現(spring啓動ice)

三 項目git  https://github.com/HandsomeMars/springboot-ice-demo


一 前言

1、基於上篇slice2java編譯,本文接着使用slice2java實現簡單的客戶端和服務端

2、本文列舉兩種實現:

  • java main方法調用
  • 結合spring IOC容器管理

二 實現

1、目錄結構

src
├─main
│  ├─java
│  │  ├─com
│  │  │  └─zyj
│  │  │      │  SpringbootIceApplication.java
│  │  │      │
│  │  │      ├─ice
│  │  │      │  ├─java     ##java實現
│  │  │      │  │  ├─client
│  │  │      │  │  │      IceClient.java
│  │  │      │  │  │
│  │  │      │  │  └─server
│  │  │      │  │          HelloServant.java
│  │  │      │  │          IceService.java
│  │  │      │  │
│  │  │      │  └─spring   ##spring結合
│  │  │      │      ├─client
│  │  │      │      │      IceClient.java
│  │  │      │      │
│  │  │      │      └─server
│  │  │      │              HelloServant.java
│  │  │      │              IceService.java
│  │  │      │
│  │  │      └─util
│  │  │              SpringContextUtil.java
│  │  │
│  │  └─slice2java        ##slice生成文件
│  │          Callback_HelloI_hello.java
│  │          HelloI.java
│  │          HelloIHolder.java
│  │          HelloIPrx.java
│  │          HelloIPrxHelper.java
│  │          HelloIPrxHolder.java
│  │          _Callback_HelloI_hello.java
│  │          _HelloIDisp.java
│  │          _HelloIOperations.java
│  │          _HelloIOperationsNC.java
│  │
│  └─resources
│      │  application.properties
│      │
│      └─slice
│              slice.bat
│              test.ice
│
└─test
    └─java
        └─com
            └─zyj
                    SpringbootIceApplicationTests.java

2、java實現

step1: HelloServant 實現服務端  _xxxDisp抽象類

package com.zyj.ice.java.server;

import Ice.Current;
import Ice.StringHolder;
import slice2java._HelloIDisp;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class HelloServant extends _HelloIDisp {
    @Override
    public int hello(String instr, StringHolder outstr, Current __current) {
        System.out.println("server接受:"+instr+" 時間:"+System.currentTimeMillis());
        outstr.value="hello client";
        return 0;
    }
}

setp2:配置server 

package com.zyj.ice.java.server;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class IceService {
    /**服務名*/
    private static final String SERVER_NAME="Hello";
    /**服務端點*/
    private static final String SERVER_ENDPOINT="tcp -p 10006";


    public static void main(String[] args) {
        //獲取實現類 SpringContextUtil.getBean(helloServant)
        HelloServant helloServant=new HelloServant();

        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
            communicator = Ice.Util.initialize(args);

            //創建一個名爲queryEmployeeAdapter的適配器並且默認使用tcp協議  服務部署在10.4.30.81機器上 服務開啓10006監聽端口

            Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints(SERVER_NAME,SERVER_ENDPOINT);

            // 將servant與ice對象標識符建立映射關係,並添加到ice對象適配器中
            adapter.add(helloServant, Ice.Util.stringToIdentity(SERVER_NAME));

            // 激活對象適配器
            adapter.activate();

            System.out.println("服務啓動");
            // 服務在退出之前一直保持監聽狀態
            communicator.waitForShutdown();

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //異常銷燬通信器
            if(communicator != null){
                communicator.destroy();
            }
        }
    }


}

step3:IceClient 使用 xxxPrxHelper 獲取 xxxPrx

package com.zyj.ice.java.client;

import Ice.StringHolder;
import slice2java.HelloIPrx;
import slice2java.HelloIPrxHelper;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class IceClient {

    /**服務名*/
    private static final String SERVER_NAME="Hello";
    /**服務端點*/
    private static final String SERVER_ENDPOINT="tcp -h 127.0.0.1 -p 10006";


    public static void main(String[] args) {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
            communicator = Ice.Util.initialize(args);

            //構建服務端的代理對象  服務端對象標識以 SERVER_NAME:SERVER_ENDPOINT 格式
            Ice.ObjectPrx op = communicator.stringToProxy(SERVER_NAME+":"+SERVER_ENDPOINT);

            //檢查通用客戶端代理op 是不是queryServer對象標識符所關聯的ice對象的代理
            HelloIPrx qp = HelloIPrxHelper.checkedCast(op);

            if(qp == null){
                throw new Exception("qp == null");
            }

            //測試發送信息到戶無端
            StringHolder holder=new StringHolder();
            int result = qp.hello("hello",holder);

            // 輸出服務端返回結果
            System.out.println("client接受:"+holder.value+" 時間:"+System.currentTimeMillis());

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

        }
    }

}

step4:啓動驗證

啓動server

 啓動client

3、spring實現(spring啓動ice)

step1:改造服務實現類(添加@Service註解)

package com.zyj.ice.spring.server;

import Ice.Current;
import Ice.StringHolder;
import org.springframework.stereotype.Service;
import slice2java._HelloIDisp;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class HelloServant extends _HelloIDisp {
    @Override
    public int hello(String instr, StringHolder outstr, Current __current) {
        System.out.println("server接受:"+instr+" 時間:"+System.currentTimeMillis());
        outstr.value="hello client";
        return 0;
    }
}

 step2:改造server  主要防止ice阻塞,所以通過線程處理

package com.zyj.ice.spring.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class IceService implements Runnable{

    /**服務名*/
    private static final String SERVER_NAME="Hello";
    /**服務端點*/
    private static final String SERVER_ENDPOINT="tcp -p 10006";

    @Autowired
    private HelloServant helloServant;

    @PostConstruct
    private void startIceServer() {
        //構造線程池啓動當前任務
        LinkedBlockingQueue<Runnable> runnableList=new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(100,100,1L, TimeUnit.SECONDS,runnableList);
        threadPoolExecutor.execute(this);
    }


    @Override
    public void run() {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
            communicator = Ice.Util.initialize();

            //創建一個名爲queryEmployeeAdapter的適配器並且默認使用tcp協議  服務部署在10.4.30.81機器上 服務開啓10006監聽端口

            Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints(SERVER_NAME,SERVER_ENDPOINT);

            // 將servant與ice對象標識符建立映射關係,並添加到ice對象適配器中
            adapter.add(helloServant, Ice.Util.stringToIdentity(SERVER_NAME));

            // 激活對象適配器
            adapter.activate();

            System.out.println("服務啓動");
            // 服務在退出之前一直保持監聽狀態
            communicator.waitForShutdown();

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //異常銷燬通信器
            if(communicator != null){
                communicator.destroy();
            }
        }

    }
}

step3:改造客戶端(客戶端連接服務端後,可以保持服務端xxxPrx對象持續調用)

package com.zyj.ice.spring.client;

import Ice.StringHolder;
import org.springframework.stereotype.Service;
import slice2java.HelloIPrx;
import slice2java.HelloIPrxHelper;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class IceClient implements Runnable{

    /**服務名*/
    private static final String SERVER_NAME="Hello";
    /**服務端點*/
    private static final String SERVER_ENDPOINT="tcp -h 127.0.0.1 -p 10006";


    @PostConstruct
    private void startIceClient() {
        //構造線程啓動客戶端
        LinkedBlockingQueue<Runnable> runnableList=new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(100,100,1L, TimeUnit.SECONDS,runnableList);
        threadPoolExecutor.execute(this);
    }


    @Override
    public void run() {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args傳入一下ice初始化的參數如超時時間,線程池大小等
            communicator = Ice.Util.initialize();

            //構建服務端的代理對象  服務端對象標識以 SERVER_NAME:SERVER_ENDPOINT 格式
            Ice.ObjectPrx op = communicator.stringToProxy(SERVER_NAME+":"+SERVER_ENDPOINT);

            //檢查通用客戶端代理op 是不是queryServer對象標識符所關聯的ice對象的代理
            HelloIPrx qp = HelloIPrxHelper.checkedCast(op);

            if(qp == null){
                throw new Exception("qp == null");
            }

            //測試發送信息到戶無端
            StringHolder holder=new StringHolder();
            int result = qp.hello("hello",holder);

            // 輸出服務端返回結果
            System.out.println("client請求結果:"+holder.value+" 時間:"+System.currentTimeMillis());

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

        }
    }
}

step4 啓動

三 項目git  https://github.com/HandsomeMars/springboot-ice-demo

https://github.com/HandsomeMars/springboot-ice-demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章