Search in sources :

Example 1 with TServer

use of org.apache.thrift.server.TServer in project accumulo by apache.

the class TServerUtilsTest method testStartServerUsedPortWithSearch.

@Test
public void testStartServerUsedPortWithSearch() throws Exception {
    TServer server = null;
    int[] port = findTwoFreeSequentialPorts(1024);
    // Bind to the port
    InetAddress addr = InetAddress.getByName("localhost");
    ((ConfigurationCopy) factory.getSystemConfiguration()).set(Property.TSERV_CLIENTPORT, Integer.toString(port[0]));
    ((ConfigurationCopy) factory.getSystemConfiguration()).set(Property.TSERV_PORTSEARCH, "true");
    try (ServerSocket s = new ServerSocket(port[0], 50, addr)) {
        ServerAddress address = startServer();
        assertNotNull(address);
        server = address.getServer();
        assertNotNull(server);
        assertEquals(port[1], address.getAddress().getPort());
    } finally {
        if (null != server) {
            TServerUtils.stopTServer(server);
        }
    }
}
Also used : ConfigurationCopy(org.apache.accumulo.core.conf.ConfigurationCopy) TServer(org.apache.thrift.server.TServer) ServerAddress(org.apache.accumulo.server.rpc.ServerAddress) ServerSocket(java.net.ServerSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with TServer

use of org.apache.thrift.server.TServer in project accumulo by apache.

the class TServerUtilsTest method testStartServerPortRangeFirstPortUsed.

@Test
public void testStartServerPortRangeFirstPortUsed() throws Exception {
    TServer server = null;
    InetAddress addr = InetAddress.getByName("localhost");
    int[] port = findTwoFreeSequentialPorts(1024);
    String portRange = Integer.toString(port[0]) + "-" + Integer.toString(port[1]);
    // Bind to the port
    ((ConfigurationCopy) factory.getSystemConfiguration()).set(Property.TSERV_CLIENTPORT, portRange);
    try (ServerSocket s = new ServerSocket(port[0], 50, addr)) {
        ServerAddress address = startServer();
        assertNotNull(address);
        server = address.getServer();
        assertNotNull(server);
        assertTrue(port[1] == address.getAddress().getPort());
    } finally {
        if (null != server) {
            TServerUtils.stopTServer(server);
        }
    }
}
Also used : ConfigurationCopy(org.apache.accumulo.core.conf.ConfigurationCopy) TServer(org.apache.thrift.server.TServer) ServerAddress(org.apache.accumulo.server.rpc.ServerAddress) ServerSocket(java.net.ServerSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 3 with TServer

use of org.apache.thrift.server.TServer in project mlib by myshzzx.

the class ThriftServerFactory method build.

/**
 * @return build but not start server.
 * @throws Exception
 */
@SuppressWarnings("ConstantConditions")
public TServer build() throws Exception {
    final TServer server;
    int poolSize = Runtime.getRuntime().availableProcessors() * 2;
    poolSize = Math.max(poolSize, this.serverPoolSize);
    AtomicInteger tpi = new AtomicInteger(1);
    ThreadPoolExecutor tPool = new ThreadPoolExecutor(poolSize, poolSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), r -> {
        Thread t = new Thread(r, "tServer-" + serverTPI.getAndIncrement() + "-" + tpi.getAndIncrement());
        t.setDaemon(true);
        return t;
    });
    tPool.allowCoreThreadTimeOut(true);
    if (useTLS) {
        // TLS server
        TSSLTransportFactory.TSSLTransportParameters transportParams = new TSSLTransportFactory.TSSLTransportParameters();
        transportParams.setKeyStore(ThriftServerFactory.class.getClassLoader().getResource(this.selfKeyStore).getPath(), this.selfKeyStorePw);
        transportParams.requireClientAuth(this.isRequireClientAuth);
        if (this.isRequireClientAuth)
            transportParams.setTrustStore(ThriftServerFactory.class.getClassLoader().getResource(this.trustKeyStore).getPath(), this.trustKeyStorePw);
        TServerSocket serverTransport = TSSLTransportFactory.getServerSocket(this.serverPort, this.clientTimeout, InetAddress.getByName(this.serverHost), transportParams);
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(this.processor).protocolFactory(new TCompactProtocol.Factory()).executorService(tPool));
    } else {
        // Non security Server
        TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress(this.serverHost, this.serverPort), this.clientTimeout);
        server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(this.processor).protocolFactory(new TCompactProtocol.Factory()).transportFactory(new TFramedTransport.Factory(nonTLSServerMaxFrameSize)).executorService(tPool));
    }
    if (this.serverEventHandler != null) {
        server.setServerEventHandler(this.serverEventHandler);
    }
    return server;
}
Also used : TServer(org.apache.thrift.server.TServer) InetSocketAddress(java.net.InetSocketAddress) TSSLTransportFactory(org.apache.thrift.transport.TSSLTransportFactory) TSSLTransportFactory(org.apache.thrift.transport.TSSLTransportFactory) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TServerSocket(org.apache.thrift.transport.TServerSocket) THsHaServer(org.apache.thrift.server.THsHaServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer)

Example 4 with TServer

use of org.apache.thrift.server.TServer in project HackTutorial by linrongbin16.

the class Server method main.

public static void main(String[] args) throws Exception {
    ServerSocket socket = new ServerSocket(30003);
    TServerSocket serverTransport = new TServerSocket(socket);
    Basic.BasicService.Processor processor = new Basic.BasicService.Processor(new BasicServiceImpl());
    TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
    System.out.println("Server Starting...");
    server.serve();
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) Args(org.apache.thrift.server.TServer.Args) TServer(org.apache.thrift.server.TServer) ServerSocket(java.net.ServerSocket) TServerSocket(org.apache.thrift.transport.TServerSocket) TSimpleServer(org.apache.thrift.server.TSimpleServer)

Example 5 with TServer

use of org.apache.thrift.server.TServer 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)

Aggregations

TServer (org.apache.thrift.server.TServer)33 TServerSocket (org.apache.thrift.transport.TServerSocket)16 TProcessor (org.apache.thrift.TProcessor)12 TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)11 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)9 TThreadPoolServer (org.apache.thrift.server.TThreadPoolServer)9 Test (org.junit.Test)9 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)8 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)7 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)7 TServerTransport (org.apache.thrift.transport.TServerTransport)7 TTransportFactory (org.apache.thrift.transport.TTransportFactory)7 InetSocketAddress (java.net.InetSocketAddress)6 TFramedTransport (org.apache.thrift.transport.TFramedTransport)6 ConfigurationCopy (org.apache.accumulo.core.conf.ConfigurationCopy)5 ServerAddress (org.apache.accumulo.server.rpc.ServerAddress)5 TProtocol (org.apache.thrift.protocol.TProtocol)5 TTransport (org.apache.thrift.transport.TTransport)5 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4