【优雅代码】08-构建自己的连接池

简介: 线程池的优势自不必多说,连接池和线程池有着众多相通之处,比较常见的连接池有druid、jedis等,但若是某些自研数据库等该如何构建自己的连接池就成问题。笔者使用http这一工具进行构建,可以对比效率差异。核心包为common-pool2

【优雅代码】08-构建自己的连接池

欢迎关注b站账号/公众号【六边形战士夏宁】,一个要把各项指标拉满的男人。该文章已在 github目录收录。
屏幕前的 大帅比大漂亮如果有帮助到你的话请顺手点个赞、加个收藏这对我真的很重要。别下次一定了,都不关注上哪下次一定。

1.背景

线程池的优势自不必多说,连接池和线程池有着众多相通之处,比较常见的连接池有druid、jedis等,但若是某些自研数据库等该如何构建自己的连接池就成问题。笔者使用http这一工具进行构建,可以对比效率差异。核心包为common-pool2

2.构建对象工厂

public class HttpCoonFactory extends BasePooledObjectFactory<HttpClient> {

    @Override
    public HttpClient create() throws Exception {
        // 和线程池一样的设计思路创建对象
        return HttpClients.createDefault();
    }

    @Override
    public PooledObject<HttpClient> wrap(HttpClient httpClient) {
        // 和线程池一样的设计思路,包装对象
        return new DefaultPooledObject<HttpClient>(httpClient);
    }
}

3.定制化参数

public class HttpPoolConfig extends GenericObjectPoolConfig {

    public HttpPoolConfig() {
        // 这里的配置和其它连接池基本一致,一脉相承的设计思路
        setMinIdle(5);
        setTestOnBorrow(true);
        setMaxTotal(50);
    }
}

4.构建连接池

public class HttpPoolManager extends GenericObjectPool<HttpClient> {
    private static HttpPoolManager httpPoolManager = new HttpPoolManager();

    public static HttpPoolManager getInstance() {
        // 将单例暴露出去
        return httpPoolManager;
    }
    private HttpPoolManager() {
        // 将配置注入到连接池内
        super(new HttpCoonFactory(), new HttpPoolConfig());
    }
}

5.构建工具类

@Slf4j
public class HttpUtil {

    /**
     * 发送get方法已改造成使用连接池
     * @param url
     * @return
     */
    public static String sendGet(String url) {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(RequestConfig.custom()
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .setSocketTimeout(3000)
                .build());
        CloseableHttpClient httpClient = null;
        try {
            httpClient = (CloseableHttpClient) HttpPoolManager.getInstance().borrowObject();
            try {
                @Cleanup CloseableHttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity);
                }
            } catch (IOException e) {
                log.warn(String.format("%s:%s",
                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                        e.getMessage()), e);
            }
        } catch (Exception e) {
            log.warn(String.format("%s:%s",
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    e.getMessage()), e);
        }finally {
            HttpPoolManager.getInstance().returnObject(httpClient);
        }
        return "";
    }

    /**
     * 发送post方法已改造成使用连接池
     * @param url
     * @return
     */
    public static String sendPost(String url, Map<String,String> paramsMap,Map<String, String> headMap){
        List<NameValuePair> formParams = new ArrayList<>();
        if(MapUtils.isNotEmpty(paramsMap)){
            for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
                formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(RequestConfig.custom()
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .setSocketTimeout(3000)
                .build());
        if(MapUtils.isNotEmpty(headMap)){
            for (Map.Entry<String, String> entry : headMap.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "utf-8"));
            @Cleanup CloseableHttpClient httpClient = HttpClients.createDefault();
            @Cleanup CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            log.warn(String.format("%s:%s",
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    e.getMessage()), e);
        }
        return "";
    }

    public static String sendPostJson(String url,String param,Map<String, String> headMap){
        StringEntity entity = new StringEntity(param,"utf-8");
        entity.setContentType(MediaType.APPLICATION_JSON_VALUE);
        entity.setContentEncoding("utf-8");
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(RequestConfig.custom()
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .setSocketTimeout(3000)
                .build());
        if(MapUtils.isNotEmpty(headMap)){
            for (Map.Entry<String, String> entry : headMap.entrySet()) {
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        try {
            httpPost.setEntity(entity);
            @Cleanup CloseableHttpClient httpClient = HttpClients.createDefault();
            @Cleanup CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entityResukt = response.getEntity();
            if (entityResukt != null) {
                return EntityUtils.toString(entityResukt);
            }
        } catch (IOException e) {
            log.warn(String.format("%s:%s",
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    e.getMessage()), e);
        }
        return "";
    }

    /**
     *
     * @author seal 876651109@qq.com
     * @date 2020/6/4 7:23 PM
     */
    public static String postFile(InputStream stream,String fileName,String requestUrl){
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(true);
            conn.setChunkedStreamingMode(1024 * 10000);
            conn.setRequestProperty("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
            @Cleanup OutputStream out = new DataOutputStream(conn.getOutputStream());
            IOUtils.copy(stream,out);
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

6.使用及对比

public static void main(String[] args) {
        String url = "http://www.baidu.com";
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("pool");
        for (int i = 0; i < 100; i++) {
            sendGet(url);
        }
        stopWatch.stop();
        stopWatch.start("common");
        for (int i = 0; i < 100; i++) {
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(RequestConfig.custom()
                    .setConnectTimeout(3000)
                    .setConnectionRequestTimeout(3000)
                    .setSocketTimeout(3000)
                    .build());
            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
                httpclient.execute(httpGet).getEntity();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
  • 结果如下,用连接池快了一倍,好处大大地
---------------------------------------------
ns         %     Task name
---------------------------------------------
1698114024  031%  pool
3696532228  069%  common
相关文章
解决开启子线程,导致request上下文和session信息丢失问题
解决开启子线程,导致request上下文和session信息丢失问题
1464 0
|
C# 开发者 Windows
C#开源的两款功能强大的录屏神器
ScreenToGif和ShareX是两款免费、开源的Windows截图和GIF制作工具,由C#开发。ScreenToGif适合教程制作和趣味GIF,而ShareX则提供灵活的截图及上传功能。两者都在GitHub上有源代码,相关介绍链接也已提供。此外,它们都已被收录进C#/.NET/.NET Core的优秀项目精选列表,以帮助开发者发现最新最佳实践。
193 6
|
SQL 监控 数据库
MSSQL性能调优实战:索引策略优化、SQL查询重写与高效并发管理的具体技巧
在Microsoft SQL Server(MSSQL)的性能调优过程中,索引策略的优化、SQL查询的重写以及高效并发管理是关键环节
|
供应链 监控 数据安全/隐私保护
ERP系统中的供应链风险管理与应对策略解析
【7月更文挑战第25天】 ERP系统中的供应链风险管理与应对策略解析
1126 0
|
存储 缓存 分布式计算
【大数据】计算引擎MapReduce
【大数据】计算引擎MapReduce
517 0
|
SQL 存储 弹性计算
阿里云RDS负责人彭祥:RDS On倚天ECS的技术演进
软硬协同优化,业务代码零改造,实现无缝迁移的同时降本增效
阿里云RDS负责人彭祥:RDS On倚天ECS的技术演进
|
敏捷开发 小程序 Java
【软件测试】软件测试基础概念总结
能够设计出高效的发现缺陷和保证产品质量的优秀测试用例 具有探索性思维,发散思维,对软件测试有浓厚的兴趣并且对工作有责任感和压力
【软件测试】软件测试基础概念总结
|
SQL Java 数据库连接
七十三、Spring与DAO操作 update()
七十三、Spring与DAO操作 update()
七十三、Spring与DAO操作 update()
|
4天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用