thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bryan Buchanan <bry...@webbtide.com>
Subject TThreadPoolServer
Date Sun, 28 May 2017 09:09:25 GMT
If a client that connects to a TThreadPoolServer goes away, when (if ever)
are threads returned to the pool.

Note that in the example I've set the client socket timeout to zero so it
wait forever for a server response. If I press ^C on the client while the
server process is waiting for an input, the server process will never
"finish".


test.thrift
-------------
namespace java com.webbtide.shared.thrift

service TestApplication {

    string doSomething()

}

Client: Test.java
------------------------
public class Test {

    public static final int SOCKET_TIMEOUT = 0;

    public Test() {

        try {

            TTransport transport = new TSocket("192.168.5.1", 1200,
SOCKET_TIMEOUT);
            transport.open();

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

            String s = "";
            do {
                s = client.doSomething();
                System.err.println(s);
            } while (!s.equalsIgnoreCase("quit"));

        } catch (TException ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {

        new Test();
    }

}

Server implementation:

public class TestApplicationRemoteImpl implements TestApplication.Iface {

    TestApplicationRemoteImpl() {

        super();

    }

    @Override
    public String doSomething() throws TException {

        System.err.println("[TestApplicationRemoteImpl.doSomething] start");

        Console console = System.console();
        if (console == null) {
            return "quit";
        }

        System.out.print("Enter something: ");
        String s = console.readLine();

        System.err.println("[TestApplicationRemoteImpl.doSomething]
finish");

        return s;
    }
}

Server:

public class TestApplicationRemoteServer {

    public static TestApplicationRemoteImpl handler;
    public static TestApplication.Processor processor;

    public static void main(String[] args) {

        BasicConfigurator.configure();

        try {
            handler = new TestApplicationRemoteImpl();
            processor = new TestApplication.Processor(handler);

            Runnable simple = () -> {
                simple(processor, 1200);
            };
            new Thread(simple).start();
            System.err.println("[TestApplicationRemoteServer] started");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void simple(TestApplication.Processor processor, int
port) {
        try {

            TServerTransport serverTransport = new TServerSocket(port);

            // Use this for a multithreaded server
            TServer server = new TThreadPoolServer(new
TThreadPoolServer.Args(serverTransport).processor(processor));
            System.out.println("Starting the server...");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message