Example usage for org.apache.commons.pool.impl SoftReferenceObjectPool setFactory

List of usage examples for org.apache.commons.pool.impl SoftReferenceObjectPool setFactory

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl SoftReferenceObjectPool setFactory.

Prototype

public synchronized void setFactory(final PoolableObjectFactory factory) throws IllegalStateException 

Source Link

Usage

From source file:org.codelabor.system.remoting.tcp.factories.SocketPoolTest.java

/**
 * Test method for/*from  w ww.  j ava  2 s . c o m*/
 * {@link org.codelabor.system.remoting.tcp.factories.SocketPoolFactory#makeObject()}
 * .
 * 
 * @throws Exception
 * @throws IllegalStateException
 * @throws NoSuchElementException
 */
@Test
public void testSoftReferenceObjectPool() throws Exception {
    SoftReferenceObjectPool socketPool = new SoftReferenceObjectPool();
    SocketPoolFactory socketPoolFactory = new SocketPoolFactory();
    socketPoolFactory.setHost("localhost");
    socketPoolFactory.setPort(8080);
    socketPool.setFactory(socketPoolFactory);
    Socket sockets[] = new Socket[10];

    for (int i = 0; i < 10; i++) {
        sockets[i] = (Socket) socketPool.borrowObject();
        System.out.println("borrowObject: " + sockets[i].hashCode());
        System.out.println("active: " + socketPool.getNumActive() + ", idle: " + socketPool.getNumIdle());

    }
    for (int i = 0; i < 10; i++) {
        System.out.println("returnbject: " + sockets[i].hashCode());
        socketPool.returnObject(sockets[i]);
        sockets[i] = null;
        System.out.println("active: " + socketPool.getNumActive() + ", idle: " + socketPool.getNumIdle());
    }
    // while (true) {
    // Thread.sleep(1000);
    // System.out.println("active: " + socketPool.getNumActive()
    // + ", idle: " + socketPool.getNumIdle());
    // }

}