Soft Reference Object Pool Demo : Object Pool « Apache Common « Java

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 Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Apache Common » Object PoolScreenshots 
Soft Reference Object Pool Demo

import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.SoftReferenceObjectPool;

import java.util.HashMap;
import java.lang.ref.SoftReference;

public class TestSoftRef extends TestCase {

  private SoftReferenceObjectPool pool;

  public static TestSuite suite() {
    return new TestSuite(TestSoftRef.class);
  }

  public static void main(String[] args) {
  String[] testClassName = TestSoftRef.class.getName() };
    junit.textui.TestRunner.main(testClassName);
  }

  public TestSoftRef(String s) {
    super(s);
  }

public void testOutOfMemory() throws Exception {

  Object obj = pool.borrowObject();
  pool.returnObject(obj);
  obj = null;

  assertEquals(5, pool.getNumIdle());

  try {
    HashMap map = new HashMap();

    for(int i = 0; i < 1000000; i++) {
      map.put(new Integer(i)new String("Fred Flintstone" + i));
    }
  }catch(OutOfMemoryError ex) {
      pool.borrowObject();
      pool.borrowObject();
      pool.borrowObject();
      pool.borrowObject();
      pool.borrowObject();
     // assertEquals(0, pool.getNumIdle());
  }
}

  public void setUp() throws Exception {
    this.pool = new SoftReferenceObjectPool(new PoolableObjectFactory() {
       int counter;
       public Object makeObject()                   { return String.valueOf(counter++)}
       public void destroyObjectObject obj )      {}
       public boolean validateObjectObject obj )  { return true}
       public void activateObjectObject obj )     {}
       public void passivateObjectObject obj )    {}
    }5);
  }
}


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

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();
  }
}

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

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");
  }
}

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

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");
  }
}
           
       
Related examples in the same category
1. Keyed Object Pool
2. Test Object Pool
3. Test Redundant Object Pool
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.