Java相关问题整理

2019-01-01 23:18:18来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

1. OkHttpClient调用出现大量CLOSE_WAIT。

问题定位:

  凡是系统中出现大量的CLOSE_WAIT,说明你的代码写的有问题,即:没有关闭连接。

在OkHttpClient中,默认时 HTTP头字段 Connection 设置值为keep-alive,这样会导致服务端断开连接时,客户端不能及时的断开连接,从而出现大量的CLOSE_WAIT。

问题修改:

  把HTTP头字段 Connection 设置值为close

 

如下是一段短连接http调用代码。

package com.xman.httpclient;

import okhttp3.*;
import org.springframework.stereotype.Service;

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

/**
 * Created by wangyaofu on 2018/4/25.
 */
@Service
public class HttpServiceImpl {

    public final static int CONNECT_TIMEOUT =10000;
    public final static int READ_TIMEOUT=10000;
    public final static int WRITE_TIMEOUT=10000;

    public String httpGet(String url, String userAgent) {
            final Request request = new Request.Builder()
                    .url(url).header("User-Agent", userAgent).header("Connection", "close")
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                    .build();
        Response response;
        try {
            response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            //logger.error("httpGet err=" + e.getMessage());
        }
        return null;
    }

    public String httpPost(String url, String body) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body);
        //创建一个请求对象
        Request request = new Request.Builder()
                .url(url).header("Connection", "close")
                .post(requestBody)
                .build();
        //发送请求获取响应
        try {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                    .build();
            Response response=okHttpClient.newCall(request).execute();
            if (response != null && response.body() != null) {
                return response.body().string();
            }
        } catch (Exception e) {
            //logger.error("httpPost err=" + e.getMessage());
        }
        return null;
    }
}

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java的3大特性

下一篇:SpringBoot(二十)Swagger2-自动生成RESTful规范API文档