Java使用连接池访问FastDFS
来源:互联网 发布:java调用http接口json 编辑:程序博客网 时间:2024/06/04 19:03
Java使用连接池访问FastDFS
连接池代码
package com.zeunpro.server.fileupload.utils;import java.io.IOException;import java.net.InetSocketAddress;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.TimeUnit;import com.zeunpro.server.fileupload.csource.ClientGlobal;import com.zeunpro.server.fileupload.csource.fastdfs.StorageClient1;import com.zeunpro.server.fileupload.csource.fastdfs.StorageServer;import com.zeunpro.server.fileupload.csource.fastdfs.TrackerClient;import com.zeunpro.server.fileupload.csource.fastdfs.TrackerGroup;import com.zeunpro.server.fileupload.csource.fastdfs.TrackerServer;public class ConnectionPool { //连接实例的限制 private int size = 5; //繁忙的连接实例 private ConcurrentHashMap<StorageClient1, Object> busyConnectionPool = null; //空闲连接实例 private ArrayBlockingQueue<StorageClient1> idleConnectionPool = null; private final static String tracker_server = "192.168.15.233"; private final static int port = 22122; private Object obj = new Object(); //singleton private ConnectionPool() { busyConnectionPool = new ConcurrentHashMap<StorageClient1, Object>(); idleConnectionPool = new ArrayBlockingQueue<StorageClient1>(size); init(size); }; private static ConnectionPool instance = new ConnectionPool(); //获取连接池实例 public static ConnectionPool getPoolInstance() { return instance; } //初始化连接池 private void init(int size) { initClientGlobal(); TrackerServer trackerServer = null; try { TrackerClient trackerClient = new TrackerClient(); // Only tracker trackerServer = trackerClient.getConnection(); for (int i = 0; i < size; i++) { StorageServer storageServer = null; StorageClient1 client1 = new StorageClient1(trackerServer, storageServer); idleConnectionPool.add(client1); } } catch (IOException e) { e.printStackTrace(); } finally { if (trackerServer != null) { try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 1. 从idleConnectionPool弹出一个连接, // 2. 将连接推入busyConnectionPool; // 3. 返回连接 // 4. 如果没有空闲连接,请等待wait_time秒,然后再次检查 public StorageClient1 checkout(int waitTimes) throws InterruptedException { StorageClient1 client1 = idleConnectionPool.poll(waitTimes, TimeUnit.SECONDS); busyConnectionPool.put(client1, obj); return client1; } // 1. 从busyConnectionPool弹出连接; // 2. 将连接推入idleConnectionPool; // 3. 做必要的清理工作。 public void checkin(StorageClient1 client1) { if (busyConnectionPool.remove(client1) != null) { idleConnectionPool.add(client1); } } // 所以如果连接因某些错误而中断(比如:套接字初始化失败,网络中断等),请删除此连接 // 从busyConnectionPool,并初始化一个新的连接。 public void drop(StorageClient1 client1) { if (busyConnectionPool.remove(client1) != null) { TrackerServer trackerServer = null; try { TrackerClient trackerClient = new TrackerClient(); // TODO 此处有内存泄露,因为trackerServer没有关闭连接 trackerServer = trackerClient.getConnection(); StorageServer storageServer = null; StorageClient1 newClient1 = new StorageClient1(trackerServer, storageServer); idleConnectionPool.add(newClient1); } catch (IOException e) { e.printStackTrace(); } finally { if (trackerServer != null) { try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } } } } private void initClientGlobal() { InetSocketAddress[] trackerServers = new InetSocketAddress[1]; trackerServers[0] = new InetSocketAddress(tracker_server, port); ClientGlobal.setG_tracker_group(new TrackerGroup(trackerServers)); // 连接超时的时限,单位为毫秒 ClientGlobal.setG_connect_timeout(2000); // 网络超时的时限,单位为毫秒 ClientGlobal.setG_network_timeout(30000); ClientGlobal.setG_anti_steal_token(false); // 字符集 ClientGlobal.setG_charset("UTF-8"); ClientGlobal.setG_secret_key(null); }}
上传代码
package com.zeunpro.server.fileupload.utils;import java.net.InetSocketAddress;import com.zeunpro.server.fileupload.csource.ClientGlobal;import com.zeunpro.server.fileupload.csource.fastdfs.StorageClient1;import com.zeunpro.server.fileupload.csource.fastdfs.TrackerGroup;public class FileUpload { private final static String tracker_server = "192.168.15.233"; private final static int port = 22122; public FileUpload() { init(); } public String fileUpload(byte[] fileBuff, String fileExtName) throws Exception { String address = null; StorageClient1 client1 = null; try { client1 = ConnectionPool.getPoolInstance().checkout(10); address = client1.upload_file1(fileBuff, fileExtName, null); ConnectionPool.getPoolInstance().checkin(client1); } catch (InterruptedException e) { // 确实没有空闲连接,并不需要删除与fastdfs连接 throw e; } catch (Exception e) { // 发生io异常等其它异常,默认删除这次连接重新申请 ConnectionPool.getPoolInstance().drop(client1); e.printStackTrace(); throw e; } return address; } private void init() { InetSocketAddress[] trackerServers = new InetSocketAddress[1]; trackerServers[0] = new InetSocketAddress(tracker_server, port); ClientGlobal.setG_tracker_group(new TrackerGroup(trackerServers)); // 连接超时的时限,单位为毫秒 ClientGlobal.setG_connect_timeout(2000); // 网络超时的时限,单位为毫秒 ClientGlobal.setG_network_timeout(30000); ClientGlobal.setG_anti_steal_token(false); // 字符集 ClientGlobal.setG_charset("UTF-8"); ClientGlobal.setG_secret_key(null); }}
转载博客地址
阅读全文