java - 使用 Apache Thrift 时出现 TTransportException 异常

标签 java sockets thrift transport

我正在使用 apache Thrift。我收到 TTransportException 异常,而我的代码看起来一切正常。这是我的 Thrift 服务器代码:

private TNonblockingServerSocket socket;
/**
 * @breif Store processor instance.
 */
private PringService.Processor processor;
/**
 * Store server instance.
 */
private TServer tServer;
/**
 *
 * @breif A handle to the unique Singleton instance.
 */
static private ThriftServer _instance = null;

/**
 * @breif The unique instance of this class.
 * @throws TTransportException
 */
static public ThriftServer getInstance() throws TTransportException {
    if (null == _instance) {
        _instance = new ThriftServer();
    }
    return _instance;
}

/**
 * @breif A Ctor for ThriftServer. Initialize all members.
 * @throws TTransportException
 */
private ThriftServer() throws TTransportException {
    socket = new TNonblockingServerSocket(Config.THRIFT_PORT);
    processor = new PringService.Processor(new Handler());
    THsHaServer.Args args = new THsHaServer.Args(socket);
    args.processor(processor);
    args.transportFactory(new TFramedTransport.Factory());
    args.inputProtocolFactory(new TBinaryProtocol.Factory());
    args.outputProtocolFactory(new TBinaryProtocol.Factory());
    tServer = new THsHaServer(args);
    /*tServer = new THsHaServer(processor, socket,
     new TFramedTransport.Factory(),
     new TFramedTransport.Factory(),
     new TBinaryProtocol.Factory(),
     new TBinaryProtocol.Factory());*/
}

/**
 * @breif main method
 * @param args the command line arguments
 * @throws TTransportException
 */
public static void main(String[] args) throws TTransportException {
    // To Run it directly from PringCore.jar, else use SmsProcessor Helper functionality
    ThriftServer server = new ThriftServer();
    server.execute(args);
}

@Override
/**
 * @breif Starts the execution.
 */
protected void execute(String[] args) {
    if (db != null) {
        db.close();
    }
    tServer.serve();
}

private static class Handler implements PringService.Iface { …… }

这是我的节俭客户:

    TTransport transport;
  try {
     transport = new TSocket("localhost", Config.THRIFT_PORT);        
     transport.open();

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

     String result = client.importPringer(2558456, true);

    System.out.println("Result String is ::"+result);
     transport.close();
  } catch (TTransportException e) {
     e.printStackTrace();
  } catch (TException e) {
     e.printStackTrace();
  }

当我运行 Thrift 服务器然后运行 ​​Thrift 客户端时,出现以下异常:

org.apache.thrift.transport.TTransportException
at org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)
at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
at org.apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:378)
at org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:297)
at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:204)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:69)

我是否在我的 thrift 服务器或客户端上使用了不匹配的传输套接字/层?还是有其他问题?

提前感谢您的指导:)

最佳答案

当您使用TNonblockingServerSocket 时,您需要在服务器端和客户端都使用TFramedTransportTNonblockingServerSocketdocumentation 对此非常明确:

To use this server, you MUST use a TFramedTransport at the outermost transport, otherwise this server will be unable to determine when a whole method call has been read off the wire. Clients must also use TFramedTransport.

因此,您的客户应如下所示:

TTransport transport;

try {
    transport = new TSocket("localhost", Config.THRIFT_PORT);        
    transport.open();

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

    String result = client.importPringer(2558456, true);

    System.out.println("Result String is ::"+result);
    transport.close();
} catch (TTransportException e) {
    e.printStackTrace();
} catch (TException e) {
    e.printStackTrace();
}

关于java - 使用 Apache Thrift 时出现 TTransportException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751845/

相关文章:

Java 初学者 : How to access a Hashmap of SortedSet entries?

java - 为 SWT Combo 设置颜色

python-3.x - Python TCP 客户端

php - 在 PHP 中创建简单的客户端/服务器 UDP 示例

java - 第一个使用 Apache Thrift 的程序——我应该在哪里定义接口(interface)?在客户端或服务器代码中

java - 如何在整个应用程序中访问 Guice 注入(inject)器

rpc - 微服务之间的通信

java - 随机化数据库行结果

java - JPA - 检索所有实体类

Java - Swing UI 独立的套接字逻辑