ICEインスタンス(EclipseとSlice 2 Javaプラグイン)

14999 ワード

link:http://hi.baidu.com/dd_taiyangxue/blog/item/c7 f 01444 b 4 a 82 acffca 382.
一、準備条件
1、Eclipse 3.4
2、Slice 2 Javaプラグイン:ダウンロードアドレスと使用説明:http://www.zeroc.com/eclipse.html、具体的には以下の通りである。
ZeroC hosts an Eclipse plug-i-n site that you can add to your Eclipse configration.Follow these steps to install the Slice 2 Java plug-n:
  • From the Help menu,chose Software Updates
  • Select the Available Software tab
  • Click Add Site
  • In the Location field,enterhttp://www.zeroc.com/download/eclipse
  • Select the Slice 2 Java plug-n and click Install
  • The plug-n requires Ice 3.3.1 or later.Users of Ice for Android 0.1 may also use the plug-n but you must up grade your Ice installation to version 3.3.1 or later.
    インストール成功後:EclipseのWindowをクリックします。preferencesは以下の図の通りです。
    ICE实例(Eclipse与Slice2Java插件)
    3、Ice-33.1-VC 80.msiのインストール、ディレクトリはD:\Ice-33.1 。ダウンロード先:です。http://www.zeroc.com/download.html
    二、操作手順
    1、Javaプロジェクトの確立:MyTestICE
    2、sliceフォルダを作成し、そのディレクトリの下で作成します。Printer.iceの内容は以下の通りです。
    module Printer Interface{interface Printer{void print String}
    3、Slice 2 Javaプラグインを使用する(コマンドラインも使用できます。slice 2 java Printer.ice)
    プロジェクト「MyTestICE」――Slice 2 Java――Add Slice 2 Java builderを右クリックして、自動的にgeneratedフォルダの下のjavaファイルを生成します。
    ICE实例(Eclipse与Slice2Java插件)
     
     
    4、servant類のPrinterIを作成する
    ICE实例(Eclipse与Slice2Java插件)

    View Code
     1 package printer;
    
     2 
    
     3 import Ice.Current;
    
     4 import PrinterInterface._PrinterDisp;
    
     5 
    
     6 @SuppressWarnings("serial")
    
     7 public class PrinterI extends _PrinterDisp
    
     8 {
    
     9     
    
    10     public void shutdown(Current arg0)
    
    11     {
    
    12         
    
    13     }
    
    14     
    
    15     public void writeMessage(String arg0, int arg1, Current arg2)
    
    16     {
    
    17         
    
    18     }
    
    19     
    
    20     public void printString(String s, Ice.Current current)
    
    21     {
    
    22         System.out.println("The string is : "+s);
    
    23     }
    
    24     
    
    25 }
     
    5、ICEのserverを作成する
    ICE实例(Eclipse与Slice2Java插件)

    View Code
     1 package server;
    
     2 
    
     3 import printer.PrinterI;
    
     4 
    
     5 public class Server
    
     6 {
    
     7     public static void main(String[] args)
    
     8     {
    
     9         int status = 0;
    
    10         Ice.Communicator ic = null;
    
    11         try
    
    12         {
    
    13             ic = Ice.Util.initialize(args);
    
    14             Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints(
    
    15                 "SimplePrinterAdapter", "default -p 2888");
    
    16             Ice.Object object = new PrinterI();
    
    17             adapter.add(object, Ice.Util.stringToIdentity("SimplePrinter"));
    
    18             adapter.activate();
    
    19             ic.waitForShutdown();
    
    20         }
    
    21         catch (Ice.LocalException e)
    
    22         {
    
    23             e.printStackTrace();
    
    24             status = 1;
    
    25         }
    
    26         catch (Exception e)
    
    27         {
    
    28             System.err.println(e.getMessage());
    
    29             status = 1;
    
    30         }
    
    31         finally
    
    32         {
    
    33             if (ic != null)
    
    34                 ic.destroy();
    
    35         }
    
    36         System.exit(status);
    
    37     }
    
    38 }
    6、clientクライアントを作成する
    ICE实例(Eclipse与Slice2Java插件)

    View Code
     1 package client;
    
     2 
    
     3 import PrinterInterface.PrinterPrx;
    
     4 
    
     5 public class Client
    
     6 {
    
     7     public static void main(String[] args)
    
     8     {
    
     9         int status = 0;
    
    10         Ice.Communicator ic = null;
    
    11         try
    
    12         {
    
    13             ic = Ice.Util.initialize(args);
    
    14             Ice.ObjectPrx base = ic
    
    15                 .stringToProxy("SimplePrinter:default -h 10.130.14.49 -p 2888");
    
    16             PrinterPrx printer = PrinterPrxHelper.checkedCast(base);
    
    17             if (printer == null)
    
    18                 throw new Error("Invalid proxy");
    
    19             printer.printString("Hello World!");
    
    20         }
    
    21         catch (Ice.LocalException e)
    
    22         {
    
    23             e.printStackTrace();
    
    24             status = 1;
    
    25         }
    
    26         catch (Exception e)
    
    27         {
    
    28             System.err.println(e.getMessage());
    
    29             status = 1;
    
    30         }
    
    31         finally
    
    32         {
    
    33             if (ic != null)
    
    34                 ic.destroy();
    
    35         }
    
    36         System.exit(status);
    
    37     }
    
    38 }
     
     
    7、他のマシンでServerを許可し、本機でClientを実行し、Serverを実行するマシンに出力する:The string is:Hello World!