CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-thrift--libthrift

Apache Thrift Java Library - A lightweight, language-independent software stack for point-to-point RPC implementation providing clean abstractions and implementations for data transport, data serialization, and application level processing

Pending
Overview
Eval results
Files

servers.mddocs/

Server Implementations

Various server types for different performance and scalability requirements. Thrift provides multiple server implementations ranging from simple single-threaded servers to high-performance non-blocking servers for different use cases.

Capabilities

Server Base Classes

Core server interfaces and abstract classes that all server implementations extend.

/**
 * Abstract base class for all Thrift server implementations
 */
public abstract class TServer {
    /** Serve requests (blocking call that runs the server) */
    public abstract void serve();
    
    /** Stop the server */
    public void stop();
    
    /** Check if server is currently serving */
    public boolean isServing();
    
    /** Set event handler for server lifecycle events */
    public void setServerEventHandler(TServerEventHandler eventHandler);
    
    /** Get the server event handler */
    public TServerEventHandler getEventHandler();
    
    /**
     * Abstract base class for server configuration arguments
     */
    public static abstract class AbstractServerArgs<T extends AbstractServerArgs<T>> {
        /** Set the request processor */
        public T processor(TProcessor processor);
        
        /** Set the server transport */
        public T serverTransport(TServerTransport serverTransport);
        
        /** Set the protocol factory */
        public T protocolFactory(TProtocolFactory protocolFactory);
        
        /** Set the transport factory */
        public T transportFactory(TTransportFactory transportFactory);
        
        /** Set input protocol factory */
        public T inputProtocolFactory(TProtocolFactory inputProtocolFactory);
        
        /** Set output protocol factory */
        public T outputProtocolFactory(TProtocolFactory outputProtocolFactory);
        
        /** Set input transport factory */
        public T inputTransportFactory(TTransportFactory inputTransportFactory);
        
        /** Set output transport factory */
        public T outputTransportFactory(TTransportFactory outputTransportFactory);
        
        /** Set processor factory */
        public T processorFactory(TProcessorFactory processorFactory);
    }
}

/**
 * Interface for handling server lifecycle events
 */
public interface TServerEventHandler {
    /** Called when server starts serving */
    public void preServe();
    
    /** Called when a new client connects */
    public ServerContext createContext(TProtocol input, TProtocol output);
    
    /** Called when a client disconnects */
    public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output);
    
    /** Called before processing each request */
    public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport);
}

/**
 * Interface for server context information
 */
public interface ServerContext {
    /** Check if this context wraps an object of the given type */
    public <T> boolean isWrapperFor(Class<T> iface);
    
    /** Unwrap this context to the specified type */
    public <T> T unwrap(Class<T> iface);
}

Simple Server

Single-threaded blocking server for basic use cases and development.

/**
 * Simple single-threaded server that processes one request at a time
 */
public class TSimpleServer extends TServer {
    /** Create simple server with arguments */
    public TSimpleServer(AbstractServerArgs args);
    
    /** Serve requests in single thread (blocking) */
    public void serve();
    
    /** Stop the server */
    public void stop();
    
    /**
     * Configuration arguments for TSimpleServer
     */
    public static class Args extends AbstractServerArgs<Args> {
        /** Create args with server transport */
        public Args(TServerTransport transport);
    }
}

Usage Examples:

import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.protocol.TBinaryProtocol;

// Create server transport
TServerSocket serverTransport = new TServerSocket(9090);

// Create processor for your service
MyService.Processor<MyServiceHandler> processor = 
    new MyService.Processor<>(new MyServiceHandler());

// Create simple server
TSimpleServer server = new TSimpleServer(
    new TSimpleServer.Args(serverTransport)
        .processor(processor)
        .protocolFactory(new TBinaryProtocol.Factory())
);

// Start serving (blocks until stopped)
server.serve();

Thread Pool Server

Multi-threaded server using a thread pool for concurrent request processing.

/**
 * Multi-threaded server using thread pool for request processing
 */
public class TThreadPoolServer extends TServer {
    /** Create thread pool server with arguments */
    public TThreadPoolServer(AbstractServerArgs args);
    
    /** Serve requests using thread pool */
    public void serve();
    
    /** Stop the server and shutdown thread pool */
    public void stop();
    
    /**
     * Configuration arguments for TThreadPoolServer
     */
    public static class Args extends AbstractServerArgs<Args> {
        /** Create args with server transport */
        public Args(TServerTransport transport);
        
        /** Set minimum number of worker threads */
        public Args minWorkerThreads(int n);
        
        /** Set maximum number of worker threads */
        public Args maxWorkerThreads(int n);
        
        /** Set request timeout in milliseconds */
        public Args requestTimeout(int n);
        
        /** Set request timeout unit */
        public Args requestTimeoutUnit(TimeUnit timeoutUnit);
        
        /** Set stop timeout for graceful shutdown */
        public Args stopTimeoutVal(int stopTimeoutVal);
        
        /** Set stop timeout unit */
        public Args stopTimeoutUnit(TimeUnit stopTimeoutUnit);
        
        /** Set custom executor service */
        public Args executorService(ExecutorService executorService);
        
        /** Set custom thread factory */
        public Args executorService(ThreadFactory threadFactory);
    }
}

Usage Examples:

import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

// Create thread pool server with custom configuration
TServerSocket serverTransport = new TServerSocket(9090);
MyService.Processor<MyServiceHandler> processor = 
    new MyService.Processor<>(new MyServiceHandler());

TThreadPoolServer server = new TThreadPoolServer(
    new TThreadPoolServer.Args(serverTransport)
        .processor(processor)
        .protocolFactory(new TBinaryProtocol.Factory())
        .minWorkerThreads(10)
        .maxWorkerThreads(100)
        .requestTimeout(30)
        .requestTimeoutUnit(TimeUnit.SECONDS)
);

// Or with custom executor
TThreadPoolServer customServer = new TThreadPoolServer(
    new TThreadPoolServer.Args(serverTransport)
        .processor(processor)
        .executorService(Executors.newFixedThreadPool(50))
);

server.serve();

Non-blocking Server

Non-blocking server using NIO for high-performance single-threaded operation.

/**
 * Non-blocking server implementation using NIO
 */
public class TNonblockingServer extends AbstractNonblockingServer {
    /** Create non-blocking server with arguments */
    public TNonblockingServer(AbstractNonblockingServerArgs args);
    
    /**
     * Configuration arguments for TNonblockingServer
     */
    public static class Args extends AbstractNonblockingServerArgs<Args> {
        /** Create args with non-blocking server transport */
        public Args(TNonblockingServerTransport transport);
    }
}

/**
 * Abstract base class for non-blocking server implementations
 */
public abstract class AbstractNonblockingServer extends TServer {
    /** Maximum read buffer size */
    public static final long MAX_READ_BUFFER_BYTES = Long.MAX_VALUE;
    
    /** Maximum frame size */
    public static final int MAX_FRAME_SIZE = 16384000;
    
    /** Serve requests using non-blocking I/O */
    public void serve();
    
    /** Stop the server */
    public void stop();
    
    /**
     * Abstract configuration arguments for non-blocking servers
     */
    public static abstract class AbstractNonblockingServerArgs<T extends AbstractNonblockingServerArgs<T>> 
            extends AbstractServerArgs<T> {
        /** Set maximum read buffer bytes */
        public T maxReadBufferBytes(long maxReadBufferBytes);
        
        /** Set maximum frame size */
        public T maxFrameSize(int maxFrameSize);
    }
}

Usage Examples:

import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.protocol.TBinaryProtocol;

// Create non-blocking server
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(9090);
MyService.Processor<MyServiceHandler> processor = 
    new MyService.Processor<>(new MyServiceHandler());

TNonblockingServer server = new TNonblockingServer(
    new TNonblockingServer.Args(serverTransport)
        .processor(processor)
        .protocolFactory(new TBinaryProtocol.Factory())
        .maxFrameSize(1024 * 1024)  // 1MB max frame
);

server.serve();

Half-Sync/Half-Async Server

Server with non-blocking I/O and thread pool for processing.

/**
 * Half-sync/half-async server: non-blocking I/O with thread pool processing
 */
public class THsHaServer extends AbstractNonblockingServer {
    /** Create HsHa server with arguments */
    public THsHaServer(AbstractNonblockingServerArgs args);
    
    /**
     * Configuration arguments for THsHaServer
     */
    public static class Args extends AbstractNonblockingServerArgs<Args> {
        /** Create args with non-blocking server transport */
        public Args(TNonblockingServerTransport transport);
        
        /** Set minimum worker threads */
        public Args minWorkerThreads(int n);
        
        /** Set maximum worker threads */
        public Args maxWorkerThreads(int n);
        
        /** Set stop timeout for graceful shutdown */
        public Args stopTimeoutVal(int stopTimeoutVal);
        
        /** Set stop timeout unit */
        public Args stopTimeoutUnit(TimeUnit stopTimeoutUnit);
        
        /** Set custom executor service for processing */
        public Args executorService(ExecutorService executorService);
    }
}

Usage Examples:

import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import java.util.concurrent.TimeUnit;

// Create HsHa server for high concurrency
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(9090);
MyService.Processor<MyServiceHandler> processor = 
    new MyService.Processor<>(new MyServiceHandler());

THsHaServer server = new THsHaServer(
    new THsHaServer.Args(serverTransport)
        .processor(processor)
        .protocolFactory(new TBinaryProtocol.Factory())
        .minWorkerThreads(5)
        .maxWorkerThreads(50)
        .stopTimeoutVal(10)
        .stopTimeoutUnit(TimeUnit.SECONDS)
);

server.serve();

Threaded Selector Server

High-performance server with multiple selector threads and thread pool.

/**
 * High-performance server with multiple selector threads
 */
public class TThreadedSelectorServer extends AbstractNonblockingServer {
    /** Create threaded selector server with arguments */
    public TThreadedSelectorServer(AbstractNonblockingServerArgs args);
    
    /**
     * Configuration arguments for TThreadedSelectorServer
     */
    public static class Args extends AbstractNonblockingServerArgs<Args> {
        /** Create args with non-blocking server transport */
        public Args(TNonblockingServerTransport transport);
        
        /** Set number of selector threads */
        public Args selectorThreads(int selectorThreads);
        
        /** Set number of worker threads */
        public Args workerThreads(int workerThreads);
        
        /** Set stop timeout for graceful shutdown */
        public Args stopTimeoutVal(int stopTimeoutVal);
        
        /** Set stop timeout unit */
        public Args stopTimeoutUnit(TimeUnit stopTimeoutUnit);
        
        /** Set custom executor service for processing */
        public Args executorService(ExecutorService executorService);
        
        /** Set custom executor service for selector threads */
        public Args selectorExecutorService(ExecutorService selectorExecutorService);
        
        /** Set accept policy */
        public Args acceptPolicy(AcceptPolicy acceptPolicy);
        
        /** Set accept queue size per thread */
        public Args acceptQueueSizePerThread(int acceptQueueSizePerThread);
    }
    
    /**
     * Accept policy for new connections
     */
    public enum AcceptPolicy {
        ROUND_ROBIN,
        FAIR_ACCEPT
    }
}

Usage Examples:

import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import java.util.concurrent.Executors;

// Create threaded selector server for maximum performance
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(9090);
MyService.Processor<MyServiceHandler> processor = 
    new MyService.Processor<>(new MyServiceHandler());

TThreadedSelectorServer server = new TThreadedSelectorServer(
    new TThreadedSelectorServer.Args(serverTransport)
        .processor(processor)
        .protocolFactory(new TBinaryProtocol.Factory())
        .selectorThreads(4)           // 4 selector threads
        .workerThreads(32)            // 32 worker threads
        .acceptQueueSizePerThread(4)  // Accept queue size
        .acceptPolicy(TThreadedSelectorServer.AcceptPolicy.FAIR_ACCEPT)
);

server.serve();

SASL Non-blocking Server

Non-blocking server with SASL authentication support.

/**
 * Non-blocking server with SASL authentication
 */
public class TSaslNonblockingServer extends AbstractNonblockingServer {
    /** Create SASL non-blocking server with arguments */
    public TSaslNonblockingServer(AbstractNonblockingServerArgs args);
    
    /**
     * Configuration arguments for TSaslNonblockingServer
     */
    public static class Args extends AbstractNonblockingServerArgs<Args> {
        /** Create args with non-blocking server transport */
        public Args(TNonblockingServerTransport transport);
        
        // SASL-specific configuration methods would be added here
    }
}

Servlet-based Servers

HTTP servlet-based server implementations for web containers.

/**
 * Servlet-based Thrift server for web containers
 */
public class TServlet extends HttpServlet {
    /** Create servlet with processor and protocol factory */
    public TServlet(TProcessor processor, TProtocolFactory protocolFactory);
    
    /** Create servlet with processor factory */
    public TServlet(TProcessorFactory processorFactory, TProtocolFactory protocolFactory);
    
    /** Handle HTTP POST requests */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException;
    
    /** Handle HTTP GET requests (typically returns error) */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException;
}

/**
 * Extensible servlet with customizable request/response handling
 */
public class TExtensibleServlet extends TServlet {
    /** Create extensible servlet */
    public TExtensibleServlet(TProcessor processor, TProtocolFactory protocolFactory);
    
    /** Customize request handling */
    protected void customizeRequest(HttpServletRequest request);
    
    /** Customize response handling */
    protected void customizeResponse(HttpServletResponse response);
}

Usage Examples:

import org.apache.thrift.server.TServlet;
import org.apache.thrift.protocol.TBinaryProtocol;
import javax.servlet.annotation.WebServlet;

// Servlet configuration
@WebServlet("/thrift")
public class MyThriftServlet extends TServlet {
    public MyThriftServlet() {
        super(new MyService.Processor<>(new MyServiceHandler()),
              new TBinaryProtocol.Factory());
    }
}

// Or create servlet programmatically
TServlet servlet = new TServlet(
    new MyService.Processor<>(new MyServiceHandler()),
    new TBinaryProtocol.Factory()
);

Server Utilities

Utility classes and interfaces for server operations.

/**
 * Represents a method invocation for logging and debugging
 */
public class Invocation {
    /** Create invocation */
    public Invocation(String methodName, Object[] args);
    
    /** Get method name */
    public String getMethodName();
    
    /** Get method arguments */
    public Object[] getArgs();
    
    /** String representation of invocation */
    public String toString();
}

Complete Server Example:

import org.apache.thrift.server.*;
import org.apache.thrift.transport.*;
import org.apache.thrift.protocol.*;
import java.util.concurrent.TimeUnit;

public class ThriftServerExample {
    public static void main(String[] args) throws Exception {
        // Create server transport
        TServerSocket serverTransport = new TServerSocket(9090);
        
        // Create processor
        MyService.Processor<MyServiceHandler> processor = 
            new MyService.Processor<>(new MyServiceHandler());
        
        // Add server event handler
        TServerEventHandler eventHandler = new TServerEventHandler() {
            public void preServe() {
                System.out.println("Server starting...");
            }
            
            public ServerContext createContext(TProtocol input, TProtocol output) {
                System.out.println("Client connected");
                return null;
            }
            
            public void deleteContext(ServerContext ctx, TProtocol input, TProtocol output) {
                System.out.println("Client disconnected");
            }
            
            public void processContext(ServerContext ctx, TTransport in, TTransport out) {
                // Pre-process request
            }
        };
        
        // Create and configure server
        TThreadPoolServer server = new TThreadPoolServer(
            new TThreadPoolServer.Args(serverTransport)
                .processor(processor)
                .protocolFactory(new TBinaryProtocol.Factory())
                .transportFactory(new TFramedTransport.Factory())
                .minWorkerThreads(10)
                .maxWorkerThreads(100)
                .requestTimeout(30)
                .requestTimeoutUnit(TimeUnit.SECONDS)
        );
        
        server.setServerEventHandler(eventHandler);
        
        // Start server
        System.out.println("Starting server on port 9090...");
        server.serve();
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-thrift--libthrift

docs

async.md

core.md

index.md

metadata.md

protocols.md

schemes.md

servers.md

transports.md

utilities.md

tile.json