Helllo
I setup object pooling using Jakarta Commons. I developed a test Bean and Client. The test bean has an int instance variable which is initialized to -1. When it is first retrieved from the pool it is set to a value equal to the count in a loop only when it is less than 0.
/*
* Created on Jul 14, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.winterthur.test.web.spring;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
/**
* @author ddla83
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestPooledBean implements PooledInterface{
private GregorianCalendar startCalendar;
private GregorianCalendar endCalendar;
private int indexNumber = -1;
private DateFormat df;
public TestPooledBean(){
super();
df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss:SSS Z");
startCalendar = new GregorianCalendar();
System.out.println("Constructor start time: " + df.format(startCalendar.getTime()));
}
public String getNowTime(){
return new String("Start time: " + df.format((new GregorianCalendar()).getTime()));
}
public String outputContextualData(){
return new String("");
}
public void setIndexNumber(int index){
if (this.indexNumber < 0){
this.indexNumber = index;
}
}
public int getIndexNumber(){
return this.indexNumber;
}
public void finalize(){
endCalendar = new GregorianCalendar();
System.out.println("End time: " + df.format(endCalendar.getTime()));
}
}
The client has a for loop which loops 10 times. In the loop, the beans are put into a vector and then later they are taken off and traces are put out.
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t(getServletContext());
CommonsPoolTargetSource cpts = (CommonsPoolTargetSource) ctx.getBean("poolTargetSource");
int count = 10;
Vector holder = new Vector();
for (int i=0; i < count; i++){
PooledInterface tpb = (TestPooledBean)ctx.getBean("pool");
int setIndexNumber = i + 1;
tpb.setIndexNumber(setIndexNumber);
System.out.println("The counter is: " + i);
System.out.println("The index number is:" + tpb.getIndexNumber());
holder.add(tpb);
}
System.out.println(cpts.getIdleCount());
for (int i = 0; i < count; i++){
PooledInterface pi = (PooledInterface)holder.get(i);
System.out.println("This is a test of the pooled objects start time.");
System.out.println("/n Object number: " + pi.getIndexNumber() + " the check object now time is: " + pi.getNowTime());
}
However, all the beans have an index of 1, which seems to indicate to me that they are the same bean. My expectation is that inside the client for loop, when a bean is requested that a new one be delivered unless it has been explicitly returned to the pool, which I do after the snippet of code I show above. Also, it appears that all the beans have the same start time, which again suggests I am not getting multiple instances but rather the same one. The bean is set to singleton="false". Yesterday, several members of the forum help me get this up and running successfully, but I don't think the behavior is quite right.
Ideas? Thanks for the help.
David


Reply With Quote
.