PoolingÀ̶õ °ÍÀº performance °³¼±°ú reousrceÀÇ È¿À²ÀûÀÎ °ü¸®¸¦ À§ÇØ
ÀÌ¿ëµÇ¾îÁö°í, »ç¿ë¿¹·Î´Â ThreadPool, JDBC¿¡¼ ConnectionPool,
ObjectPoolµîÀÌ ÀÖ´Ù. À̹ø article¿¡¼´Â network programmingÀ» ÇÒ¶§
»ý¼ºÇÏ´Â Socket°´Ã¼¿¡ ´ëÇÑ pooling ¼Ò½º¸¦ ¼Ò°³ ÇÏ°Ú½À´Ï´Ù. PoolingÀ» ±¸Çö ÇÒ¶§
Áß¿ä ºÎºÐÀº Pooling´ë»ó °´Ã¼¸¦ ¾ò±âÀ§ÇØ Multi Thread°¡ Á¢±ÙÇÏ´Â °æ¿ì¿¡ ´ëÇÑ
Concurrency Issue¸¦ ÇØ°áÇØ¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù.
Socket pooling°´Ã¼´Â ´Ù¸¥ pooling °´Ã¼¿Í ¸¶Âù°¡Áö·Î
server side component·Î ÀÌ¿ëµÇ¾î Áö´Âµ¥, . servlet/jsp web
tier¿¡¼ ƯÁ¤ Ÿ system°ú socket Åë½ÅÀ» ÇØ¾ß ÇѴٰųª, EJB component°¡
ƯÁ¤ Ÿ system°ú Åë½ÅÀ» ÇؾßÇÒ °æ¿ì ÀÌ¿ëÇÒ¼ö ÀÖÁÒ. À§±×¸²°ú °°ÀÌ Æ¯Á¤ Ÿ ½Ã½ºÅÛÀÇ
Server Application¿¡ connectionÀ» ¿¬°áÇÏ°í ÀÖ´Â Socket°´Ã¼¸¦ ¹Ì¸® »ý¼ºÇØ
poolingÀ¸·Î °ü¸®Çϸé, ±×·¡¼, À§ ±×¸²ÀÇ client´Â servlet/jsp,ejbµîÀÇ
server side component¸¦ ÀǹÌÇÏ´Â °ÍÀÔ´Ï´Ù. ´ÙÀ½Àº SocketPool°´Ã¼¸¦ »ç¿ëÇÏ´Â
client¿¡´ëÇÑ sequenceÀÔ´Ï´Ù.
´ÙÀ½Àº SocketPool¿¡ ´ëÇÑ source ÄÚµåÀÌ´Ù.
package specular.net;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* SocketPool.java
* Jeon HongSeong
* 2001.07.04
*/
public final class SocketPool
{
//Singleton
private static SocketPool instance;
private ArrayList free;
private ArrayList used;
private int count;
//Configuration variable
private static String HOST;
private static int PORT;
private static int MAX_COUNT;
private static int INITIAL_COUNT;
private static int TIMEOUT;
private static boolean BLOCK;
static {
loadConf();
try {
instance = new SocketPool();
} catch(IOException e) {
e.printStackTrace();
}
}
//Constructor
private SocketPool() throws IOException {
free = new ArrayList();
used = new ArrayList();
while(count<INITIAL_COUNT) {
addSocket();
}
}
public static SocketPool getInstance()
throws IOException
{
if(instance==null) {
synchronized (SocketPool.class) {
if(instance==null) {
instance = new SocketPool();
}
}
}
return instance;
}
//Loading the configuration from pool.properties file.
private static void loadConf() {
ResourceBundle rb = ResourceBundle.getBundle("pool") ;
HOST = rb.getString("host");
PORT = Integer.parseInt(rb.getString("port"));
MAX_COUNT = Integer.parseInt(rb.getString("maxCons"));
INITIAL_COUNT = Integer.parseInt(rb.getString("initialCons"));
String timeout = rb.getString("timeout");
String block = rb.getString("block");
if(block!=null) {
BLOCK = Boolean.getBoolean(block);
TIMEOUT = Integer.parseInt(timeout);
}
System.out.println("Socket Pooling Configuration ****************");
System.out.println("Host : "+HOST);
System.out.println("Port : "+PORT);
System.out.println("Max_Count : "+MAX_COUNT);
System.out.println("Min_Count : "+INITIAL_COUNT);
System.out.println("BLOCK : "+BLOCK);
System.out.println("TIMEOUT : "+TIMEOUT);
System.out.println("---------------------------------------------");
}
public Socket getSocket()
throws IOException
{
return getSocket(BLOCK,TIMEOUT);
}
public synchronized Socket getSocket(boolean block, long timeout)
throws IOException
{
if(free.isEmpty()) {
if(count<MAX_COUNT) {
addSocket();
} else if(block) {
try {
synchronized(this) {
wait(timeout);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
if(free.isEmpty()) {
if(count<MAX_COUNT) {
addSocket();
} else {
throw new IOException("Timeout waiting "+
for a socket to be released.");
}
}
} else {
throw new IOException("Maximum number "+
of allowed Sockets reached");
}
}
Socket sck=null;
synchronized(used) {
sck = (Socket)free.remove(free.size()-1);
used.add(sck);
}
return sck;
}
public synchronized void release(Socket sck)
throws IOException
{
if(used.contains(sck)) {
int idx = used.indexOf(sck);
used.remove(idx);
free.add(sck);
} else {
throw new IOException("Socket " + sck +
" did not come from this SocketPool");
}
notify();
}
public synchronized void closeAll()
throws IOException
{
for(int i=0;i<free.size();i++) {
Socket sck = (Socket)free.remove(i);
try {
sck.close();
} catch(IOException e) {
System.out.println(e.toString());
}
}
for(int i=0;i<used.size();i++) {
used.remove(i);
}
}
private void addSocket()
throws IOException
{
Socket sck = new Socket(HOST,PORT);
free.add(sck);
count++;
}
} |
´ÙÀ½Àº SocketPool °´Ã¼°¡ ÀÌ¿ëÇÑ´Â Configuration ¼³Á¤ ÆÄÀÏÀÌ´Ù. ÀÌÆÄÀÏÀº
CLASSPATH°æ·Î¿¡ ÀÖÀ¸¸é µÈ´Ù.
host=127.0.0.1
port=8080
maxCons=100
initialCons=10
block=true
timeout=500 |
Socket°´Ã¼¿¡ ´ëÇÑ resource¸¦ È¿À² ÀûÀ¸·Î °ü¸® ÇÏ°íÀÚ ÇÑ´Ù¸é, Socket¿¡ ´ëÇÑ
»ç¿ëÀÌ ÀûÀ»°æ¿ì Socket°´Ã¼¸¦ close()½ÃÄÑÁÖ´Â Thread¸¦ Ãß°¡·Î Á¤ÀÇÇÏ¸é µÇ°ÚÁÒ.
2001.07.04 written by
Jeon HongSeong |