【问题标题】:Using Thrift with Java, org.apache.thrift.TApplicationException unknown result将 Thrift 与 Java 一起使用,org.apache.thrift.TApplicationException 未知结果
【发布时间】:2014-04-20 06:04:55
【问题描述】:

我正在尝试用Thrift 编写RPC,客户端似乎与服务器通信正常,服务器创建一个列表以返回给客户端(正确格式)。但是当我收到此错误时,客户端不知何故无法识别数据包:

org.apache.thrift.TApplicationException: getEntityByIP failed: unknown result

这是我的 thrift 文件的样子:

struct EntityLookupMessage{
1: list<i32> entityIDs;
}
service EntityJoinService {
   list<i32> getEntityByIP(1:string IPval,2:i32 date);  
}

而ServerImpl是如下方法:

public List<Integer> getEntityByIP(String IPval, int date) throws TException {
    try{
        System.out.println("Checking..."+IPval);
        List<Integer> response=EntityJoinStandalone.getEntityByIP(entityLookup,IPval, date);
        System.out.println(response);
        return response;
    }finally{
        // TODO Auto-generated method stub
    return null
    }

这样被客户端调用:

List<Integer> entity = client.getEntityByIP(IPval, date); 

任何想法为什么会这样?

【问题讨论】:

    标签: java rpc thrift thrift-protocol


    【解决方案1】:

    原因

    Thrift by design 不允许空结果。这是生成的recv_Xxx()函数的代码:

    public List<Integer> recv_getEntityByIP() throws org.apache.thrift.TException
    {
      getEntityByIP_result result = new getEntityByIP_result();
      receiveBase(result, "getEntityByIP");
      if (result.isSetSuccess()) {
        return result.success;
      }
      throw new org.apache.thrift.TApplicationException(  
            org.apache.thrift.TApplicationException.MISSING_RESULT, 
            "getEntityByIP failed: unknown result");
    }
    

    你必须返回一个有效的结果,即...

    • 有效列表,可以为空,但不能为null
    • 服务器上抛出异常

    解决方案

    finally 子句中删除return null

    最佳实践

    将结果放入一个对象中,类似于您对 args 所做的:

    struct EntityByIP_result {
      1: list<i32> data;
    }
    

    这样您还留有未来进一步改进的空间,您可以随时向struct 添加新字段。

    【讨论】:

    • 但列表不为空。我在返回之前打印它,它不为空。它是 [1002]。
    • 难怪。你认为 finally 分支会发生什么?在您返回之前,您 print() 的内容并不重要。真正重要的是你return,最后(双关语)。
    • 啊,好吧,太生疏了,我的异常在 Java 中捕获。
    猜你喜欢
    • 2020-09-20
    • 2012-10-04
    • 2017-07-31
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多