ICE 學習筆記

來源:互聯網
上載者:User
Hello.ice #ifndef SIMPLE_ICE
  #define SIMPLE_ICE
  //名字空間 or 包名
  module demo.slice.Hello
  {
  interface Hello{
   void printString(string s);
  };
  };#endif
  伺服器:
  HelloI.java
  package demo.ice.Hello.server;
  import demo.slice.Hello._HelloDisp;
  import Ice.Current;
  /**
  * @author MudfishCN
  *
  * servant class: HelloI
  * _HelloDisp 基類由slice2java編譯器產生,它是一個抽象類別。
  * _HelloDisp 抽象類別包含一個printString()方法。
  */
  public class HelloI extends _HelloDisp {
   /* (non-Javadoc)
   * @see demo._HelloOperations#printString(java.lang.String, Ice.Current)
   */
   public void printString(String s, Current __current) {
   // TODO Auto-generated method stub
   System.out.println(s);
   }
  }
  HelloServer.java
  package demo.ice.Hello.server;
  import Ice.LocalException;
  /**
  * @author MudfishCN
  * version: 1.0.1
  */
  public class HelloServer {
   public static void main(String[] args) {
   // 定義status變數,用來控制伺服器.
   int status = 0;
   Ice.Communicator ic = null; //聲明一個Communicator 對象ic
   try {
   // 初始化Ice運行時
   ic = Ice.Util.initialize(args);
   /*
   * 建立一個對象適配器(ObjectAdapter)對象IOAdapter,並初始化之。
   * 參數"HelloWorldAdapter":表示適配器的名字。
   * 參數"default -p 10000":表示適配器使用預設協議(TCP/IP)在連接埠10000處監聽到來的請求。
   * 伺服器配置完成.
   */
   Ice.ObjectAdapter IOAdapter = ic.createObjectAdapterWithEndpoints(
   "HelloWorldAdapter", "default -p 10000");
   /*
   * 為Hello介面建立一個servant.
   */
   Ice.Object IObject = (Ice.Object) new HelloI();
  
   /*
   * 將新的servant添加到適配器,
   * 並將這個新的servant命名為"HelloWorld"
   */
   IOAdapter.add(IObject, Ice.Util.stringToIdentity("HelloWorld"));
  
   /*
   * 啟用適配器,以使伺服器開始處理來自客戶的請求
   */
   IOAdapter.activate();
   /*
   * 掛起發出調用的線程,直到伺服器實現終止為止.
   * 或者是通過發出一個調用關閉運行時(run time)的指令來使伺服器終止.
   */
   ic.waitForShutdown();
   } catch (LocalException e) {
   // 捕捉ICE運行時可能拋出的所有異常
   e.printStackTrace();
   status = 1;
   } catch (Exception e) {
   // 捕捉串常量
   e.printStackTrace();
   status = 1;
   } finally {
   if (ic != null) {
   ic.destroy();
   }
   }
   System.exit(status);
   }
  }
  HelloClient.java
  package demo.ice.Hello.client;
  import Ice.ObjectPrx;
  import Ice.Util;
  import demo.slice.Hello.HelloPrx;
  import demo.slice.Hello.HelloPrxHelper;
  /**
  * @author MudfishCN
  */
  public class HelloClient {
   public static void main(String[] args) {
   int status = 0;
   String s;
   //建立一個通訊器的對象ic
   Ice.Communicator ic = null;
   try {
   // 初始化ICE運行時
   ic = Util.initialize(args);
  
   /*
   * 擷取遠程對象的代理
   * 建立一個代理對象,並用通訊器的stringToProxy()方法初始化之。
   * 參數:"HelloWorld:default -p 10000"
   * 表示:
   */
   ObjectPrx base = ic.stringToProxy("HelloWorld:default -p 10000");
   /*
   * 關鍵點
   */
   HelloPrx hello = HelloPrxHelper.checkedCast(base);
   //測試向下轉換是否成功。如果不成功拋出異常資訊"Invalid proxy".
   if (hello == null) {
   throw new Error("Invalid proxy");
   }
   //向伺服器發送十條訊息
   for(int i=1;i<=10;i++){
   s = "Hello,world!"+Integer.toString(i);
   hello.printString(s);
   }
   } catch (Ice.LocalException e) {
   e.printStackTrace();
   status = 1;
   } catch (Exception e) {
   e.printStackTrace();
   status = 1;
   } finally {
   if (ic != null) {
   //銷毀通訊串連
   ic.destroy();
   }
   }
   System.exit(status);
   }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.