前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Microsoft/thrifty:RPC方法返回NULL的异常处理

Microsoft/thrifty:RPC方法返回NULL的异常处理

作者头像
10km
发布2019-05-25 20:53:03
1.3K0
发布2019-05-25 20:53:03
举报
文章被收录于专栏:10km的专栏10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433469

我们知道:thrift框架是不允许返回值为null的,如果返回值为null,client端会抛出异常,我在之前用facebook/swift框架时就遇到了这个问题,这是当时解决问题的记录《thrift:返回null的解决办法》,现在使用Microsoft/thrifty框架实现的客户端同样也存在这个问题。

下面是thifty-compiler生成的client端存根代码的receive方法的部分片段:

    @Override
    protected PersonBean receive(Protocol protocol, MessageMetadata metadata) throws Exception {
      PersonBean result = null;
      .......
      if (result != null) {
        return result;
      } else if (ex1 != null) {
        throw ex1;
      } else {
        throw new ThriftException(ThriftException.Kind.MISSING_RESULT, "Missing result");
      }
    }
  }

可以看到,返回结果为null时,会抛出类型为MISSING_RESULTThriftException异常。

知道了原因,解决问题的方法有了:

		/**
		* 当前调用的回调函数,由当前接口方法设置
		*/
		final ServiceMethodCallback<Integer> callback = null;
		/** 创建一个侦听器对象,用于检测socket关闭状态 */
		final AsyncClientBase.Listener closeListener = new AsyncClientBase.Listener(){
		   @Override
		   public void onTransportClosed() {
		   }
		
		   @Override
		   public void onError(Throwable error) {
		       // 如果关闭时有异常,则将异常转给callback对象,
		       // 当方法返回值为null时抛出的ThriftException异常会在这里被拦截发给callback对象
		       callback.onError(error);
		   }        
		};
		
        SocketTransport transport = new SocketTransport.Builder(host,port).build();
        transport.connect();
        Protocol protocol = new BinaryProtocol(transport);
        /* force set private field 'strictWrite' to true */
        Field field = BinaryProtocol.class.getDeclaredField("strictWrite");
        field.setAccessible(true);
        field.set(protocol, true);
        // 创建client端接口实例
        final mypackage.MyPrjClient service = new mypackage.MyPrjClient(protocol,closeListener);
        
        // 创建callback对象
		callback = new ServiceMethodCallback<Integer>(){
	        @Override
	        public void onSuccess(Integer result) {
	            // do something       
	            try {
	            	// 关闭连接
	                service.close();
	            } catch (IOException e) {
	            }
	        }
	
	        @Override 
	        public void onError(Throwable error) {
	        	// 对象ThriftException异常,判断类型是否为MISSING_RESULT,是则调用onSuccess正常返回null       
	            if(error instanceof ThriftException ){
	                if(((ThriftException)error).kind == ThriftException.Kind.MISSING_RESULT  ){
	                    this.onSuccess(null);
	                }
	            }
	            // do something
	            try {
	                // 关闭连接
	                service.close();
	            } catch (IOException e) {
	            }
	        }};
	    // 执行异步接口调用
        service.callMethod(123,callback);        
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档