fastdfs java 连接池_软件研发-Java-连接池(FastDFS初稿)

1 packagecom.ccqtgb;2

3 importjava.io.IOException;4 importjava.net.InetSocketAddress;5 importjava.util.concurrent.ArrayBlockingQueue;6 importjava.util.concurrent.ConcurrentHashMap;7 importjava.util.concurrent.TimeUnit;8

9 importorg.csource.fastdfs.ClientGlobal;10 importorg.csource.fastdfs.StorageClient;11 importorg.csource.fastdfs.StorageClient1;12 importorg.csource.fastdfs.StorageServer;13 importorg.csource.fastdfs.TrackerClient;14 importorg.csource.fastdfs.TrackerGroup;15 importorg.csource.fastdfs.TrackerServer;16

17 public classConnectionPool {18

19 //最大连接数,可以写配置文件

20 private int size = 5;21 //被使用的连接

22 private ConcurrentHashMap busyConnectionPool = null;23 //空闲的连接

24 private ArrayBlockingQueue idleConnectionPool = null;25

26 private Object obj = newObject();27

28 private static ConnectionPool instance = newConnectionPool();29

30 public staticConnectionPool getConnectionPool(){31 returninstance;32 }33

34 //取出连接

35 public StorageClient1 checkout(intwaitTime){36 StorageClient1 storageClient1 = null;37 try{38 storageClient1 =idleConnectionPool.poll(waitTime, TimeUnit.SECONDS);39 System.out.println(storageClient1);40 if(storageClient1 != null){41 busyConnectionPool.put(storageClient1, obj);42 }43 } catch(InterruptedException e) {44 //TODO Auto-generated catch block

45 storageClient1 = null;46 e.printStackTrace();47 }48 returnstorageClient1;49 }50

51 //回收连接

52 public voidcheckin(StorageClient1 storageClient1){53 if(busyConnectionPool.remove(storageClient1) != null){54 idleConnectionPool.add(storageClient1);55 }56 }57

58 //如果连接无效则抛弃,新建连接来补充到池里

59 public voiddrop(StorageClient1 storageClient1){60 if(busyConnectionPool.remove(storageClient1) != null){61 TrackerServer trackerServer = null;62 TrackerClient trackerClient = newTrackerClient();63 try{64 trackerServer =trackerClient.getConnection();65 StorageClient1 newStorageClient1 = new StorageClient1(trackerServer,null);66 idleConnectionPool.add(newStorageClient1);67 System.out.println(“————————- :connection +1”);68 } catch(IOException e) {69 //TODO Auto-generated catch block

70 e.printStackTrace();71 }finally{72 if(trackerServer != null){73 try{74 trackerServer.close();75 } catch(IOException e) {76 //TODO Auto-generated catch block

77 e.printStackTrace();78 }79 }80 }81 }82 }83

84 //单例

85 privateConnectionPool(){86 busyConnectionPool = new ConcurrentHashMap();87 idleConnectionPool = new ArrayBlockingQueue(size);88 init(size);89 }90

91 //初始化连接池

92 private void init(intsize){93 initClientGlobal();94 TrackerServer trackerServer = null;95 try{96 TrackerClient trackerClient = newTrackerClient();97 //只需要一个tracker server连接

98 trackerServer =trackerClient.getConnection();99 StorageServer storageServer = null;100 StorageClient1 storageClient1 = null;101 for(int i=0; i

107 } catch(IOException e) {108 //TODO Auto-generated catch block

109 e.printStackTrace();110 }finally{111 if(trackerServer != null){112 try{113 trackerServer.close();114 } catch(IOException e) {115 //TODO Auto-generated catch block

116 e.printStackTrace();117 }118 }119 }120 }121

122 //初始化客户端

123 private voidinitClientGlobal(){124 //连接超时时间

125 ClientGlobal.setG_connect_timeout(2000);126 //网络超时时间

127 ClientGlobal.setG_network_timeout(3000);128 ClientGlobal.setG_anti_steal_token(false);129 //字符集

130 ClientGlobal.setG_charset(“UTF-8”);131 ClientGlobal.setG_secret_key(null);132 //HTTP访问服务的端口号

133 ClientGlobal.setG_tracker_http_port(8080);134

135 InetSocketAddress[] trackerServers = new InetSocketAddress[2];136 trackerServers[0] = new InetSocketAddress(“10.64.2.171”,22122);137 trackerServers[1] = new InetSocketAddress(“10.64.2.172”,22122);138 TrackerGroup trackerGroup = newTrackerGroup(trackerServers);139 //tracker server 集群

140 ClientGlobal.setG_tracker_group(trackerGroup);141 }142

143

144 }

来源:weixin_39722692

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月13日
下一篇 2021年1月13日

相关推荐