ZeroC ICE之旅------java

关于Ice请见,
ICE之轻量级分布式通讯中间件
http://masterkey.javaeye.com/blog/182954
ZeroC ICE之旅------C++
http://masterkey.javaeye.com/blog/183307
ZeroC ICE之旅------Slice
http://masterkey.javaeye.com/blog/184064
ZeroC ICE之旅------多语言互通互联
http://masterkey.javaeye.com/blog/183742
ZeroC ICE之旅------集群和容错
http://masterkey.javaeye.com/blog/185081
更多ICE文章,请关注:
Titan的天空
http://masterkey.javaeye.com

工欲善其事,必先利其器,我们首先从www.zero.com,下载最新安装包;
btw:

目前最新的v3.3
http://www.zeroc.com/download_beta.html
最新稳定版本:
http://www.zeroc.com/download.html


由于我自己的平台是CentOS release 4.4 (Final),java version "1.6.0_01"
所以下载的是:
http://www.zeroc.com/download/Ice/3.3/Ice-3.3b-rhel4-i386-rpm.tar.gz

解开之后:
-rw-r--r-- 1 506 users 1130231 4月 1 10:39 db46-4.6.21-2ice.rhel4.i386.rpm
-rw-r--r-- 1 506 users 51459 4月 1 10:39 db46-devel-4.6.21-2ice.rhel4.i386.rpm
-rw-r--r-- 1 506 users 810562 4月 1 10:40 db46-java-4.6.21-2ice.rhel4.i386.rpm
-rw-r--r-- 1 506 users 72125 4月 1 10:40 db46-utils-4.6.21-2ice.rhel4.i386.rpm
-rw-r--r-- 1 506 users 84281 4月 1 10:15 ice-3.3b-1.rhel4.noarch.rpm
-rw-r--r-- 1 506 users 450359 4月 1 10:15 ice-c++-devel-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 2823701 4月 1 10:16 ice-java-3.3b-1.rhel4.noarch.rpm
-rw-r--r-- 1 506 users 263085 4月 1 10:16 ice-java-devel-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 3439061 4月 1 10:17 ice-libs-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 162092 4月 1 10:19 ice-php-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 739055 4月 1 10:19 ice-python-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 32913 4月 1 10:19 ice-python-devel-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 223577 4月 1 10:19 ice-ruby-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 21406 4月 1 10:19 ice-ruby-devel-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 3657167 4月 1 10:20 ice-servers-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 4350193 4月 1 10:24 ice-utils-3.3b-1.rhel4.i386.rpm
-rw-r--r-- 1 506 users 74856 4月 1 10:40 mcpp-devel-2.6.4-1ice.rhel4.i386.rpm
由于需要c和java的包:
所以我们仅安装:
rmp -ivh ice-3.3b-1.rhel4.noarch.rpm
rmp -ivh ice-3.3b-1.rhel4.noarch.rpm
rpm -ivh ice-3.3b-1.rhel4.noarch.rpm
rpm -ivh ice-libs-3.3b-1.rhel4.i386.rpm
rpm -ivh ice-utils-3.3b-1.rhel4.i386.rpm
rpm -ivh mcpp-devel-2.6.4-1ice.rhel4.i386.rpm
rpm -ivh ice-servers-3.3b-1.rhel4.i386.rpm
rpm -ivh ice-libs-3.3b-1.rhel4.i386.rpm
rpm -ivh ice-c++-devel-3.3b-1.rhel4.i386.rpm
rpm -ivh ice-java-3.3b-1.rhel4.noarch.rpm
rpm -ivh ice-java-devel-3.3b-1.rhel4.i386.rpm

安装之后的Ice相关路径:
slice2cpp, slice2java在/usr/bin/下
Ice.jar 存储于 /usr/share/java/下
相关的Ice的库存储于/usr/lib下.

一切就绪,我们开始Ice之旅的Slice地带:
首先,我们建立一个demo.ice的文件:

Java代码 复制代码
  1. module Demo{  
  2. interface test{  
  3.  string   execute(string mth,string cmd);  
  4. };  
  5. };  
module Demo{interface test{ string   execute(string mth,string cmd);};};


注意,后两个"}"一定要包含";",否则 slice2java就会过不去,赫赫
执行:
slice2java demo.ice

会在当前目录产生一个Demo目录,目录下自动生成:
-rw-r--r-- 1 root root 2316 4月 15 17:01 _testDelD.java
-rw-r--r-- 1 root root 560 4月 15 17:01 _testDel.java
-rw-r--r-- 1 root root 1929 4月 15 17:01 _testDelM.java
-rw-r--r-- 1 root root 4177 4月 15 17:01 _testDisp.java
-rw-r--r-- 1 root root 1070 4月 15 17:01 testHolder.java
-rw-r--r-- 1 root root 488 4月 15 17:01 test.java
-rw-r--r-- 1 root root 481 4月 15 17:01 _testOperations.java
-rw-r--r-- 1 root root 460 4月 15 17:01 _testOperationsNC.java
-rw-r--r-- 1 root root 5418 4月 15 17:01 testPrxHelper.java
-rw-r--r-- 1 root root 569 4月 15 17:01 testPrxHolder.java
-rw-r--r-- 1 root root 567 4月 15 17:01 testPrx.java
到目前为止,demo.ice所以Ice接口部分的定义以及相关依赖都已经自动生成.

我们要实现自己的execute方法,覆盖testPrx.java的同名方法:
Java代码 复制代码
  1. //TestImp.java  
  2. package Demo;  
  3.   
  4. import Ice.Current;  
  5.   
  6.   
  7. public class TestImp extends _testDisp{  
  8.   
  9.     public String execute(String mth, String cmd, Current __current) {  
  10.         // TODO Auto-generated method stub  
  11.         return mth+cmd;  
  12.     }  
  13.   
  14.   
  15.   
  16. }  
//TestImp.javapackage Demo;import Ice.Current;public class TestImp extends _testDisp{	public String execute(String mth, String cmd, Current __current) {		// TODO Auto-generated method stub		return mth+cmd;	}}
看到了,就是这么简单,仅仅覆盖_testDisp里面的抽象方法,实现把我们自己的实现代码填充到里面就行了.
之后,我们建立一个Server服务在10000端口进行侦听。
Java代码 复制代码
  1. //Server.java  
  2. package Demo;  
  3.   
  4. public class Server {  
  5.     public static void main(String[] args) {  
  6.         int status = 0;  
  7.         Ice.Communicator ic = null;  
  8.         try {  
  9.             ic = Ice.Util.initialize(args);  
  10.             Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints(  
  11.                     "TestAdapter""default -p 10000");  
  12.             Ice.Object object = new TestImp();  
  13.             adapter.add(object, ic.stringToIdentity("TestAdapter"));  
  14.             adapter.activate();  
  15.             ic.waitForShutdown();  
  16.         } catch (Ice.LocalException e) {  
  17.             e.printStackTrace();  
  18.             status = 1;  
  19.         } catch (Exception e) {  
  20.             System.err.println(e.getMessage());  
  21.             status = 1;  
  22.         }  
  23.         if (ic != null) {  
  24.             // Clean up  
  25.             //  
  26.             try {  
  27.                 ic.destroy();  
  28.             } catch (Exception e) {  
  29.                 System.err.println(e.getMessage());  
  30.                 status = 1;  
  31.             }  
  32.         }  
  33.         System.exit(status);  
  34.     }  
  35. }  
//Server.javapackage Demo;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(					"TestAdapter", "default -p 10000");			Ice.Object object = new TestImp();			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) {			// Clean up			//			try {				ic.destroy();			} catch (Exception e) {				System.err.println(e.getMessage());				status = 1;			}		}		System.exit(status);	}}

貌似很复杂,其实里面的很多内容都是固定格式,有些部分需要固定和约定。其中上述红色部分是修改部分。

到目前为止,我们已经完成了大部分工作,我们还需要建一个客户端来对服务端的方法进行调用。
Java代码 复制代码
  1. package Demo;  
  2.   
  3. public class Client {  
  4.     public static void main(String[] args) {  
  5.         int status = 0;  
  6.         Ice.Communicator ic = null;  
  7.         try {  
  8.             ic = Ice.Util.initialize(args);  
  9. //          Ice.ObjectPrx base = ic  
  10. //                  .stringToProxy("SimplePrinter:tcp -h 172.17.12.101 -p 10000");  
  11.             Ice.ObjectPrx base = ic  
  12.             .stringToProxy("TestAdapter:default -p 10000");  
  13.   
  14.             testPrx test = testPrxHelper.checkedCast(base);  
  15.             if (test == null)  
  16.                 throw new Error("Invalid proxy");  
  17.             System.out.println(test.execute("My first Ice ""demo"));  
  18.             //System.out.println("ok");  
  19.         } catch (Ice.LocalException e) {  
  20.             e.printStackTrace();  
  21.             status = 1;  
  22.         } catch (Exception e) {  
  23.             System.err.println(e.getMessage());  
  24.             status = 1;  
  25.         }  
  26.         if (ic != null) {  
  27.             // Clean up  
  28.             //  
  29.             try {  
  30.                 ic.destroy();  
  31.             } catch (Exception e) {  
  32.                 System.err.println(e.getMessage());  
  33.                 status = 1;  
  34.             }  
  35.         }  
  36.         System.exit(status);  
  37.     }  
  38. }  
package Demo;public class Client {	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:tcp -h 172.17.12.101 -p 10000");			Ice.ObjectPrx base = ic			.stringToProxy("TestAdapter:default -p 10000");			testPrx test = testPrxHelper.checkedCast(base);			if (test == null)				throw new Error("Invalid proxy");			System.out.println(test.execute("My first Ice ", "demo"));			//System.out.println("ok");		} 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);	}}


也貌似很复杂吧,其实不然,也很简单,同样包含很多固定格式。其中

Java代码 复制代码
  1. System.out.println(test.execute("my first Ice ""demo"));  
System.out.println(test.execute("my first Ice ", "demo"));


是我们自己调用的逻辑。

赫赫,大功告成了,现在我们开始运行Server,再运行Client看到了么?
"My first Ice demo"

得到这样的结果,基本Ice之旅的Java部分简单实例我们基本完成。

Ice的性能和稳定性远超于我们的想象,skype知道么?其部分通讯架构就是采用的Ice.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值