ICE(Internet Communications Engine)是一个中间件平台。作为一个高性能的互联网通信平台,ICE包含了很多分层的服务和插件(Plug-ins),并且简单、高效和强大。
ICE当前支持C++、Java、C#、Visual Basic、Python和PHP编程语言,并支持在多种操作系统上运行。更多的操作系统和编程语言将会在以后的发布中支持。
1:下载安装包:http://www.zeroc.com/download.html
2:安装ice并在环境变量中配置ice的安装路径 例如:path=D:\ice\Ice-3.5.1\bin
3:如果进行生成java程序,使用slice2java,出现如下:代表安装成功。
slice2java: error: no input file
Usage: slice2java [options] slice-files...
Options:
-h, --help Show this message.
-v, --version Display the Ice version.
-DNAME Define NAME as 1.
-DNAME=DEF Define NAME as DEF.
-UNAME Remove any definition for NAME.
-IDIR Put DIR in the include file search path.
-E Print preprocessor output on stdout.
--output-dir DIR Create files in the directory DIR.
--tie Generate TIE classes.
--impl Generate sample implementations.
--impl-tie Generate sample TIE implementations.
--depend Generate Makefile dependencies.
--depend-xml Generate dependencies in XML format.
--list-generated Emit list of generated files in XML format.
-d, --debug Print debug messages.
--ice Permit `Ice‘ prefix (for building Ice sourc
--underscore Permit underscores in Slice identifiers.
--checksum CLASS Generate checksums for Slice definitions in
--stream Generate marshaling support for public stre
--meta META Define global metadata directive META.
4:使用ice写一个脚本
module demo {
interface Printer {
void printString(string s);
};
};
文件名为printer.ice
5:转到printer.ice所在目录,使用命令slice2java printer.ice
生成的文件copy到你的编辑器中。
6:引入Ice.jar到你的项目下面,使用你的ice安装时lib下面的Ice.jar。
7:编写服务端和客户端程序
服务端:
package servant; import Ice.Current; import demo._PrinterDisp; public class PrinterI extends _PrinterDisp{ @Override public void printString(String s, Current __current) { System.out.println(s); } }
package server; import servant.PrinterI; 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, Ice.Util.stringToIdentity("SimplePrinter")); adapter.activate(); ic.waitForShutdown(); }catch(Ice.LocalException e){ e.printStackTrace(); status=1; }catch(Exception e){ System.err.println(e.getMessage()); status=1; }finally{ if(ic!=null){ ic.destroy(); } System.exit(status); } } }
客户端:
package client; import demo.PrinterPrx; import demo.PrinterPrxHelper; public class Client { public static void main(String[] args) { int status=0; Ice.Communicator ic = null; try{ ic = Ice.Util.initialize(); Ice.ObjectPrx base = ic.stringToProxy("SimplePrinter:default -p 10000"); PrinterPrx printer = PrinterPrxHelper.checkedCast(base); if(printer==null){ throw new Error("null proxy"); } printer.printString("hello world!"); }catch(Ice.LocalException e){ e.printStackTrace(); status=1; }catch(Exception e){ System.err.println(e.getMessage()); status=1; }finally{ if(ic!=null){ ic.destroy(); } System.exit(status); } } }
8:先执行服务端,后执行客户端。互相通信。