Search in sources :

Example 21 with TServerSocket

use of org.apache.thrift.transport.TServerSocket in project accumulo by apache.

the class TestThrift1474 method test.

@Test
public void test() throws IOException, TException, InterruptedException {
    TServerSocket serverTransport = new TServerSocket(0);
    serverTransport.listen();
    int port = serverTransport.getServerSocket().getLocalPort();
    TestServer handler = new TestServer();
    ThriftTest.Processor<ThriftTest.Iface> processor = new ThriftTest.Processor<>(handler);
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport);
    args.stopTimeoutVal = 10;
    args.stopTimeoutUnit = TimeUnit.MILLISECONDS;
    final TServer server = new TThreadPoolServer(args.processor(processor));
    Thread thread = new Thread() {

        @Override
        public void run() {
            server.serve();
        }
    };
    thread.start();
    while (!server.isServing()) {
        sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
    }
    TTransport transport = new TSocket("localhost", port);
    transport.open();
    TProtocol protocol = new TBinaryProtocol(transport);
    ThriftTest.Client client = new ThriftTest.Client(protocol);
    assertTrue(client.success());
    assertFalse(client.fails());
    try {
        client.throwsError();
        fail("no exception thrown");
    } catch (ThriftSecurityException ex) {
    // expected
    }
    server.stop();
    thread.join();
}
Also used : TServer(org.apache.thrift.server.TServer) ThriftSecurityException(org.apache.accumulo.core.client.impl.thrift.ThriftSecurityException) TServerSocket(org.apache.thrift.transport.TServerSocket) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) ThriftTest(org.apache.accumulo.core.client.impl.thrift.ThriftTest) TTransport(org.apache.thrift.transport.TTransport) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) TSocket(org.apache.thrift.transport.TSocket) Test(org.junit.Test) ThriftTest(org.apache.accumulo.core.client.impl.thrift.ThriftTest)

Example 22 with TServerSocket

use of org.apache.thrift.transport.TServerSocket in project accumulo by apache.

the class TracerTest method testThrift.

@Test
public void testThrift() throws Exception {
    TestReceiver tracer = new TestReceiver();
    org.apache.htrace.Trace.addReceiver(tracer);
    ServerSocket socket = new ServerSocket(0);
    TServerSocket transport = new TServerSocket(socket);
    transport.listen();
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(transport);
    args.processor(new Processor<Iface>(TraceWrap.service(new Service())));
    final TServer tserver = new TThreadPoolServer(args);
    Thread t = new Thread() {

        @Override
        public void run() {
            tserver.serve();
        }
    };
    t.start();
    TTransport clientTransport = new TSocket(new Socket("localhost", socket.getLocalPort()));
    TestService.Iface client = new TestService.Client(new TBinaryProtocol(clientTransport), new TBinaryProtocol(clientTransport));
    client = TraceWrap.client(client);
    assertFalse(client.checkTrace(null, "test"));
    Span start = Trace.on("start");
    assertTrue(client.checkTrace(null, "my test"));
    start.stop();
    assertNotNull(tracer.traces.get(start.traceId()));
    String[] traces = { "my test", "checkTrace", "client:checkTrace", "start" };
    assertTrue(tracer.traces.get(start.traceId()).size() == traces.length);
    for (int i = 0; i < traces.length; i++) assertEquals(traces[i], tracer.traces.get(start.traceId()).get(i).description);
    tserver.stop();
    t.join(100);
}
Also used : TServer(org.apache.thrift.server.TServer) TestService(org.apache.accumulo.tracer.thrift.TestService) TestService(org.apache.accumulo.tracer.thrift.TestService) ServerSocket(java.net.ServerSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) Span(org.apache.accumulo.core.trace.Span) TServerSocket(org.apache.thrift.transport.TServerSocket) Iface(org.apache.accumulo.tracer.thrift.TestService.Iface) Iface(org.apache.accumulo.tracer.thrift.TestService.Iface) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TTransport(org.apache.thrift.transport.TTransport) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) TSocket(org.apache.thrift.transport.TSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) TSocket(org.apache.thrift.transport.TSocket) Test(org.junit.Test)

Example 23 with TServerSocket

use of org.apache.thrift.transport.TServerSocket in project yyl_example by Relucent.

the class HelloServer method getPoolServer.

/**
 * 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求
 */
public static TServer getPoolServer(int port, HelloService.Processor<HelloServiceHandler> processor) throws TTransportException {
    TServerTransport transport = new TServerSocket(port);
    TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor));
    return server;
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) TServer(org.apache.thrift.server.TServer) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) TServerTransport(org.apache.thrift.transport.TServerTransport)

Example 24 with TServerSocket

use of org.apache.thrift.transport.TServerSocket in project hbase by apache.

the class ThriftServer method getTThreadPoolServer.

protected TServer getTThreadPoolServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws Exception {
    LOG.info("starting HBase ThreadPool Thrift server on " + inetSocketAddress.toString());
    // Thrift's implementation uses '0' as a placeholder for 'use the default.'
    int backlog = conf.getInt(BACKLOG_CONF_KEY, BACKLOG_CONF_DEAFULT);
    int readTimeout = conf.getInt(THRIFT_SERVER_SOCKET_READ_TIMEOUT_KEY, THRIFT_SERVER_SOCKET_READ_TIMEOUT_DEFAULT);
    TServerTransport serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().bindAddr(inetSocketAddress).backlog(backlog).clientTimeout(readTimeout));
    TBoundedThreadPoolServer.Args serverArgs = new TBoundedThreadPoolServer.Args(serverTransport, conf);
    serverArgs.processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
    return new TBoundedThreadPoolServer(serverArgs, metrics);
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) TServerTransport(org.apache.thrift.transport.TServerTransport)

Example 25 with TServerSocket

use of org.apache.thrift.transport.TServerSocket in project zeppelin by apache.

the class RemoteInterpreterEventServer method start.

public void start() throws IOException {
    Thread startingThread = new Thread() {

        @Override
        public void run() {
            try (TServerSocket tSocket = new TServerSocket(RemoteInterpreterUtils.findAvailablePort(portRange))) {
                port = tSocket.getServerSocket().getLocalPort();
                host = RemoteInterpreterUtils.findAvailableHostAddress();
                LOGGER.info("InterpreterEventServer is starting at {}:{}", host, port);
                RemoteInterpreterEventService.Processor<RemoteInterpreterEventServer> processor = new RemoteInterpreterEventService.Processor<>(RemoteInterpreterEventServer.this);
                thriftServer = new TThreadPoolServer(new TThreadPoolServer.Args(tSocket).processor(processor));
                thriftServer.serve();
            } catch (IOException | TTransportException e) {
                throw new RuntimeException("Fail to create TServerSocket", e);
            }
            LOGGER.info("ThriftServer-Thread finished");
        }
    };
    startingThread.start();
    long start = System.currentTimeMillis();
    while ((System.currentTimeMillis() - start) < 30 * 1000) {
        if (thriftServer != null && thriftServer.isServing()) {
            break;
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }
    if (thriftServer != null && !thriftServer.isServing()) {
        throw new IOException("Fail to start InterpreterEventServer in 30 seconds.");
    }
    LOGGER.info("RemoteInterpreterEventServer is started");
    runner = new AppendOutputRunner(listener);
    appendFuture = appendService.scheduleWithFixedDelay(runner, 0, AppendOutputRunner.BUFFER_TIME_MS, TimeUnit.MILLISECONDS);
}
Also used : RemoteInterpreterEventService(org.apache.zeppelin.interpreter.thrift.RemoteInterpreterEventService) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) TServerSocket(org.apache.thrift.transport.TServerSocket) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) AppendOutputRunner(org.apache.zeppelin.interpreter.remote.AppendOutputRunner)

Aggregations

TServerSocket (org.apache.thrift.transport.TServerSocket)49 TThreadPoolServer (org.apache.thrift.server.TThreadPoolServer)30 TServerTransport (org.apache.thrift.transport.TServerTransport)20 TServer (org.apache.thrift.server.TServer)15 InetSocketAddress (java.net.InetSocketAddress)14 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)14 TTransportException (org.apache.thrift.transport.TTransportException)13 TTransportFactory (org.apache.thrift.transport.TTransportFactory)11 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)9 ArrayList (java.util.ArrayList)7 TProtocol (org.apache.thrift.protocol.TProtocol)7 TSimpleServer (org.apache.thrift.server.TSimpleServer)7 TTransport (org.apache.thrift.transport.TTransport)7 TProcessor (org.apache.thrift.TProcessor)6 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)6 TServerEventHandler (org.apache.thrift.server.TServerEventHandler)6 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)5 ServerContext (org.apache.thrift.server.ServerContext)5