Apache Thrift:从Java客户端到Python Tornado Server的通信

时间:2015-04-07 06:02:46

标签: thrift thrift-protocol

我有一个相当简单的Thrift IDL(分成两个文件,如图所示):

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 Tornado Server和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的参数总是空列表的默认参数和&#34; undefined&#34;串。无论我从Java客户端传递的数据是什么,都不会出于某种原因到达服务器。

可能出现什么问题?

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

1 个答案:

答案 0 :(得分:0)

关于默认参数存在一些已知问题,许多语言仍然根本没有实现。因此,最让我困惑的是你总是得到默认值......?

建议将args转换为单个struct,并将其作为唯一参数传递。 struct可能有默认设置或使用optional字段,因为它最符合您的用例。