Spring Boot 에서 사용한 유틸클래스여서 해당 카테고리에 넣었지만, Spring 에서도 적용 가능합니다!
아래의 유틸 클래스를 적용하여 생성한 클래스입니다.
BasicLogger 클래스
- https://velog.io/@bi-sz/Spring-Boot-BasicLogger-Class
Closeable
, JDBC
, Socket
등의 리소스를 안전하게 닫아 리소스 누수 를 방지하고, 애플리케이션의 안정성을 높이기 위해 ResourceCloseHelper 클래스를 생성해줍니다.
close(Closeable... resources
InputStream
, OutputStream
등..) 를 안전하게 닫습니다.closeDBObjects(Wrapper... objects)
ResultSet
, Statement
, Connection
)를 안전하게 닫습니다.closeSocketObjects(Socket socket, ServerSocket server)
closeSockets(Socket... sockets)
package com.liyo.cmm.util;
import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;
public class ResourceCloseHelper {
/**
* Resource close 처리.
*
* @param resources
*/
public static void close(Closeable... resources) {
for (Closeable resource : resources) {
if (resource != null) {
try {
resource.close();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ingored!!");
}
}
}
}
/**
* JDBC 관련 resource 객체 close 처리
*
* @param objects
*/
public static void closeDBObjects(Wrapper... objects) {
for (Object object : objects) {
if (object != null) {
if (object instanceof ResultSet) {
try {
((ResultSet) object).close();
} catch (SQLException ignore) {
BasicLogger.ignore("Occurred SQLException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ingored!!");
}
} else if (object instanceof Statement) {
try {
((Statement) object).close();
} catch (SQLException ignore) {
BasicLogger.ignore("Occurred SQLException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ingored!!");
}
} else if (object instanceof Connection) {
try {
((Connection) object).close();
} catch (SQLException ignore) {
BasicLogger.ignore("Occurred SQLException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ingored!!");
}
} else {
throw new IllegalArgumentException("Wrapper type is not found : " + object.toString());
}
}
}
}
/**
* Socket 관련 resource 객체 close 처리
*
* @param objects
*/
public static void closeSocketObjects(Socket socket, ServerSocket server) {
if (socket != null) {
try {
socket.shutdownOutput();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to shutdown ouput is ignored!!");
}
try {
socket.close();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ignored!!");
}
}
if (server != null) {
try {
server.close();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ignored!!");
}
}
}
/**
* Socket 관련 resource 객체 close 처리
*
* @param sockets
*/
public static void closeSockets(Socket... sockets) {
for (Socket socket : sockets) {
if (socket != null) {
try {
socket.shutdownOutput();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to shutdown ouput is ignored!!");
}
try {
socket.close();
} catch (IOException ignore) {
BasicLogger.ignore("Occurred IOException to close resource is ingored!!");
} catch (Exception ignore) {
BasicLogger.ignore("Occurred Exception to close resource is ignored!!");
}
}
}
}
}
JDBC와 Socket 관련 리소스 관리에 유용한 ResourceCloseHelper
클래스를 정리해보았습니다.