简单fastDFS连接池和客户端实现

fastdfs连接池实现:
import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;


/**
 * @Description:
 * @author:lgr
 * @CreateDate:2020.08.09 14:15
 */
public class FastdfsConnetionPool {


    /**
     * 网络请求超时时长
     */
    private static String networkTimeOut="1500";

    /**
     * 连接超时时长
     */
    private static String connectTimeout="600";

    /**
     * fastdfs服务地址
     */
    private static String fastDfsIp="127.0.0.1";

    /**
     * tracker端口号
     */
    private static String fastDfsPort="22122";


    /**
     * 默认连接池大小
     */
    public static String  connection_size = "10";

    /**
     * storageClient队列
     */
    private static LinkedBlockingQueue<StorageClient> storageClientQueue = new LinkedBlockingQueue<>(
            Integer.parseInt(connection_size));

    /**
     * 当前索引
     */
    private static int current_index;


    private static TrackerClient trackerClient;



    private static Properties properties=new Properties();

    /**
     * 初始化
     */
    static {
        //LoggerUtil.info("begin init FastDfs");
        properties.put("fastdfs.network_timeout_in_seconds",networkTimeOut);
        properties.put("fastdfs.connect_timeout_in_seconds",connectTimeout);
        properties.put("fastdfs.tracker_servers",fastDfsIp+":"+fastDfsPort);
        try {
            ClientGlobal.initByProperties(properties);
            trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
            //LoggerUtil.info("end init FastDfs");
        } catch (IOException e) {
            //LoggerUtil.info("init fastDFSConnectPool catch exception:{}",e);
        } catch (MyException e) {
            //LoggerUtil.info("init fastDFSConnectPool catch exception:{}",e);
        }
    }

    /**
     * 创建StorageClient
     */
    public static void createStorageClient() {
        synchronized (trackerClient) {
            if (current_index < Integer.parseInt(connection_size)) {
                try {
                    TrackerServer trackerServer = trackerClient.getConnection();
                    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
                    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
                    if (storageClientQueue.offer(storageClient)) {
                        current_index++;
                    }
                } catch (IOException e) {
                    //LoggerUtil.info("createStorageClient catch exception:{}",e);
                }
            }
        }
    }

    /**
     * 获取StorageClient
     * @return
     */
    public static StorageClient findStorageClient() {
        // 尝试获取一个有用的客户端连接信息
        StorageClient clientInfo = storageClientQueue.poll();
        if (clientInfo == null) {
            if (current_index < Integer.parseInt(connection_size)) {
                createStorageClient();
            }
            try {
                clientInfo= storageClientQueue.poll(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                //LoggerUtil.info("findStorageClient catch exception:{}",e);
            }
        }
        return clientInfo;
    }

    /**
     * 回收资源
     * @param storageClient
     */
    public static void recycleStorageClient(StorageClient storageClient){
        try {
            if(storageClient != null){
                storageClientQueue.offer(storageClient);
            }
        } catch (Exception e) {
            //LoggerUtil.info("recycleStorageClient catch exception:{}",e);
        }
    }






}

fastdfs客户端实现:



import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author:lgr
 * @CreateDate:2020.06.28 19:16
 */


public class FastDfsClient {

    /**
     * 上传文件
     * @param content
     * @param fileExtName
     * @param valuePairs
     * @return
     */
    public static String[] upload(byte[] content, String fileExtName, NameValuePair[] valuePairs) {
        StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
        String[] uploadResults = null;
        try {
            uploadResults = storageClient.upload_file(content, fileExtName, valuePairs);
        } catch (Exception e) {
            //LoggerUtil.info("upload catch exception:{}", e);
        } finally {
            FastdfsConnetionPool.recycleStorageClient(storageClient);
        }
        return uploadResults;
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public static void deleteFile(String groupName,String fileUrl){
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            final int group = storageClient.delete_file(groupName, fileUrl);
        } catch (IOException e) {
            //LoggerUtil.info("deleteFile catch exception:{}", e);
        } catch (Exception e) {
            //LoggerUtil.info("deleteFile catch exception:{}", e);
        }
    }

    /**
     * 文件下载
     * @param fileUrl
     * @return
     */
    public static byte[] download(String groupName,String fileUrl){
        byte[] group1s = null;
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            group1s = storageClient.download_file(groupName, fileUrl);
        } catch (IOException e) {
            //LoggerUtil.info("download catch exception:{}", e);
        } catch (Exception e) {
            //LoggerUtil.info("download catch exception:{}", e);
        }
        return group1s;
    }

    /**
     * 获取文件元数据
     * @param fileId 文件ID
     * @return
     */
    public static Map<String,String> getFileMetadata(String groupName, String fileId) {
        try {
            StorageClient storageClient = FastdfsConnetionPool.findStorageClient();
            NameValuePair[] metaList = storageClient.get_metadata(groupName,fileId);
            if (metaList != null) {
                HashMap<String,String> map = new HashMap<String, String>();
                for (NameValuePair metaItem : metaList) {
                    map.put(metaItem.getName(),metaItem.getValue());
                }
                return map;
            }
        } catch (Exception e) {
            //LoggerUtil.info("getFileMetadata catch exception:{}", e);
        }
        return null;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值