Gog*_*1nA 5 java sockets linux connection-pooling exception
我有在JAVA(版本1.8)上编写的Web服务,它连接HSM并通过套接字发送/接收数据.我的应用程序部署在Apache Tomcat/8.5.14
上面linux
.
虽然我正在关闭套接字连接,但我有
java.net.SocketException:打开的文件太多
这是myclass
public class myClass implements AutoCloseable {
Socket socket;
DataInputStream in;
DataOutputStream out;
public myClass(String ip, int port) throws Exception {
try {
socket = new Socket(ip, port);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (IOException e) {
throw new Exception("Connecting to HSM failed" + e);
}
}
public String sendCommandToHsm(String command) throws IOException {
out.writeUTF(command);
out.flush();
return in.readUTF();
}
@Override
public void close() {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
lgg.info("Closing of socket failed", e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
lgg.info("Closing of inputStream failed", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
lgg.info("Closing of outputStream failed", e);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我班上的用法
try (MyClass myClass = new MyClass(ip, port);) {
myClass.sendCommandToHsm("my command");
}
Run Code Online (Sandbox Code Playgroud)
我将服务器上的最大打开文件限制从默认值(1024)增加到8192,之后几次Exception
再次发生同样的情况.
我正在考虑创作Socket Connection Pool
,这是个好主意吗?
你能建议其他解决方案吗?
归档时间: |
|
查看次数: |
730 次 |
最近记录: |