com.baifendian.swordfish.common.hive.service2.HiveService2Client.java Source code

Java tutorial

Introduction

Here is the source code for com.baifendian.swordfish.common.hive.service2.HiveService2Client.java

Source

/*
 * Copyright (C) 2017 Baifendian Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baifendian.swordfish.common.hive.service2;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.hive.jdbc.HiveConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HiveService2Client {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private GenericKeyedObjectPool pool;

    /**
     * 
     */
    private int maxActive = 512;

    /**
     * 
     */
    private int maxIdle = 128;

    /**
     * 
     */
    private int minIdle = 0;

    /**
     * ?
     */
    private int maxWait = 2000;

    /**
     * -1 
     */
    private int timeBetweenEvictionRunsMillis = 180000;

    /**
     * ??? true
     */
    private boolean testWhileIdle = true;

    /**
     * hive 
     */
    private static HiveService2Client hiveService2Client;

    private HiveService2Client() {
        try {
            pool = bulidClientPool();
        } catch (Exception e) {
            logger.error("build client pool exception", e);
        }
    }

    /**
     * ?, ? hive 
     *
     * @return
     */
    public static HiveService2Client getInstance() {
        if (hiveService2Client == null) {
            synchronized (HiveService2Client.class) {
                if (hiveService2Client == null) {
                    hiveService2Client = new HiveService2Client();
                }
            }
        }

        return hiveService2Client;
    }

    /**
     *  hive 
     *
     * @return
     */
    protected GenericKeyedObjectPool bulidClientPool() {
        GenericKeyedObjectPool.Config poolConfig = new GenericKeyedObjectPool.Config();

        poolConfig.maxActive = maxActive;
        poolConfig.maxIdle = maxIdle;
        poolConfig.minIdle = minIdle;
        poolConfig.maxWait = maxWait;
        poolConfig.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        poolConfig.testWhileIdle = testWhileIdle;
        poolConfig.testOnBorrow = true;
        poolConfig.testOnReturn = true;

        HiveService2PoolFactory clientFactory = new HiveService2PoolFactory();

        return new GenericKeyedObjectPool(clientFactory, poolConfig);
    }

    /**
     * ? hive 
     *
     * @param hiveService2ConnectionInfo
     * @return
     * @throws Exception
     */
    public HiveConnection borrowClient(HiveService2ConnectionInfo hiveService2ConnectionInfo) throws Exception {
        return (HiveConnection) pool.borrowObject(hiveService2ConnectionInfo);
    }

    /**
     *  hive 
     *
     * @param hiveService2ConnectionInfo
     * @param client
     */
    public void returnClient(HiveService2ConnectionInfo hiveService2ConnectionInfo, HiveConnection client) {
        if (client != null) {
            try {
                pool.returnObject(hiveService2ConnectionInfo, client);
            } catch (Exception e) {
                logger.warn("HiveService2Client returnClient exception", e);
            }
        }
    }

    /**
     * ???
     *
     * @param hiveService2ConnectionInfo
     * @param client
     */
    public void invalidateObject(HiveService2ConnectionInfo hiveService2ConnectionInfo, HiveConnection client) {
        try {
            pool.invalidateObject(hiveService2ConnectionInfo, client);
        } catch (Exception e) {
            logger.error("HiveService2Client invalidateObject error", e);
        }
    }

    /**
     * 
     */
    public void clear() {
        pool.clear();
    }
}