Java使用连接池访问FastDFS

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 {

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

转载博客地址


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值