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

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

Introduction

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

Prototype

public synchronized void returnObject(final Object obj) throws Exception 

Source Link

Usage

From source file:TestRedundantObjectPool.java

 public static void main(String args[]) throws Exception {

   SoftReferenceObjectPool pool =
     new SoftReferenceObjectPool(new EmployeeFactory(), 5);

   try{/*w ww.  j av  a  2 s . c  om*/

      System.err.println("Number of employees in pool: " + pool.getNumIdle());

      Employee employee = (Employee)pool.borrowObject();

      System.err.println("Borrowed Employee: " + employee);

      employee.doWork();

      pool.returnObject(employee);

      // employee = null;

      HashMap map = new HashMap();

      System.err.println("Running memory intensive operation");
      for(int i = 0; i < 1000000; i++) {
         map.put(new Integer(i), new String("Fred Flintstone" + i));
      }

   }catch(OutOfMemoryError e) {
      System.err.println("Borrowed employee after OutOfMemory: " +
        pool.borrowObject());
      System.err.println("Error: "  + e);
   }
}

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

/**
 * Test method for//from   www .  j  a  va  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());
    // }

}