Progress Bar Bean

First let me credit this for William Louth.

I always wondered about the use of StatefulSessionBeans. Each and every time I used it, I would find a better way without using it.(Like use a serialized object and store the state in JSP sessions etc..). But after seeing this example, I could appreciate the use of a StatefulSessionBeans.

Problem

You are using StatelessSessionBeans (or some other facade) to access a large bunch of EntityBeans and you want to show a progress indicator for the user. These entity beans may also contain video or other data (just to emphasize the large amount of data is being requested by the use). You need to keep track of the total data being downloaded.

Solution

Use a session bean to get all the details as vectors. And create a stateful session bean to hold these vectors. Use this bean to iterate through the values. Return a reference to this bean to the user.

public class DetailIteratorBean implements SessionBean {
 private SessionContext _context;
 private Vector _vector;
 private int _pos = 0;
 private int _step = 10;
 private int _size = 0;
public void ejbCreate(Vector vector, int step) throws CreateException {
 _vector = new Vector(vector);
 _step = step;
 _size = vector.size();
 }
public void ejbRemove() {}

public void ejbActivate(){}

public void ejbPassivate(){}

public void setSessionContext(SessionContext context){ _context = context;}

public Vector next() {
 Vector vector = null;
 int size = Math.min(_step,_size-_pos);
 vector = new Vector(size);
 int index = _pos;
 for(int i=0; i < size; i++)
 vector.add(_vector.get(index++));
 _pos = ++index;
 return vector;
 }

public boolean hasMore() { return _vector.size() != 0; } public int size() { return _vector.size(); } public int step() { return _step; } public void step(int step) { _step = step;}
}

-- SeshKumar

EditText of this page (last edited July 27, 2008) or FindPage with title or text search