| <?xml version="1.0"?> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| https://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <document xmlns="http://maven.apache.org/XDOC/2.0" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd"> |
| <properties> |
| <title>Examples</title> |
| <author email="dev@commons.apache.org">Apache Commons Documentation Team</author> |
| </properties> |
| |
| <body> |
| <section name="A Simple Pool Client"> |
| <p> |
| Suppose you're writing a set of <code>java.io.Reader</code> utilities, and would like to |
| provide a method for dumping the contents of a <code>Reader</code> to a <code>String</code>. |
| Here's the code for the <code>ReaderUtil</code>, implemented without an <code>ObjectPool</code>: |
| </p> |
| <source> |
| import java.io.Reader; |
| import java.io.IOException; |
| |
| public class ReaderUtil { |
| public ReaderUtil() { |
| } |
| |
| /** |
| * Dumps the contents of the {@link Reader} to a |
| * String, closing the {@link Reader} when done. |
| */ |
| public String readToString(Reader in) throws IOException { |
| StringBuffer buf = new StringBuffer(); |
| try { |
| for(int c = in.read(); c != -1; c = in.read()) { |
| buf.append((char)c); |
| } |
| return buf.toString(); |
| } catch(IOException e) { |
| throw e; |
| } finally { |
| try { |
| in.close(); |
| } catch(Exception e) { |
| // ignored |
| } |
| } |
| } |
| } |
| </source> |
| <p> |
| For the sake of this example, let's assume we want to pool the <code>StringBuffer</code>s |
| used to buffer the <code>Reader</code>'s contents. (A pool of <code>StringBuffer</code>s |
| may or may not be useful in practice. We're just using it as a simple example here.) |
| </p> |
| <p> |
| Let's further assume that a complete pool implementation will be provided via |
| a constructor. (We'll show you how to create such an implementation in just a moment.) |
| Then to use the pool we simply call <code>borrowObject</code> to obtain the buffer, and |
| then call <code>returnObject</code> when we're done with it. |
| Then a <code>ReaderUtil</code> implementation using a pool of <code>StringBuffer</code>s might look |
| like this: |
| </p> |
| <source> |
| import java.io.IOException; |
| import java.io.Reader; |
| import org.apache.commons.pool3.ObjectPool; |
| |
| public class ReaderUtil { |
| |
| private ObjectPool<StringBuffer> pool; |
| |
| public ReaderUtil(ObjectPool<StringBuffer> pool) { |
| this.pool = pool; |
| } |
| |
| /** |
| * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done. |
| */ |
| public String readToString(Reader in) |
| throws IOException { |
| StringBuffer buf = null; |
| try { |
| buf = pool.borrowObject(); |
| for (int c = in.read(); c != -1; c = in.read()) { |
| buf.append((char) c); |
| } |
| return buf.toString(); |
| } catch (IOException e) { |
| throw e; |
| } catch (Exception e) { |
| throw new RuntimeException("Unable to borrow buffer from pool" + e.toString()); |
| } finally { |
| try { |
| in.close(); |
| } catch (Exception e) { |
| // ignored |
| } |
| try { |
| if (null != buf) { |
| pool.returnObject(buf); |
| } |
| } catch (Exception e) { |
| // ignored |
| } |
| } |
| } |
| } |
| </source> |
| <p> |
| Since we've constrained ourselves to the <code>ObjectPool</code> interface, an arbitrary pool |
| implementation (returning, in our case, <code>StringBuffer</code>s) can be used. When a different |
| or "better" pool implementation comes along, we can simply drop it into our <code>ReaderUtil</code> |
| without changing a line of code. |
| </p> |
| </section> |
| <section name="A PooledObjectFactory"> |
| <p> |
| The implementations provided in pool2 wrap pooled objects in <code>PooledObject</code> |
| wrappers for internal use by the pool and object factories. The <code>PooledObjectFactory</code> |
| interface defines lifecycle methods for pooled objects. The simplest way to implement a |
| <code>PoolableObjectFactory</code> is to extend |
| <a href="./apidocs/org/apache/commons/pool2/BasePooledObjectFactory.html"> |
| <code>BasePooledObjectFactory</code></a>. |
| </p> |
| <p> |
| Here's a <code>PooledObjectFactory</code> implementation that creates |
| <code>StringBuffer</code>s as used above. |
| </p> |
| <source> |
| import org.apache.commons.pool3.BasePooledObjectFactory; |
| import org.apache.commons.pool3.PooledObject; |
| import org.apache.commons.pool3.impl.DefaultPooledObject; |
| |
| public class StringBufferFactory |
| extends BasePooledObjectFactory<StringBuffer> { |
| |
| @Override |
| public StringBuffer create() { |
| return new StringBuffer(); |
| } |
| |
| /** |
| * Use the default PooledObject implementation. |
| */ |
| @Override |
| public PooledObject<StringBuffer> wrap(StringBuffer buffer) { |
| return new DefaultPooledObject<StringBuffer>(buffer); |
| } |
| |
| /** |
| * When an object is returned to the pool, clear the buffer. |
| */ |
| @Override |
| public void passivateObject(PooledObject<StringBuffer> pooledObject) { |
| pooledObject.getObject().setLength(0); |
| } |
| |
| // for all other methods, the no-op implementation |
| // in BasePooledObjectFactory will suffice |
| } |
| </source> |
| <p> |
| We can, for example, use this factory with the <code>GenericObjectPool</code> to instantiate our |
| <code>ReaderUtil</code> as follows: |
| </p> |
| <source>ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool<StringBuffer>(new StringBufferFactory()));</source> |
| </section> |
| </body> |
| </document> |