2015-04-07 69 views
0

我有一个非常简单节俭IDL(分为两个文件如图所示):阿帕奇节俭:从Java客户端的通信到Python龙卷风服务器

core.thrift

namespace cpp MyProduct.Core 
namespace java com.mycompany.myproduct.core 
namespace py myproduct.core 

/** 
* Struct used to indicate a location referenced by geodetic coordinates. 
*/ 
struct GeoPoint{ 
    /** 
    * Latitude for this point 
    */ 
    1: required double latitude; 

    /** 
    * Longitude for this point 
    */ 
    2: required double longitude; 

    /** 
    * Elevation for this point 
    */ 
    3: required double elevation; 
} 

processing.thrift

include "core.thrift" 

namespace cpp MyProduct.Processing 
namespace java com.mycompany.myproduct.processing 
namespace py myproduct.processing 

/** 
* MyProduct processing services 
*/ 
service PointsQuery{ 
    /** 
    * Returns elevation of a list of input (geodetic) points 
    * The elevation attribute in input GeoPoints are ignored 
    */ 
    list<double> getElevations(1: required list<core.GeoPoint> inputPoints = [], 2: required string layername = "undefined"); 
} 

我使用Python的龙卷风服务器和Java客户端,为此,代码如下:

Python的服务器:

class PointQueryHandler(object): 
    def __init__(self): 
     self.log = {} 

    def getElevations(self, inputPoints, layerName, callback=None):   
     elevation_list = [] 
     // ...implementation here to fill elevation list with doubles... 
     if callback: 
      callback(elevation_list) 
     else: 
      return elevation_list 

def main(): 
    handler = PointQueryHandler() 
    processor = PointsQuery.Processor(handler) 
    factory = TBinaryProtocol.TBinaryProtocolFactory() 
    server = TTornado.TTornadoServer(processor, factory) 

    print "Starting the server..." 
    server.bind(9090) 
    server.start(1) 
    ioloop.IOLoop.instance().start() 

if __name__ == "__main__": 
    main() 

Java客户端:

public class QueryPointsTest { 
    public static void main(String [] args) { 
     try { 
      TTransport transport; 

      transport = new TSocket("localhost", 9090); 
      transport.open(); 

      TProtocol protocol = new TBinaryProtocol(transport); 
      PointsQuery.Client client = new PointsQuery.Client(protocol); 

      perform(client); 

      transport.close(); 
     } catch (TTransportException x) { 
      System.out.println("Unable to connect to service provider: " + "localhost" + 9090); 
     } catch (TException err){ 
      System.out.println("Error when accessing remote service: "); 
      err.printStackTrace(); 
     } 
    } 

    private static void perform(PointsQuery.Client client) throws TException 
    { 
     List<GeoPoint> pointList = new ArrayList<GeoPoint>(); 

     GeoPoint pt1 = new GeoPoint(74.53951, 34.36709, 0.0); 
     GeoPoint pt2 = new GeoPoint(74.52242,34.35413, 0.0); 
     GeoPoint pt3 = new GeoPoint(74.51398,34.41069, 0.0); 
     GeoPoint pt4 = new GeoPoint(83, 39.36709, 0.0); 

     pointList.add(pt1); 
     pointList.add(pt2); 
     pointList.add(pt3); 
     pointList.add(pt4); 

     List<Double> result = client.getElevations(pointList, "dummylayername"); 
     System.out.println("Done"); 
    } 
} 

客户端成功连接到服务器和Python的PointQueryHandler.getElevations函数被调用。但是,问题是PointQueryHandler.getElevations的参数始终是空列表和“未定义”字符串的默认参数。无论从Java客户端传出的数据是否由于某种原因都未到达服务器。

可能会出现什么问题?

(Thrift的版本:0.9.2,2.7.5的Python,JDK 1.7.0_45,平台:Windows 7的64位)

+0

嗨。我看到你问了这个问题已经有一段时间了,但是..我有同样的问题,并想知道你是否有这个运气。如果你还记得当然... – melis

回答

0

大约有默认参数的一些已知的问题,很多的语言仍然没有完全实现这一点。因此,最让我感到困惑的是你总是得到默认值......?

建议将参数转换为您传递的唯一参数struct。那struct可能有默认值或使用optional字段,因为你喜欢它,因为它最符合你的用例。