简介
Thrift是Facebook的核心框架之一,使不同的开发语言开发的系统可以通过该框架实现彼此的通信,类似于webservice,但是Thrift提供了近乎变态的效率和开发的方便性,是webservice所不能比拟的。给分布式开发带来了极大的方便。但是这柄利器也有一些不完美。
问题
首先文档相当的少,只有一个wiki网站提供相应的帮助。这对于Thrift的推广极为不利。
其次框架本身实现有一些缺陷,就Thrift的java部分来说,没有提供连接池的支持,对RPC的调用效率有所影响。
对于文档稀少的问题,只能是通过一些Thrift的开发者和使用者多供献一些自己的心得来解决。这得需要一个过程。而连接池的问题的解决则可以快速一些。
提到池一般做过Java开发的肯定会想到ObjectPool,Apache Commons项目确实给我们的开发得来了很大的便利性,其中的pool项目正是我们实现thrift连接池的基础,当然也少不了神器spring framework。
实现
一,定义thrift连接池接口
ConnectionProvider
/*
* @(#)ConnectionProvider.java 0.1 05/11/17
*
* Copyright 2010 QISI, Inc. All rights reserved.
* QISI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.qidea.thrift.pool;
import org.ansport.TSocket;
/**
*
* @author sunwei
* @version 2010-8-6
* @since JDK1.5
*/
public interface ConnectionProvider
{
/**
* 取链接池中的一个链接
*
* @return
*/
public TSocket getConnection();
/**
* 返回链接
*
* @param socket
*/
public void returnCon(TSocket socket);
}
二,实现连接池
GenericConnectionProvider
/*
* @(#)DefaultConnectionProviderImpl.java 0.1 05/11/17
*
* Copyright 2010 QISI, Inc. All rights reserved.
* QISI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.qidea.thrift.pool;webservice实现
import org.apachemons.pool.ObjectPool;
import org.apachemons.pool.impl.GenericObjectPool;
import org.ansport.TSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
*
* @author sunwei
* @version 2010-8-10
* @since JDK1.5
*/
public class GenericConnectionProvider implements ConnectionProvider,
InitializingBean, DisposableBean
{
public static final Logger logger = LoggerFactory
.getLogger(GenericConnectionProvider.class);
/** 服务的IP地址 */
private String serviceIP;
/** 服务的端口 */
private int servicePort;
/** 连接超时配置 */
private int conTimeOut;
/** 可以从缓存池中分配对象的最大数量 */
private int maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
/** 缓存池中最大空闲对象数量 */
private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
/** 缓存池中最小空闲对象数量 */
private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
/** 阻塞的最大数量 */
private long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
/** 从缓存池中分配对象,是否执行PoolableObjectFactory.validateObject方法 */
private boolean testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW;
private boolean testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN;
private boolean testWhileIdle = GenericObjectPool.DEFAULT_TEST_WHILE_IDLE;
/** 对象缓存池 */
private ObjectPool objectPool = null;
/**
*
*/
@Override
public void afterPropertiesSet() throws Exception
{
// 对象池
objectPool = new GenericObjectPool();
//
((GenericObjectPool) objectPool).setMaxActive(maxActive);
((GenericObjectPool) objectPool).setMaxIdle(maxIdle);
((GenericObjectPool) objectPool).setMinIdle(minIdle);
((GenericObjectPool) objectPool).setMaxWait(maxWait);
((GenericObjectPool) objectPool).setTestOnBorrow(testOnBorrow);
((GenericObjectPool) objectPool).setTestOnReturn(testOnReturn);
((GenericObjectPool) objectPool).setTestWhileIdle(testWhileIdle);
((GenericObjectPool) objectPool)
.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
// 设置factory
ThriftPoolableObjectFactory thriftPoolableObjectFactory = new ThriftPoolableObjectFactory(
serviceIP, servicePort, conTimeOut);
objectPool.setFactory(thriftPoolableObjectFactory);
}
@Override
public void destroy()
{
try
{
objectPool.close();
}
catch (Exception e)
{
throw new RuntimeException("erorr destroy()", e);
}
}
@Override
public TSocket getConnection()
{
try
{
TSocket socket = (TSocket) objectPool.borrowObject();
return socket;
}
catch (Exception e)
{
throw new RuntimeException("error getConnection()", e);
}
}
@Override
public void returnCon(TSocket socket)
{
try
{
urnObject(socket);
}
catch (Exception e)
{
throw new RuntimeException("error returnCon()", e);
}
}
public String getServiceIP()
{
return serviceIP;
}
public void setServiceIP(String serviceIP)
{
this.serviceIP = serviceIP;
}
public int getServicePort()
{
return servicePort;
}
public void setServicePort(int servicePort)
{
this.servicePort = servicePort;
}
public int getConTimeOut()
{
return conTimeOut;
}
public void setConTimeOut(int conTimeOut)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论