All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lowdad.thrift.client.pool.TTransportPoolFactory Maven / Gradle / Ivy

The newest version!
package com.lowdad.thrift.client.pool;

import com.lowdad.thrift.client.K8ServiceKey;
import com.lowdad.thrift.client.config.RpcClientProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * @author lowdad
 */
@Slf4j
public class TTransportPoolFactory extends BaseKeyedPooledObjectFactory {

    @Override
    public TTransport create(K8ServiceKey key) throws Exception {
        if (key != null) {
            String host = key.getK8ServiceHost();
            int port = key.getK8ServicePort();
            TSocket socket = new TSocket(host, port, Integer.valueOf(RpcClientProperties.getInstance().getTimeOut()));
            TTransport transport = new TFramedTransport(
                    socket, Integer.valueOf(RpcClientProperties.getInstance().getMaxFrameSize()));
            transport.open();
            return transport;

        } else {
            return null;
        }
    }

    @Override
    public PooledObject wrap(TTransport transport) {
        return new DefaultPooledObject<>(transport);
    }

    @Override
    public void destroyObject(K8ServiceKey key, PooledObject obj) throws Exception {
        obj.getObject().close();
    }

    @Override
    public boolean validateObject(K8ServiceKey key, PooledObject obj) {
        return obj.getObject().isOpen();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy