(THRIFT-5) A TNonblockingServers (single-threaded and thread-pool) for Java
This patch adds two Thrift servers for Java that both use non-blocking I/O
to avoid locking up worker threads for idle connections. The two classes are
- TNonblockingServer, which supports single-threaded serving
- THsHaServer, which performs I/O in one thread and method invocations in
a configurable thread pool.
To support these servers, TNonblockingServerSocket and TNonblockingSocket
have been added.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@673550 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/java/src/TestNonblockingServer.java b/test/java/src/TestNonblockingServer.java
new file mode 100644
index 0000000..450d67a
--- /dev/null
+++ b/test/java/src/TestNonblockingServer.java
@@ -0,0 +1,70 @@
+package com.facebook.thrift.test;
+
+import com.facebook.thrift.TException;
+import com.facebook.thrift.protocol.TBinaryProtocol;
+import com.facebook.thrift.protocol.TProtocol;
+import com.facebook.thrift.protocol.TProtocolFactory;
+import com.facebook.thrift.server.TServer;
+import com.facebook.thrift.server.TSimpleServer;
+import com.facebook.thrift.server.TNonblockingServer;
+import com.facebook.thrift.server.THsHaServer;
+import com.facebook.thrift.transport.TNonblockingServerSocket;
+import com.facebook.thrift.transport.TNonblockingServerTransport;
+import com.facebook.thrift.transport.TFramedTransport;
+
+// Generated code
+import thrift.test.*;
+
+import java.net.ServerSocket;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.HashSet;
+
+
+public class TestNonblockingServer extends TestServer {
+ public static void main(String [] args) {
+ try {
+ int port = 9090;
+ boolean hsha = false;
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("-p")) {
+ port = Integer.valueOf(args[i++]);
+ } else if (args[i].equals("-hsha")) {
+ hsha = true;
+ }
+ }
+
+ // Processor
+ TestHandler testHandler =
+ new TestHandler();
+ ThriftTest.Processor testProcessor =
+ new ThriftTest.Processor(testHandler);
+
+ // Transport
+ TNonblockingServerSocket tServerSocket =
+ new TNonblockingServerSocket(port);
+
+ TServer serverEngine;
+
+ if (hsha) {
+ // HsHa Server
+ serverEngine = new THsHaServer(testProcessor, tServerSocket);
+ } else {
+ // Nonblocking Server
+ serverEngine = new TNonblockingServer(testProcessor, tServerSocket);
+ }
+
+ // Run it
+ System.out.println("Starting the server on port " + port + "...");
+ serverEngine.serve();
+
+ } catch (Exception x) {
+ x.printStackTrace();
+ }
+ System.out.println("done.");
+ }
+}