如何在Java中为异步服务器和客户端使用TBinaryProtocol和TFramedTransport?我从网上获得的很多例子都失败了,文档也不是很好。任何具体例子的帮助都会很棒!
public class NonblockingServer {
private void start() {
try {
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TThreadPoolServer.Args serverArgs = newTThreadPoolServer.Args(serverTransport);
serverArgs.processor(processor);
serverArgs.protocolFactory(protocolFactory);
TServer server = new TThreadPoolServer(serverArgs)
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NonblockingServer srv = new NonblockingServer();
srv.start();
}
}
AsyncClient
public class AsyncClient {
private void invoke() {
try {
ArithmeticService.AsyncClient client = new ArithmeticService.
AsyncClient(new TBinaryProtocol.Factory(), new TAsyncClientManager(),
new TNonblockingSocket("localhost", 7911));
client.add(200, 400, new AddMethodCallback());
client = new ArithmeticService.
AsyncClient(new TBinaryProtocol.Factory(), new TAsyncClientManager(),
new TNonblockingSocket("localhost", 7911));
client.multiply(20, 50, new MultiplyMethodCallback());
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AsyncClient c = new AsyncClient();
c.invoke();
}
我收到以下异常错误:
java.io.IOException: Read call frame size failed
at org.apache.thrift.async.TAsyncMethodCall.doReadingResponseSize(TAsyncMethodCall.java:234)
at org.apache.thrift.async.TAsyncMethodCall.transition(TAsyncMethodCall.java:192)
at org.apache.thrift.async.TAsyncClientManager$SelectThread.transitionMethods(TAsyncClientManager.java:143)
at org.apache.thrift.async.TAsyncClientManager$SelectThread.run(TAsyncClientManager.java:113)
错误似乎发生在Thrift源代码
中 private void doReadingResponseSize() throws IOException {
if (transport.read(sizeBuffer) < 0) {
throw new IOException("Read call frame size failed");
}
if (sizeBuffer.remaining() == 0) {
state = State.READING_RESPONSE_BODY;
frameBuffer = ByteBuffer.allocate(TFramedTransport.decodeFrameSize(sizeBufferArray));
}
}