ICE轉摘學習

   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應用就完成了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章