Keyed Object Pool : Object Pool « Apache Common « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Apache Common » Object PoolScreenshots 
Keyed Object Pool

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

public class TestKeyedObjectPool {

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

    GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
    pool.setFactory(new SkilledEmployeeFactory());

    pool.addObject("Java");
    pool.addObject("Java");
    pool.addObject("VB");
    pool.addObject("C++");

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

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

    employee.doWork();

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

    pool.returnObject("Java", employee);

    System.err.println("Number of Java employees in pool: " +
      pool.getNumIdle("Java"" out of total employees: " +
      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.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();
  }
}
           
       
Related examples in the same category
1.Test Object Pool
2.Test Redundant Object Pool
3.Soft Reference Object Pool Demo
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.