HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"An Object Pool Using Remote Method Invocation"
Volume 3, Issue 2, p. 56

	

Listing 1.
 
public class ObjectPool {  
 protected int size;  
 protected Vector in;  
 protected Vector out;  
 /* member class: */  
 public class PoolObject extends UnicastRemoteObject  
     implements Remote, Unreferenced {  
  public PoolObject() throws RemoteException { }  
  public void unreferenced() {  
   try { checkIn(this); // reclaim lost Object  
   } catch (Exception e) {}  
  }  
 }  

 /** constructor */  
 public ObjectPool(int size) {  
  this.size = size;  
  this.in = new Vector(size);  
  this.out = new Vector(size);  
 }  

 /** factory method to create new Object for the pool */  
 protected PoolObject newPoolObject() throws RemoteException {  
  return new PoolObject();  
 }  

 /** check out an Object from the pool */  
 public synchronized Object checkOut() {  
  Object o = null;  
  if (out.size() < size) {  
   if (in.isEmpty()) {  
    try {  
     o = newPoolObject();  
    }  
    catch (RemoteException e) {  
    }  
   }  
   else {  
    o = in.elementAt(0);  
    in.removeElementAt(0);  
   }  
   out.addElement(o);  
  }  
  return o;  
 }  

 /** check an Object back into the pool */  
 public synchronized void checkIn(Object o) {  
  int x = out.indexOf(o);   
   /* rely on Vector's use of equals() method  
   ** to equate stubs with remote objects if necessary  
   */  
  if (x < 0) throw new NoSuchElementException();  
  Object oo = out.elementAt(x);  
   /* get the real Object, not a stub */  
  out.removeElementAt(x);  
  in.addElement(oo);  
 }  
}  

Listing 2.
  
public XXPool extends ObjectPool {  
 class XXObject extends ObjectPool.PoolObject implements Remote {  
  ... // instance variables and methods for your Object  
  XXObject() throws RemoteException {}  
 }  
 public XXPool(int size) {  
  super(size);  
 }  
 protected PoolObject newPoolObject() throws RemoteException {  
  return new XXObject();  
 }  
}  

Listing 3.
  
public XXPool {  
 static public class XXObject implements Serializable {  
  private Object poolObject;  
  ... // instance variables and methods for your Object  
  XXObject(Object poolObject) {  
   this.poolObject = poolObject;  
  }  
 }  
 ObjectPool objectPool;  
 public XXPool(int size) {  
  this.objectPool = new ObjectPool(size);  
 }  
 public XXObject checkOut() {  
  Object o = objectPool.checkOut();  
  if (o == null) return null;  
  return new XXObject(o);  
 }  
 public void checkIn(XXObject o) {  
  objectPool.checkIn(o.poolObject);  
 }  
}
 
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.