初识apache commons-pool 2.x

做项目时,需要使用到能够缓存对象的技术。开始想使用缓存,但是后来一想,对于这个功能使用缓存不适合,于是就想到了使用对象池。看到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版本的资料更少。各位还是自行参照官方文档来学习,本身这个框架中类也不多,也就三四十个。


Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.getPoolConfig(LettuceConnectionConfiguration.java:207) The following method did not exist: 'void org.apache.commons.pool2.impl.GenericObjectPoolConfig.setMaxWait(java.time.Duration)' The calling method's class, org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory, was loaded from the following location: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/springframework/boot/spring-boot-autoconfigure/3.1.2/spring-boot-autoconfigure-3.1.2.jar!/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration$PoolBuilderFactory.class The called method's class, org.apache.commons.pool2.impl.GenericObjectPoolConfig, is available from the following locations: jar:file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar!/org/apache/commons/pool2/impl/GenericObjectPoolConfig.class The called method's class hierarchy was loaded from the following locations: org.apache.commons.pool2.impl.GenericObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.impl.BaseObjectPoolConfig: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar org.apache.commons.pool2.BaseObject: file:/D:/Developing%20learning%20software/apache-maven-3.9.2-bin/nfv/org/apache/commons/commons-pool2/2.6.0/commons-pool2-2.6.0.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory and org.apache.commons.pool2.impl.GenericObjectPoolConfig
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值