具有URLConnection的Java应用程序导致“打开的文件太多" [英] Java app with URLConnection leads "Too many open files"

查看:145
本文介绍了具有URLConnection的Java应用程序导致“打开的文件太多"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一小段Java程序,如下所示:

I wrote a small pieces of java program as following:

package com.ny.utils.pub;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetWriter {
private static String link = "http://xxx.yyyyyy.com:4444";

public String getLink() {
    return link;
}

public static void setLink(String link) {
    NetWriter.link = link;
}

private static HttpURLConnection conn = null;
private static BufferedReader bufReader = null;
private static InputStreamReader isReader = null;
private static OutputStreamWriter osw = null;
private static URL url = null;
static {
    try {
        url = new URL(link);
    } catch(MalformedURLException e) {
    }
}

public static void write(String msg) {
    long threadId = Thread.currentThread().getId();
    System.out.println("--Insert>{" + threadId + "}:" + msg);
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", 
              "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Language", "en-US");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);

        osw = new OutputStreamWriter(conn.getOutputStream());
        osw.write(msg);
        osw.flush();
        osw.close();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.err.println("Server not return HTTP_OK status");
        } else {
            System.out.println(" request: " + msg);
            isReader = new InputStreamReader(conn.getInputStream());
            bufReader = new BufferedReader(isReader);
            String rep = bufReader.readLine();
            if (conn.getResponseCode() == 200) {
                System.out.println("Post data OK to " + link);
            }
            System.out.println(" response: " + rep);
        }
    } catch(IOException e) {
        System.err.println("Post data error: " + link + " "
                + e.getMessage());
        e.printStackTrace();
    }
}
}

当我编写了另一个程序来调用此类中的方法时,这将导致打开的文件太多",然后操作系统将拒绝用户登录.调用的脚本如下:

When I wrote another program to invoke method in this class, which will lead "Too many open files" and then the OS will refuse user to login. The called script is as following:

try{        
    NetWriter.write(new String(content, "utf-8")); 
}catch(Exception e){
    logger.error(e.getMessage());
    e.printStackTrace();
    }
}

问题再次出现时,我发现手柄占用量正在增加. 以下是我执行命令"lsof -p PROGRAM_PID"的消息

When the problem reappeared, I found that the handle occupation was increasing. The following is piece of message that I execute the command "lsof -p PROGRAM_PID"

java    27439 root   66u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   67u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   68u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   69r  FIFO                0,6             10151917 pipe
java    27439 root   70w  FIFO                0,6             10151917 pipe
java    27439 root   71r  0000               0,11          0  10151918 eventpoll
java    27439 root   72r  FIFO                0,6             10151919 pipe
java    27439 root   73w  FIFO                0,6             10151919 pipe
java    27439 root   74r  0000               0,11          0  10151920 eventpoll
java    27439 root   75u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   76u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   77r  FIFO                0,6             10152042 pipe
java    27439 root   78w  FIFO                0,6             10152042 pipe
java    27439 root   79r  0000               0,11          0  10152043 eventpoll
java    27439 root   80r  FIFO                0,6             10152044 pipe
java    27439 root   81w  FIFO                0,6             10152044 pipe
java    27439 root   82r  0000               0,11          0  10152045 eventpoll
java    27439 root   83u  unix 0xffff8103473fb6c0             10151765 socket
java    27439 root   84r  FIFO                0,6             10154168 pipe
java    27439 root   85w  FIFO                0,6             10154168 pipe
java    27439 root   86r  0000               0,11          0  10154169 eventpoll
java    27439 root   87r  FIFO                0,6             10154170 pipe

句柄(管道套接字事件池)的数量最多为数千.

Number of handles (pipe socket eventpool) will up to thousands.

我尝试了许多方法来避免这种情况,但是失败了. 有人可以告诉我上面程序的缺陷吗?

I tried many methods to avoid this, but failed. Anybody can tell me the defect in the above program?

推荐答案

您不关闭输入阅读器.它应该关闭.

You do not close the input reader. It should be closed.

通常,应关闭finally块中的资源.

As a general rule, you should close resources in a finally block.

在这种情况下,您应该在finally块中关闭输入和输出读取器.

In this case, you should close the input and output readers in a finally block.

这篇关于具有URLConnection的Java应用程序导致“打开的文件太多"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆