var tran = ThriftPool.Instance().BorrowInstance(); TProtocol protocol = new TBinaryProtocol(tran); var client = new xxxx(protocol); //xxxx爲生成的thrift代理類的Client對象 var ret = client.TestMethod(parameters); //TestMethod爲業務方法名稱 ThriftPool.Instance().ReturnInstance(tran); return ret;
internal class ThriftConfig { #region 單例入口 private static ThriftConfig instance = null; private static object objLock = new object(); public static ThriftConfig Instance() { if (instance == null) { lock (objLock) { if (instance == null) { instance = new ThriftConfig(); } } } return instance; } #endregion #region 私有構造函數 private ThriftConfig() { Host = ConfigHelper.GetAppSettingValue("ESHost"); Port = ConfigHelper.GetAppSettingInt32Value("ESPort"); Encode = Encoding.UTF8; Timeout = 3000; MaxActive = ConfigHelper.GetAppSettingInt32Value("ESMaxActiveConnectionCount"); MaxIdle = ConfigHelper.GetAppSettingInt32Value("ESMaxIdleConnectionCount"); MinIdle = ConfigHelper.GetAppSettingInt32Value("ESMinIdleConnectionCount"); MaxWait = 5; } #endregion #region 配置屬性定義 public string Host { get; set; } public int Port { get; set; } public Encoding Encode { get; set; } public bool Zipped { get; set; } public int Timeout { get; set; } public int MaxActive { get; set; } public int MaxIdle { get; set; } public int MinIdle { get; set; } public int MaxWait { get; set; } #endregion }
internal class ThriftPool { #region 屬性 private ThriftConfig config; /// 對象緩存池 private static Stack<TTransport> objectPool { get; set; } /// 同步對象 private static AutoResetEvent resetEvent; /// 空閒對象數 private static volatile int idleCount = 0; private static volatile int activeCount = 0; /// 同步對象鎖 private static object locker = new object(); #endregion #region 單例入口 private static long testcount = 0; private static ThriftPool instance = null; private static object objLock = new object(); public static ThriftPool Instance() { if (instance == null) { lock (objLock) { if (instance == null) { instance = new ThriftPool(); } } } return instance; } #endregion #region 構造函數 private ThriftPool() { config = ThriftConfig.Instance(); CreateResetEvent(); CreateThriftPool(); } #endregion #region 公有操做方法 /// 從對象池取出一個對象 public TTransport BorrowInstance() { lock (locker) { //Zkx.Infrastruction.Logger.Log.DebugFormat("借前對象池個數:{0},空閒個數:{1}", objectPool.Count(), idleCount); TTransport transport; //對象池無空閒對象 if (idleCount == 0) { //對象池已已建立對象數達上限 if (activeCount >= config.MaxActive) { //Console.WriteLine("waiting..." + activeCount); resetEvent.WaitOne(); } else { PushObject(CreateInstance()); } } transport = objectPool.Pop(); //Console.WriteLine("Pop 對象..." + transport.IsOpen); //空閒對象數小於最小空閒數,添加一個對象到對象池(已建立數不能超標) if (--idleCount < config.MinIdle && activeCount < config.MaxActive) { PushObject(CreateInstance()); } ValidateInstance(transport); //Console.WriteLine("借出......對象池個數:{0},空閒個數:{1}," + testcount, objectPool.Count(), idleCount); return transport; } } /// 歸還一個對象 /// <param name="instance"></param> public void ReturnInstance(TTransport instance) { lock (locker) { // Console.WriteLine("Push 對象..." + instance.IsOpen); //空閒對象數達到上限,再也不返回線程池,直接銷燬 if (idleCount == config.MaxIdle) { DestoryInstance(instance); } else { ValidateInstance(instance); PushObject(instance); //發通知信號,有對象歸還到對象池 resetEvent.Set(); //Console.WriteLine("歸還..."); } } } #endregion #region 私有方法 /// 建立線程同步對象 private void CreateResetEvent() { lock (locker) { if (resetEvent == null) { resetEvent = new AutoResetEvent(false); } } } /// 建立對象池 private void CreateThriftPool() { lock (locker) { if (objectPool == null) { objectPool = new Stack<TTransport>(); } } } /// 添加對象到對象池 private void PushObject(TTransport transport) { objectPool.Push(transport); idleCount++; } /// 建立一個對象 private TTransport CreateInstance() { activeCount++; var objsocket = new TSocket(config.Host, config.Port); objsocket.Timeout = 5000; TTransport transport = objsocket; transport.Open(); //Console.WriteLine("建立對象..." + activeCount); return transport; } /// 校驗對象 private void ValidateInstance(TTransport instance) { if (!instance.IsOpen) { //Console.WriteLine("校驗_從新打開..."); instance.Open(); } } /// 銷燬對象 private void DestoryInstance(TTransport instance) { if (instance.IsOpen) { instance.Close(); } //instance.Flush(); instance.Dispose(); activeCount--; //Console.WriteLine("銷燬..."); } #endregion }