熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JAVA調用ICE接口實踐

2013-11-23 19:20:53  來源: Java核心技術 

  最近剛開發了一個需求使用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的配置文件中配好需要的參數即可直接使用


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26695.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.