FastDFSConfig.java 3.7 KB

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