FastDFSConfig.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.yihu.base.config;
  2. import com.yihu.base.fastdfs.FastDFSClientPool;
  3. import com.yihu.base.fastdfs.FastDFSHelper;
  4. import org.csource.common.MyException;
  5. import org.csource.fastdfs.ClientGlobal;
  6. import org.csource.fastdfs.TrackerGroup;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import javax.annotation.PostConstruct;
  13. import java.net.InetSocketAddress;
  14. /**
  15. * @author Sand
  16. * @version 1.0
  17. * @created 2015.11.27 16:08
  18. */
  19. @Configuration
  20. public class FastDFSConfig {
  21. private Logger logger= LoggerFactory.getLogger(FastDFSConfig.class);
  22. @Value("${fast-dfs.pool.init-size}")
  23. private int initPoolSize;
  24. @Value("${fast-dfs.pool.max-size}")
  25. private int maxPoolSize;
  26. @Value("${fast-dfs.pool.wait-time}")
  27. private int waitTime;
  28. @Value("${fast-dfs.connect-timeout}")
  29. private int connectTimeout;
  30. @Value("${fast-dfs.network-timeout}")
  31. private int networkTimeout;
  32. @Value("${fast-dfs.charset}")
  33. private String charset;
  34. @Value("${fast-dfs.tracker-server}")
  35. private String trackerServers;
  36. @Value("${fast-dfs.http.tracker-http-port}")
  37. private int httpPort;
  38. @Value("${fast-dfs.http.anti-steal-token}")
  39. private boolean antiStealToken;
  40. @Value("${fast-dfs.http.secret-key}")
  41. private String secretKey;
  42. @PostConstruct
  43. void init() {
  44. try {
  45. // 此代码复制自:ClientGlobal.init() 方法
  46. ClientGlobal.g_connect_timeout = connectTimeout;
  47. if (ClientGlobal.g_connect_timeout < 0) {
  48. ClientGlobal.g_connect_timeout = 5;
  49. }
  50. ClientGlobal.g_connect_timeout *= 1000;
  51. ClientGlobal.g_network_timeout = networkTimeout;
  52. if (ClientGlobal.g_network_timeout < 0) {
  53. ClientGlobal.g_network_timeout = 30;
  54. }
  55. ClientGlobal.g_network_timeout *= 1000;
  56. ClientGlobal.g_charset = charset;
  57. if (ClientGlobal.g_charset == null || ClientGlobal.g_charset.length() == 0) {
  58. ClientGlobal.g_charset = "ISO8859-1";
  59. }
  60. String[] szTrackerServers = trackerServers.split(";");
  61. if (szTrackerServers == null) {
  62. throw new MyException("item \"tracker_server\" not found");
  63. } else {
  64. InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
  65. for (int i = 0; i < szTrackerServers.length; ++i) {
  66. String[] parts = szTrackerServers[i].split("\\:", 2);
  67. if (parts.length != 2) {
  68. throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
  69. }
  70. tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
  71. }
  72. ClientGlobal.g_tracker_group = new TrackerGroup(tracker_servers);
  73. ClientGlobal.g_tracker_http_port = httpPort;
  74. ClientGlobal.g_anti_steal_token = antiStealToken;
  75. if (ClientGlobal.g_anti_steal_token) {
  76. ClientGlobal.g_secret_key = secretKey;
  77. }
  78. }
  79. } catch (MyException e) {
  80. logger.error("FastDFS初始化失败: " + e.getMessage());
  81. }
  82. }
  83. @Bean
  84. public FastDFSClientPool fastDFSClientPool(){
  85. FastDFSClientPool clientPool = new FastDFSClientPool();
  86. clientPool.setMaxPoolSize(maxPoolSize);
  87. return clientPool;
  88. }
  89. @Bean
  90. public FastDFSHelper fastDFSUtil(){
  91. FastDFSHelper util = new FastDFSHelper();
  92. return util;
  93. }
  94. }