Giter Site home page Giter Site logo

ddth-thriftpool's Introduction

ddth-thriftpool

DDTH's Apache Thrift Client Pooling library.

By Thanh Ba Nguyen (btnguyen2k (at) gmail.com)

Project home: https://github.com/DDTH/ddth-thriftpool

OSGi Environment: ddth-thriftpool is packaged as an OSGi bundle.

License

See LICENSE.txt for details. Copyright (c) 2014-2015 Thanh Ba Nguyen.

Third party libraries are distributed under their own license(s).

Installation

Latest release version: 0.2.2. See RELEASE-NOTES.md.

Maven dependency:

<dependency>
	<groupId>com.github.ddth</groupId>
	<artifactId>ddth-thriftpool</artifactId>
	<version>0.2.2</version>
</dependency>

Usage

Example of creating a pool of Facebook's Scribe Clients:

import com.github.ddth.thriftpool.ITProtocolFactory;
...
// setup a TProtocol Factory, method 1: implement ITProtocolFactory interface
ITProtocolFactory protocolFactory = new ITProtocolFactory() {
    @Override
    public TProtocol create(int serverIndexHash) throws Exception {
        //since we have only one server, serverIndexHash is not used
        TTransport transport = new TFramedTransport(new TSocket(SCRIBE_HOST, SCRIBE_PORT));
        try {
            transport.open();
        } catch (TTransportException e) {
            //transport.close();
            throw e;
        }
        TProtocol protocol = new TBinaryProtocol(transport);
        return protocol;
    }
    
    @Override
    public int getNumServers() {
        return 1;
    }
};

// setup a TProtocol Factory, method 2: extends AbstractTProtocolFactory class
ITProtocolFactory protocolFactory = new AbstractTProtocolFactory("host1:port1,host2:port2,host3:port3") {
    @Override
    public TProtocol create(HostAndPort hostAndPort) throws Exception {
        TTransport transport = new TFramedTransport(new TSocket(hostAndPort.host, hostAndPort.port));
        try {
            transport.open();
        } catch (TTransportException e) {
            //transport.close();
            throw e;
        }
        TProtocol protocol = new TBinaryProtocol(transport);
        return protocol;
    }
};
...

// create and initialize the pool
import scribe.thrift.scribe;
...
ThriftClientPool<scribe.Client, scribe.Iface> pool = new ThriftClientPool<scribe.Client, scribe.Iface>();
pool.setClientClass(scribe.Client.class).setClientInterface(scribe.Iface.class);
pool.setTProtocolFactory(protocolFactory);

// additional stuff: pool configuration
//PoolConfig poolConfig = new PoolConfig();
//poolConfig.setMaxActive(4).setMaxIdle(2).setMinIdle(1);
//pool.setPoolConfig(poolConfig);

// additional stuff: retry policy
//RetryPolicy retryPolicy = new RetryPolicy()
//retryPolicy.setNumRetries(5);
//retryPolicy.setSleepMsBetweenRetries(1000);
//retryPolicy.setRetryType(RetryPolicy.RetryType.RANDOM);
//pool.setRetryPolicy(retryPolicy)

//remember to initialize the pool!
pool.init();
...

// do some stuff
scribe.Iface client = pool.borrowObject();
try {
    System.out.println("Name:\t\t" + client.getName());
    System.out.println("Version:\t" + client.getVersion());
    System.out.println("Status:\t\t" + client.getStatus());
    System.out.println("Alive since:\t" + client.aliveSince());
    System.out.println("Options:\t" + client.getOptions());
    System.out.println("Counters:\t" + client.getCounters());
} finally {
    pool.returnObject(client);
}
....

// destroy the pool when done
pool.destroy();

ddth-thriftpool's People

Contributors

btnguyen2k avatar

Watchers

Feng Zhu avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.