ICE通訊架構

來源:互聯網
上載者:User

        Ice 出自ZeroC名門之下 , Ice 是一種物件導向的中介軟體平台。從根本上說,這意味著Ice 為構建物件導向的客戶-伺服器應用提供了工具、API 和庫支援。Ice 應用適合於異構平台環境中使用:客戶和伺服器可以採用不同的程式設計語言,可以運行在不同的作業系統和機器架構上,並且可以使用多種網路技術進行通訊。無論部署環境如何,這些應用的源碼都是可移植的。 
        Zeroc ICE ( Internet Communications Engine )中介軟體號稱標準統一,開源,跨平台,跨語言,分布式,安全,服務透明,負載平衡,物件導向,效能優越,防火期穿透,通訊屏蔽。因此相比 Corba,DCOM,SOAP,J2EE等的中介軟體技術,自然是集眾多優點於一身,而卻沒有他們的缺點。

         其採用C/S 模式結構,支援同步調用方式和非同步呼叫方式,非同步派發調用方式。支援跨語言的對象調用。多種語言之間採用共同的Slice(Specification Language for Ice)進行溝通。支援ice到C,JAVA,C#,VB,Python,Ruby,PHP等多種語言的映射。 Ice具有豐富的特性,其效能遠是基於jms 所不能比的。

         下面記錄一個基於java的ice應用過程

 1 下載最新安裝包;

           http://www.zeroc.com/download.html 根據作業系統和需要選擇

           我這裡安裝的是ice-3.3.0

 2 安裝之後的Ice相關路徑:
            slice2cpp,slice2java在/bin/下 
             Ice.jar 儲存於/lib/java2/下
            相關的Ice的庫儲存於/lib下.

3.建立ice檔案

model.ice

 

#ifndef _MODEL
#define _MODEL

module com
{
   module alan
   {
     module generated
     {
         module model
         {
                 /**定義整型數組**/
                    sequence<int> IntegerArray;

                    /**自訂Map類型**/
                    dictionary<string, string> CustomMap;

                    /**訊息類型**/
                    enum MessageType {ERROR,INFO,WARNING};

                    /**訊息的操作類型**/
                    enum ActionType  {Add,Modifiy,Remove,Stop,Start,Pause};
                
                    /** 訊息結構 **/
                    ["java:getset"]
                    struct Message {
                         /**訊息類型**/
                         MessageType type;
                         /**訊息類型**/
                         ActionType action;
                         /**相關id**/
                         IntegerArray relatedIds;
                         /**擴充屬性**/
                         CustomMap extention;
                    };
            };
        };
 };
};
#endif

 

service.ice

     #ifndef _GENERATED
     #define _GENERATED

     #include <model.ice>

      module com
     {
            module alan
            {
             module generated
              {
                interface MessageServiceIce
               {

                /**
                 *  向ice服務發送資訊
                 *  @param message 訊息內容
                 *  @return true 成功  false 失敗
                 */
                string sendMessage(model::Message msg);
               };
          };
        };
    };
    #endif

   4.dos環境下執行(可以搞成.bat檔案)

          cd E:/workspace/ICETest/slice

          E:/Ice-3.3.0/bin/slice2java -I. --output-dir=../src *.ice //生產代碼

          E:/Ice-3.3.0/bin/slice2html -I. --output-dir=doc *.ice//生產doc文檔,可以忽略

       

   5.代碼生產後結構(實際應用中可以吧generted 打包放置到client和server端)

     

匯入ice.jar編寫代碼,MessageServiceIceImpl .java代碼:

public class MessageServiceIceImpl extends _MessageServiceIceDisp {
  
 public String sendMessage(Message msg, Current __current) {
  String str = msg.getType() +" "+ msg.getAction()+" " + Arrays.toString(msg.getRelatedIds());
     return str;
 }

}

伺服器端代碼:

 

public class IceService {

 public static void main(String[] args){
   int status = 0;
      Communicator ic = null;
   try{
   ic = Util.initialize(args);
   ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("testAdapter", "tcp -h localhost -p 10000");
   ObjectImpl object = new MessageServiceIceImpl();
   adapter.add(object, ic.stringToIdentity("testAdapter"));
   adapter.activate(); 
   ic.waitForShutdown(); 
   } catch (Ice.LocalException e) {
    e.printStackTrace();
    status = 1;
   } catch (Exception e) {
    System.err.println(e.getMessage());
    status = 1;
   }
  if (ic != null) {
   try {
    ic.destroy();
   } catch (Exception e) {
    System.err.println(e.getMessage());
    status = 1;
   }
  }
  System.exit(status);
   
 }

用戶端代碼:

public class IceClient {

 public static void main(String[] args){
      int status = 0; 
   Communicator ic = null; 
   try{
    ic = Ice.Util.initialize(args);
    Ice.ObjectPrx base = ic.stringToProxy("testAdapter:tcp -h localhost -p 10000"); 

    MessageServiceIcePrx client = MessageServiceIcePrxHelper.checkedCast(base); 
    if (client == null) 
      throw new Error("Invalid proxy"); 
    Map<String ,String > map = new HashMap<String, String>();
       Message msg = new Message(MessageType.INFO, ActionType.Add,new int[]{1},map);
       System.out.println(client.sendMessage(msg));//這裡調用了Service端方法
   } catch (Ice.LocalException e) {
    e.printStackTrace();
    status = 1;
   } catch (Exception e) {
    System.err.println(e.getMessage());
    status = 1;
  }
  if (ic != null) {
   try {
    ic.destroy();
   } catch (Exception e) {
    System.err.println(e.getMessage());
    status = 1;
   }
  }
     System.exit(status);
 }
}

6.最後我們開始運行Server,再運行Client,看到控制台輸出INFO Add [1],一個基本的ice應用就完成了!

關於ICE 更多請關注下篇:http://blog.csdn.net/liuzhoulong/article/details/6228333

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.