LJClientPool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using LJLib.Net.SPI.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Threading;
  6. namespace LJLib.Client
  7. {
  8. public sealed class LJClientPool : ILJClient
  9. {
  10. private const int DEFAULT_RETRY_COUNT = 1;
  11. private LJClientCreator _creator;
  12. private ILJClient[] _clients;
  13. private bool[] _client_states;
  14. private object _syncRoot = new object();
  15. public LJClientPool(LJClientCreator creator, int size)
  16. {
  17. _creator = creator;
  18. _clients = new ILJClient[size];
  19. _client_states = new bool[size];
  20. for (int i = 0; i < size; i++)
  21. {
  22. _clients[i] = null;
  23. _client_states[i] = false;
  24. }
  25. }
  26. public string DoExcute(string apiName, string requestJSON)
  27. {
  28. int i;
  29. do
  30. {
  31. lock (_syncRoot)
  32. {
  33. for (i = 0; i < _client_states.Length; i++)
  34. {
  35. if (!_client_states[i])
  36. {
  37. _client_states[i] = true;
  38. break;
  39. }
  40. }
  41. }
  42. if (i >= _client_states.Length)
  43. {
  44. Thread.Sleep(100);
  45. }
  46. } while (i >= _client_states.Length);
  47. try
  48. {
  49. int retry = 0;
  50. string rslt = null;
  51. while (retry <= DEFAULT_RETRY_COUNT)
  52. {
  53. try
  54. {
  55. if (_clients[i] == null)
  56. {
  57. _clients[i] = _creator.newClient();
  58. }
  59. rslt = _clients[i].DoExcute(apiName, requestJSON);
  60. break;
  61. }
  62. catch (Exception e)
  63. {
  64. Trace.Write(e.ToString());
  65. if (_clients[i] != null)
  66. {
  67. _clients[i].Dispose();
  68. _clients[i] = null;
  69. }
  70. ++retry;
  71. if (retry > DEFAULT_RETRY_COUNT)
  72. {
  73. throw e;//大于重试次数时异常重抛
  74. }
  75. }
  76. }
  77. return rslt;
  78. }
  79. catch (Exception ex)
  80. {
  81. Trace.Write(ex.ToString());
  82. return ex.ToString();
  83. }
  84. finally
  85. {
  86. _client_states[i] = false;
  87. }
  88. }
  89. public void Beat()
  90. {
  91. for (int i = 0; i < _client_states.Length; i++)
  92. {
  93. lock (_syncRoot)
  94. {
  95. if (_client_states[i])
  96. {
  97. continue;
  98. }
  99. _client_states[i] = true;
  100. }
  101. try
  102. {
  103. //空实例无须心跳
  104. if (_clients[i] != null)
  105. {
  106. _clients[i].Beat();
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. _clients[i].Dispose();
  112. _clients[i] = null;
  113. Trace.Write("连接池请求异常" + e.ToString());
  114. }
  115. finally
  116. {
  117. _client_states[i] = false;
  118. }
  119. }
  120. }
  121. public void Dispose()
  122. {
  123. for (int i = 0; i < _clients.Length; i++)
  124. {
  125. if (_clients[i] != null)
  126. {
  127. _clients[i].Dispose();
  128. _clients[i] = null;
  129. }
  130. }
  131. }
  132. }
  133. }