thrift-client-pool-java

A Thrift Client pool for Java

License

License

The Artistic License 2.0
Categories

Categories

Java Languages CLI User Interface
GroupId

GroupId

com.wealoha
ArtifactId

ArtifactId

thrift-client-pool-java
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

thrift-client-pool-java
A Thrift Client pool for Java
Project URL

Project URL

https://github.com/aloha-app/thrift-client-pool-java
Source Code Management

Source Code Management

https://github.com/aloha-app/thrift-client-pool-java.git

Download thrift-client-pool-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.wealoha/thrift-client-pool-java/ -->
<dependency>
    <groupId>com.wealoha</groupId>
    <artifactId>thrift-client-pool-java</artifactId>
    <version>1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.wealoha/thrift-client-pool-java/
implementation 'com.wealoha:thrift-client-pool-java:1.2'
// https://jarcasting.com/artifacts/com.wealoha/thrift-client-pool-java/
implementation ("com.wealoha:thrift-client-pool-java:1.2")
'com.wealoha:thrift-client-pool-java:jar:1.2'
<dependency org="com.wealoha" name="thrift-client-pool-java" rev="1.2">
  <artifact name="thrift-client-pool-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.wealoha', module='thrift-client-pool-java', version='1.2')
)
libraryDependencies += "com.wealoha" % "thrift-client-pool-java" % "1.2"
[com.wealoha/thrift-client-pool-java "1.2"]

Dependencies

compile (5)

Group / Artifact Type Version
org.apache.commons : commons-pool2 jar 2.2
org.apache.commons : commons-lang3 jar 3.3
org.apache.thrift : libthrift jar 0.9.2
ch.qos.logback : logback-classic jar 1.1.2
org.slf4j : slf4j-api jar 1.7.12

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

thrift-client-pool-java

A Thrift Client pool for Java

  • 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>com.wealoha</groupId>
    <artifactId>thrift-client-pool-java</artifactId>
    <version>1.0</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);
}

// shard support
List<ServiceInfo> serviceList = Arrays.asList( //
                new ServiceInfo("127.0.0.1", 9090), //
                new ServiceInfo("127.0.0.1", 9091), //
                new ServiceInfo("127.0.0.1", 9092), //
                new ServiceInfo("127.0.0.1", 9093), //
                new ServiceInfo("127.0.0.1", 9094));

ShardedThriftClientPool<Integer, Client> shardedPool = new ShardedThriftClientPool<>(
        serviceList, //
        key -> key, // hash function
        servers -> { // shard function
            return Arrays.asList( //
                    Arrays.asList(servers.get(0), servers.get(1)), //
                    Arrays.asList(servers.get(2), servers.get(3)), //
                    Arrays.asList(servers.get(4)));
        }, //
        servers -> new ThriftClientPool<>(servers, Client::new, config));

Integer key = 10;
ThriftClientPool<Client> pool = shardedPool.getShardedPool(key);
// Use pool as previous examples.
  • ❶ 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

com.wealoha

Finka

The developer team of App Aloha.

Versions

Version
1.2
1.1
1.0