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);    }}

转载博客地址


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 提前还贷需要什么手续 提前还贷利息怎么算 提前还贷违约金 公积金冲还贷 提前还贷计算器2019 装250个app还贷 还贷能力系数 公积金还贷计算器 美利车金融还贷规则 装250个app还贷欠55万 公积金逐年还贷是个坑 上海公积金还贷 美利车提前还贷不给证 房贷提前还贷计算器 提前还贷计算 银行提前还贷计算器 提前还贷计算器2017 提前还贷计算器在线计算 银行还贷计算器 购房还贷计算器 房屋还贷计算器 公积金逐月还贷 提前还贷利息计算器 购房能力评估计算器 提前还款计算器 大额还款计算器 提前还款利息怎么算 长贷短还计算器 商业贷款提前还款计算器 按揭提前还款计算器 提前还款计算器等额本金 汽车计算器 房贷提前还款计算器在线计算 等额本金房贷提前还款计算器 提前还房贷计算 已经贷款 房贷还款 贷款4000元 提前还贷款计算器 怎么还房贷 怎么房贷