Test Object Pool : Object Pool « Apache Common « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Apache Common » Object PoolScreenshots 
Test Object Pool

import org.apache.commons.pool.impl.GenericObjectPool;

public class TestObjectPool {

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

    GenericObjectPool pool = new GenericObjectPool();
    pool.setFactory(new EmployeeFactory());

    /*First way of initializing pool
    pool.setMinIdle(5);
    pool.setTimeBetweenEvictionRunsMillis(500);
    Thread.currentThread().sleep(600);*/

    /* second, and preferred way */
    for(int i = 0; i < 5; ++i) {
      pool.addObject();
    }

    // pool.setTestOnReturn(true);
    pool.setMinEvictableIdleTimeMillis(1000);
    pool.setTimeBetweenEvictionRunsMillis(600);

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

        Thread.currentThread().sleep(1500);

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

    employee.setName("Fred Flintstone");
    employee.doWork();

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

    pool.returnObject(employee);

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


--------------------------------------------


public class Employee {

  private static int base = 0;


  private int id;

  private String name;

  public Employee() {
    this.id = base++;
  }

  public int getId() {
    return this.id;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void doWork() {
    // does nothing
  }

  public String toString() {
    return ("Id: " this.id + ", Name: " this.name);
  }

  public void finalize() {
    System.err.println("Employee " + toString() " made redundant");
  }
}

-----------------------------

import org.apache.commons.pool.BasePoolableObjectFactory;

public class EmployeeFactory extends BasePoolableObjectFactory {

  public Object makeObject() {
    return new Employee();
  }

  public boolean validateObject(Object obj) {
    if(obj instanceof Employee) {
      if(((Employee)obj).getName() == null)
        return true;
    }
    return false;
  }

  public void passivateObject(Object objthrows Exception {
    if(obj instanceof Employee) {
      ((Employee)obj).setName(null);
    else throw new Exception("Unknown object");
  }
}

---------------------------------------------
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;

public class SkilledEmployeeFactory extends BaseKeyedPoolableObjectFactory {

  public Object makeObject(Object key) {
    if(key == null || !(key instanceof String|| ((String)key).length() == 0)
      throw new IllegalArgumentException("Invalid key specified");
    return new SkilledEmployee(key.toString());
  }

  /*public boolean validateObject(Object key, Object obj) {
    if(obj instanceof Employee) {
      if(((Employee)obj).getName() == null)
        return true;
    }
    return false;
  }

  public void passivateObject(Object obj) throws Exception {
    if(obj instanceof Employee) {
      ((Employee)obj).setName(null);
    } else throw new Exception("Unknown object");
  }*/
}

-------------------------------------
public class SkilledEmployee extends Employee {

  private String skill;

  public SkilledEmployee(String skill) {
    this.skill = skill;
  }

  public String getSkill() {
    return this.skill;
  }

  public String toString() {
    return getSkill() " -- " + getName();
  }
}
           
       
TestObjectPool.zip( 1,218 k)
Related examples in the same category
1. Keyed Object Pool
2. Test Redundant Object Pool
3. Soft Reference Object Pool Demo
w__w___w___.__j__av_a_2___s._co___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.