All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kischang.fastdfs.pool.ConnectionFactory Maven / Gradle / Ivy

The newest version!
package com.github.kischang.fastdfs.pool;

import com.github.kischang.fastdfs.FastDFSTemplateFactory;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;

/**
 * 链接创建
 *
 * @author KisChang
 * @version 1.0
 */
class ConnectionFactory extends BasePoolableObjectFactory {

    private FastDFSTemplateFactory factory;

    public ConnectionFactory(FastDFSTemplateFactory templateFactory) {
        this.factory = templateFactory;
    }

    public StorageClient makeObject() throws Exception {
        TrackerClient trackerClient = new TrackerClient(factory.getG_tracker_group());
        TrackerServer trackerServer = trackerClient.getConnection();
        return new StorageClient(trackerServer, null);
    }

    public void destroyObject(StorageClient obj) throws Exception {
        close(obj.getTrackerServer());
    }

    public boolean validateObject(StorageClient obj) {
        try {
            Socket socket = obj.getTrackerServer().getSocket();
            if (!socket.isConnected()) {
                return false;
            }
            if (socket.isClosed()) {
                return false;
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }


    private void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ignored) {
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy