FastDFSClientPool.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package cn.stylefeng.guns.zjxlUtil;
  2. import org.csource.fastdfs.ClientGlobal;
  3. import org.csource.fastdfs.StorageClient;
  4. import org.csource.fastdfs.TrackerClient;
  5. import org.csource.fastdfs.TrackerServer;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. /**
  10. * Created by szx on 2015/9/19.
  11. */
  12. public class FastDFSClientPool {
  13. private static volatile FastDFSClientPool pool;
  14. private Map<StorageClient, Boolean> map;
  15. private int maxPoolSize = 20;
  16. private int waitTime = 500;
  17. private int initPoolSize = 5;
  18. static {
  19. try {
  20. String basePath = FastDFSClientPool.class.getResource("/").getPath();
  21. ClientGlobal.init(FastDFSUtil.class.getResource("/config/fdfs_client.conf").getPath());
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. private FastDFSClientPool() {
  27. init();
  28. }
  29. public static FastDFSClientPool getInstance() {
  30. if (pool == null) {
  31. synchronized (FastDFSClientPool.class) {
  32. if(pool == null) {
  33. pool = new FastDFSClientPool();
  34. }
  35. }
  36. }
  37. System.out.println("FastDFSClientPool:"+pool.toString());
  38. return pool;
  39. }
  40. private void init() {
  41. try {
  42. map = new HashMap<StorageClient, Boolean>();
  43. //不做初始缓存
  44. // for (int i = 0; i < initPoolSize; i++) {
  45. // map.put(getNewStorageClient(), true);
  46. // }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. public TrackerServer getTrackerServer(){
  52. try {
  53. TrackerClient tracker = new TrackerClient();
  54. return tracker.getConnection();
  55. }catch (Exception e){
  56. e.printStackTrace();
  57. }
  58. return null;
  59. }
  60. private StorageClient getNewStorageClient() {
  61. try {
  62. TrackerClient tracker = new TrackerClient();
  63. TrackerServer trackerServer = tracker.getConnection();
  64. StorageClient client = new StorageClient(trackerServer, null);
  65. return client;
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. return null;
  70. }
  71. public synchronized StorageClient getStorageClient() {
  72. StorageClient client = null;
  73. try {
  74. for (Entry<StorageClient, Boolean> entry : map.entrySet()) {
  75. if (entry.getValue()) {
  76. client = entry.getKey();
  77. System.out.println("从缓存中获得fastdfs client");
  78. map.put(client, false);
  79. break;
  80. }
  81. }
  82. if (client == null) {
  83. if(map.size()<maxPoolSize) {
  84. System.out.println("创建新fastdfs client:");
  85. client = getNewStorageClient();
  86. map.put(client, false);
  87. }
  88. }
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. return client;
  93. }
  94. public void releaseStorageClient(StorageClient client) {
  95. if (client == null) {
  96. return;
  97. }
  98. try {
  99. if(map.containsKey(client)) {
  100. map.put(client, true);
  101. } else {
  102. client =null;
  103. }
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }