做项目时,需要使用到能够缓存对象的技术。开始想使用缓存,但是后来一想,对于这个功能使用缓存不适合,于是就想到了使用对象池。看到apache的commons-pool这个框架很好,就学习了一番。下面是对照着官方的例子,自己实现的一个小例子。
需要注意的是,这个是基于apache commons-pool 2.x版本的,如果你使用的是1.x版本的,那么应该看官网上1.x版本的例子,而不是看这个。因为2.x的更改挺大的,很多api的使用都改变了。
下面我将一步步介绍做这个例子的过程:
1.在maven中加入其依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.projecthome</groupId>
<artifactId>TestCommonsPool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestCommonsPool</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
</project>
2.新建一个User bean,位于com.projecthome.bean包下,这个bean是我们要在对象池中保存的对象:
package com.projecthome.bean;
import java.io.Serializable;
/**
*
* User entry.
*
* @author AlstonWilliams
* @date 2016/03/15
* @contact pshuyue@gmail.com
*
* */
public class User implements Serializable{
private String userName;
private String userPassword;
private String userTelephonenumber;
private String userEmail;
private String userRelation;
private int userProject;
private String userRole;
private String userPortrait;
private String userIntroduction;
private String userField;
private int userClass;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
if (userName == null) {
userName = "null";
}
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
if (userPassword == null) {
userPassword = "null";
}
this.userPassword = userPassword;
}
public String getUserTelephonenumber() {
return userTelephonenumber;
}
public void setUserTelephonenumber(String userTelephoneNumber) {
if (userTelephoneNumber == null) {
userTelephoneNumber = "null";
}
this.userTelephonenumber = userTelephoneNumber;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
if (userEmail == null) {
userEmail = "null";
}
this.userEmail = userEmail;
}
public String getUserRelation() {
return userRelation;
}
public void setUserRelation(String userRelation) {
if (userRelation == null) {
userRelation = "null";
}
this.userRelation = userRelation;
}
public int getUserProject() {
return userProject;
}
//此处应当注意处理NumberFormatException
public void setUserProject(String userProject) {
this.userProject = new Integer(userProject);
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
if (userRole == null) {
userRole = "null";
}
this.userRole = userRole;
}
public String getUserPortrait() {
return userPortrait;
}
public void setUserPortrait(String userPortrait) {
if (userPortrait == null) {
userPortrait = "null";
}
this.userPortrait = userPortrait;
}
public String getUserIntroduction() {
return userIntroduction;
}
public void setUserIntroduction(String userIntroduction) {
if (userIntroduction == null) {
userIntroduction = "null";
}
this.userIntroduction = userIntroduction;
}
public String getUserField() {
return userField;
}
public void setUserField(String userField) {
if (userField == null) {
userField = "null";
}
this.userField = userField;
}
public int getUserClass() {
return userClass;
}
//此处应当注意处理NumberFormatException
public void setUserClass(String userClass) {
this.userClass = new Integer(userClass);
}
}
3.继承BaseKeyedPooledObjectFactory类来实现池:
package com.projethome.objectpool;
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import com.projecthome.bean.User;
public class KeyUserObjectPoolFactory extends BaseKeyedPooledObjectFactory<String, User>{
@Override
public User create(String arg0) throws Exception {
return new User();
}
@Override
public PooledObject<User> wrap(User arg0) {
return new DefaultPooledObject<User>(arg0);
}
}
4.使用这个对象池来存储以及取出对象:
package com.projecthome.TestCommonsPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import com.projecthome.bean.User;
import com.projethome.objectpool.KeyUserObjectPoolFactory;
public class Main {
public static void main(String[] args) throws Exception{
GenericKeyedObjectPool<String, User> userPool = new GenericKeyedObjectPool<String, User>(new KeyUserObjectPoolFactory());
User user = userPool.borrowObject("pshuyue@gmail.com");
user.setUserClass("1");
user.setUserIntroduction("hello,逄淑越");
userPool.returnObject("pshuyue@gmail.com", user);
User user2 = userPool.borrowObject("baoqiyao@qq.com");
user2.setUserClass("2");
user2.setUserIntroduction("包琪瑶");
userPool.returnObject("baoqiyao@qq.com", user2);
user = userPool.borrowObject("baoqiyao@qq.com");
System.out.println(user.getUserClass());//输出2
System.out.println(user.getUserIntroduction());//输出"包琪瑶"
userPool.returnObject("baoqiyao@qq.com", user);
}
}
这个例子只是简单地实现了存取对象的功能,没有设置各种对象池的属性,只是使用的默认值。其实添加也很简单,各位可以自行试一下。
这个框架中文资料不多,2.x版本的资料更少。各位还是自行参照官方文档来学习,本身这个框架中类也不多,也就三四十个。