最近刚开发了一个需求使用ICE调用服务器端的接口过程如下
首先是准备环境通过下载得到安装包这次我用的是版本(目前最新是)
接着使用bin目录下slicejava将得到的ice文件转化为Java类命令为slicejava ****ice
将lib目录下Icejar引入Java工程内开始编写客户端代码
Java代码
public abstract class AbstractIceAgent {
protected Logger logger = LoggerFactorygetLogger(AbstractIceAgentclassgetName());
private static final int DEFAULT_TIME_OUT = ;
String identId;
public AbstractIceAgent(){
}
public AbstractIceAgent(String identId){
thisidentId = identId;
}
public String getIdentId() {
return identId;
}
public void setIdentId(String identId) {
thisidentId = identId;
}
public Object executeProxy(Object bean) {
Communicator communicator = IceUtilinitialize();
Object returnValue = null;
try {
ObjectPrx objPrx = communicatorstringToProxy(identId);
objPrxice_timeout(DEFAULT_TIME_OUT);
returnValue = methodAction(objPrx bean);
} catch (Exception e) {
loggererror(execute proxy cause error: + e);
throw new RuntimeException(e);
} finally {
if (communicator != null) {
try {
communicatordestroy();
communicator = null;
} catch (Exception e) {
// do nothing
}
}
}
return returnValue;
}
/**
* ice接口方法的定义
*
* @param base
*/
abstract Object methodAction(ObjectPrx base Object bean);
}
这是一个虚基类子类只需覆盖methodAction即可
Java代码
package comalibabachinabrmmsremind;
public interface AIMFriendBO {
int[] isFriend(String memberId String[] friendIds);
}
这是本次需要实现的接口
Java代码
public class AIMFriendAgent extends AbstractIceAgent implements AIMFriendBO {
// private static final String ICE_INTERFACE = ADPushAgentId:default h p ;
// private static BrmmsRemindAgent brmmsIceAgent = new BrmmsRemindAgent(ICE_INTERFACE);
protected Logger logger = LoggerFactorygetLogger(AIMFriendAgentclassgetName());
private String strCaller;
private String strServiceType;
public void setStrCaller(String strCaller) {
thisstrCaller = strCaller;
}
public void setStrServiceType(String strServiceType) {
thisstrServiceType = strServiceType;
}
public AIMFriendAgent(){
};
public AIMFriendAgent(String identId){
super(identId);
}
@Override
Object methodAction(ObjectPrx base Object bean) {
WWContactInterfacePrx ctWebInterfacePrx = WWContactInterfacePrxHelpercheckedCast(base);
AliIMContact contact = (AliIMContact) bean;
IntSeqHolder isFriendSeq=new IntSeqHolder();
StringHolder retMsgHolder=new StringHolder();
int result = ctWebInterfacePrxIsFriend(strCaller strServiceType contactgetMemberId() contactgetContactIds() isFriendSeq retMsgHolder);
// means ok normally
if(result==){
return isFriendSeqvalue;
}else{
loggererror(check contact error in AIMFriendAgent cause : + retMsgHoldervalue);
return null;
}
}
public int[] isFriend(String memberId String[] friendIds) {
if (memberId == null||friendIds==null) {
throw new RuntimeException(ice contact relation check parameters are empty);
}
int[] returnValue=(int[]) thisexecuteProxy(new AliIMContact(memberIdfriendIds));
return returnValue;
}
}
这是最后的实现使用时只需在spring的配置文件中配好需要的参数即可直接使用