JAVA调用ICE接口实践

最近刚开发了一个需求,使用ICE调用服务器端的接口,过程如下:

1 首先是准备环境,通过下载得到安装包,这次我用的是3.1版本(目前最新是3.4)

2 接着使用bin目录下slice2java将得到的ice文件转化为Java类,命令为slice2java    ****.ice

3 将lib目录下Ice.jar引入Java工程内,开始编写客户端代码

 

 

public abstract class AbstractIceAgent {

    protected Logger         logger           = LoggerFactory.getLogger(AbstractIceAgent.class.getName());

    private static final int DEFAULT_TIME_OUT = 2000;

    String                   identId;

    public AbstractIceAgent(){
    }

    public AbstractIceAgent(String identId){
        this.identId = identId;
    }

    public String getIdentId() {
        return identId;
    }

    public void setIdentId(String identId) {
        this.identId = identId;
    }

    public Object executeProxy(Object bean) {
        Communicator communicator = Ice.Util.initialize();
        Object returnValue = null;
        try {
            ObjectPrx objPrx = communicator.stringToProxy(identId);
            objPrx.ice_timeout(DEFAULT_TIME_OUT);
            returnValue = methodAction(objPrx, bean);
        } catch (Exception e) {
            logger.error("execute proxy cause error:" + e);
            throw new RuntimeException(e);
        } finally {
            if (communicator != null) {
                try {
                    communicator.destroy();
                    communicator = null;
                } catch (Exception e) {
                    // do nothing
                }
            }
        }
        return returnValue;
    }

    /**
     * ice接口方法的定义
     * 
     * @param base
     */
    abstract Object methodAction(ObjectPrx base, Object bean);

}

 

 

 这是一个虚基类,子类只需覆盖methodAction即可。

 

 

 

 

package  ****;

public interface AIMFriendBO {

    int[] isFriend(String memberId, String[] friendIds);
}

 

 这是本次需要实现的接口。

 

 

public class AIMFriendAgent extends AbstractIceAgent implements AIMFriendBO {

    // private static final String ICE_INTERFACE = "ADPushAgentId:default -h 10.2.225.50 -p 11001";
    // private static BrmmsRemindAgent brmmsIceAgent = new BrmmsRemindAgent(ICE_INTERFACE);

    protected Logger logger = LoggerFactory.getLogger(AIMFriendAgent.class.getName());
    
	private String              strCaller;
    private String              strServiceType;
    
	public void setStrCaller(String strCaller) {
		this.strCaller = strCaller;
	}

	public void setStrServiceType(String strServiceType) {
		this.strServiceType = strServiceType;
	}

    public AIMFriendAgent(){
    };

    public AIMFriendAgent(String identId){
        super(identId);
    }


    @Override
    Object methodAction(ObjectPrx base, Object bean) {
    	WWContactInterfacePrx ctWebInterfacePrx = WWContactInterfacePrxHelper.checkedCast(base);
        AliIMContact contact = (AliIMContact) bean;
        IntSeqHolder isFriendSeq=new IntSeqHolder();
        StringHolder retMsgHolder=new StringHolder();
        
        int result  = ctWebInterfacePrx.IsFriend(strCaller, strServiceType, contact.getMemberId(), contact.getContactIds(), isFriendSeq, retMsgHolder);
        
        // 10000 means ok normally
        if(result==10000){
        	return isFriendSeq.value;
        }else{
        	logger.error("check contact error in AIMFriendAgent , cause :" + retMsgHolder.value);
        	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[]) this.executeProxy(new AliIMContact(memberId,friendIds));
        return returnValue;
        
	}
	


}

 

这是最后的实现,使用时只需在spring的配置文件中配好需要的参数即可直接使用。  

参考文献:http://blog.csdn.net/linkyou/archive/2009/02/02/3858233.aspx

 

 

 

你可能感兴趣的:(java,spring,.net,bean,Blog)