de.cpg.oss:thrift-client-pool-java

A Thrift Client pool for Java


License
Artistic-2.0

Documentation

thrift-client-pool-java

A Thrift Client pool for Java

Build Status

  • raw and TypeSafe TServiceClient pool
  • Multi Backend Servers support
  • Backend Servers replace on the fly
  • Backend route by hash or any other algorithm
  • java.io.Closeable resources (for try with resources)
  • Ease of use
  • jdk 1.8 only (1.7 is not okay without code modification)

Usage

Add to your pom.xml

<dependency>
    <groupId>de.cpg.oss</groupId>
    <artifactId>thrift-client-pool-java</artifactId>
    <version>1.1</version>
</dependency>
// get a pool
PoolConfig config = new PoolConfig();
config.setFailover(true); // optional
config.setTimeout(1000); // optional
// PoolConfig is a instance of GenericObjectPoolConfig
config.setMinIdle(3);
config.setMaxTotal(30);
ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(
    serverList,
    e -> new YourThriftService.Client(new TFramedTransport(new TBinaryProtocol(e))),  // ❶ 
    config);

// or pre jdk1.8
ThriftClientPool<TestThriftService.Client> pool = new ThriftClientPool<>(serverList,
    new ThriftClientFactory() { // ❶

        @Override
        public TServiceClient createClient(TTransport transport) {
            return new YourThriftService.Client(new TFramedTransport(new TBinaryProtocol(transport)));
        }
    }, config);

// just call thrift
try {
    Iface iFace = pool.iface(); // ❷
    String response = iFace.echo("Hello!"); // ❸
    logger.info("get response: {}", response);
} catch (Throwable e) {
    logger.error("call echo fail", e);
}

// or call like this
try (ThriftClient<Client> thriftClient = pool.getClient()) { // ❹
    // get Iface generated by Thrift
    Iface iFace = thriftClient.iFace(); // ❺

    String response = iFace.echo("Hello!");
    logger.info("get response: {}", response);
    response = iFace.echo("Hello again!");
    logger.info("get response: {}", response);

    // finish must be called at last
    thriftClient.finish(); // ❻ 
} catch (TException e) {
    logger.error("call echo fail", e);
}
  • ❶ return your service Client(an IFace impl and TServiceClient subclass) generated by Thrift
  • ❷ obtain it from pool
  • ❸ call your service
  • ❹ another way is getting a wrapped client using try with resources
  • ❺ get Iface from wrapped client
  • ❻ when using getClient(), finish must be called if non Exception throw in interacting

ThriftClientFactory

Only one interface need to impl with few lines, return one YourThriftService.Client in createClient(TTransport) generated by thrift tools. Pool will handle all the rest.

ThriftClientPool

  • void setServices(List);

Dynamically change backend services, all new client get from getClient() will using new services.

Know issues

If you encourage this exception, please check that Iface match Pool's Client type.

java.lang.ClassCastException: com.sun.proxy.$Proxy3 cannot be cast to xx.xx.Iface

See also

A better pure connection pool with shard