码迷,mamicode.com
首页 > 编程语言 > 详细

Java相关问题整理

时间:2018-12-29 13:45:01      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:ons   sage   create   java   head   logger   log   wan   cti   

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;
    }
}

  

Java相关问题整理

标签:ons   sage   create   java   head   logger   log   wan   cti   

原文地址:https://www.cnblogs.com/voipman/p/10195164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!