Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

This pattern is similar to the managed component pattern. But instead of creating and closing a resource it borrows from and returns to a pool. Pooling is provided by commons-pool.

@Name("saxParser")
@Scope(ScopeType.EVENT)
@AutoCreate
public class PooledSaxParser {

	private SAXParser parser;

	@In
	private ObjectPool saxParserPool;

	@Create
	public void create() throws Exception {
		parser = (SAXParser) saxParserPool.borrowObject();
	}

	@Destroy
	public void destroy() throws Exception {
		saxParserPool.returnObject(parser);
	}

	@Unwrap
	public SAXParser getParser() {
		return parser;
	}
}
	
@Name("saxParserPool")
@Scope(ScopeType.APPLICATION)
@AutoCreate
public class SaxParserPool {

	private GenericObjectPool pool;

	@Create
	public void create() {
		pool = new GenericObjectPool(new SaxParserPoolFactory());
	}

	@Unwrap
	public ObjectPool getPool() {
		return pool;
	}

	public class SaxParserPoolFactory extends BasePoolableObjectFactory {
		private SAXParserFactory saxParserFactory;

		public SaxParserPoolFactory() {
			saxParserFactory = SAXParserFactory.newInstance();
		}

		public Object makeObject() throws Exception {
			return saxParserFactory.newSAXParser();
		}
	}
}