首页 > 代码库 > ICE中间件介绍以及demo

ICE中间件介绍以及demo

一:基本开发步骤

  1. 根据业务编写slice:写任何ICE应用的第一步都要编写一个slice定义,其中包含了这个应用的所有接口
  2. 编写实现接口代码:命名规则就是接口的名字加上I
  3. 编写server端代码
  4. 写配置文件,部署服务
  5. 编写client端代码
  6. 先启动server端服务,然后启动client端服务

二:demo(安装ice服务)

  1. 定义一个接口,建立一个Printer.ice文件

      moudle Demo{

       interface Printer{

      void printString(string s);

    };

     };

    2.在DOS环境下执行slice2java Printer.ice

    3.引入Ice.jar包

    4.定义类PrinterI并且继承_PrinterDisp

     

  

  public class PrinterI extends _PrinterDisp{

    private static final long serialVersionUID = -2685822703123295227L;

    public void printString(String s, Ice.Current current){
    System.out.println(s);
    }
  }

  5.编写server端

  

public class Server{
public static void main(String[] args){
int status = 0;
Ice.Communicator ic = null;
try{
ic = Ice.Util.initialize(args);
Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
Ice.Object object = new PrinterI();
adapter.add(object,ic.stringToIdentity("SimplePrinter") ); //servant标识符,用于在客户端连接
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){
//clean up
try{
ic.destroy();
}catch(Exception e){
System.err.println(e.getMessage());
status = 1;
}
}
System.exit(status);

}
}

6.编写client端

import panda.generated.Demo.PrinterPrx;
import panda.generated.Demo.PrinterPrxHelper;
public class Client {

/**
* @param args
*/
public static void main(String[] args) {
int status = 0;
Ice.Communicator ic = null;
try{
ic = Ice.Util.initialize(args);
Ice.ObjectPrx base = ic.stringToProxy("SimplePrinter:default -p 10000");
PrinterPrx printer = PrinterPrxHelper.checkedCast(base);
if(printer == null)
throw new Error("Invalid proxy");

printer.printString("Hello World!");
}catch (Ice.LocalException e){
e.printStackTrace();
status = 1;
}catch (Exception e){
System.err.println(e.getMessage());
status = 1;
}

if( ic != null ){
//clean up
try{
ic.destroy();
}catch (Exception e){
System.err.println(e.getMessage());
status = 1;
}
}

System.exit(status);
}

}

   

ICE中间件介绍以及demo