基本对象池(org.apache.commons.pool.impl.GenericObjectPool)的使用

本文介绍如何利用Apache Commons Pool库中的GenericObjectPool类及PoolableObjectFactory接口实现对象池管理。通过具体示例代码,展示了如何自定义对象工厂WdzPoolableObjectFactory来创建和维护Student对象实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package impl_genericobjectpool_test;

/**
* <p>Title: 基本对象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
* <p>Description:
* 测试 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
* <p>基本对象池的使用,
* <LI>class TestGenericObjectPool 表示一个测试使用对象池的具体例子
* <LI>class WdzPoolableObjectFactory 表示1个自己定义的生成对象的工厂
* <LI>class Student 表示 需要使用对象池来维护的类</p>
* <p>
* 引用了 common-collcetions-2.1 ,commons-pool-1.1
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: netsky</p>
* @author wdz( hotmail =wdz123@hotmail.com)
* @version 1.0
* @see {@link TestGenericObjectPool},
* org.apache.commons.pool.impl.GenericObjectPool,org.apache.commons.pool.PoolableObjectFactory
*/

public class Student {
private String sex;
private String name;
private int studentid;
private int age;
public Student() {
}

public static void main(String[] args) {
Student student1 = new Student();

}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStudentid() {
return studentid;
}

public void setStudentid(int studentid) {
this.studentid = studentid;
}

public void clear() {
studentid = 0;
name = "";
age = 0;
sex = "";

}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String toString() {
return "id =" + studentid + ",name =" + name + ",age=" + age + ",sex=" +
sex;
}
}



Class WdzPoolableObjectFactory --code


package impl_genericobjectpool_test;

import org.apache.commons.pool.impl.*;
import org.apache.commons.pool.*;

/**
* <p>Title: 基本对象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
* <p>Description:
* 测试 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
* <p>基本对象池的使用,
* <LI>class TestGenericObjectPool 表示一个测试使用对象池的具体例子
* <LI>class WdzPoolableObjectFactory 表示1个自己定义的生成对象的工厂
* <LI>class Student 表示 需要使用对象池来维护的类</p>
* <p>
* 引用了 common-collcetions-2.1 ,commons-pool-1.1
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: netsky</p>
* @author wdz( hotmail =wdz123@hotmail.com)
* @version 1.0
*/


public class WdzPoolableObjectFactory
implements PoolableObjectFactory {

/**
* 创建对象实例。同时可以分配这个对象适用的资源。
* **/
public Object makeObject() throws Exception {
return new Student();
};

/**
* 销毁对象,实际上提供了释放对象占用资源的接口。
* destroyObject is invoked on every instance when it is being "dropped"
* from the pool (whether due to the response from validateObject,
* or for reasons specific to the pool implementation.)
* */
public void destroyObject(Object obj) throws Exception {
}

/***
* 这个方法一般在 activateObject 方法执行后调用
* 检查对象的有效性
* validateObject is invoked in an implementation-specific fashion to
* determine if an instance is still valid to be returned by the pool.
* It will only be invoked on an "activated" instance.
* **/
public boolean validateObject(Object obj) {
return true;
}

/**
* 激活一个对象。
* activateObject is invoked on every instance before it is returned from the pool.
* **/
public void activateObject(Object obj) throws Exception {
}

/**
* 挂起一个对象
* passivateObject is invoked on every instance when it is returned to the pool
* **/
public void passivateObject(Object obj) throws Exception {
if (obj instanceof Student) {
( (Student) obj).clear();
}
}
}



Class TestGenericObjectPool -- Code


package impl_genericobjectpool_test;

import org.apache.commons.pool.impl.*;
import org.apache.commons.pool.*;

/**
* <p>Title: 基本对象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
* <p>Description:
* 测试 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
* <p>基本对象池的使用,
* <LI>class TestGenericObjectPool 表示一个测试使用对象池的具体例子
* <LI>class WdzPoolableObjectFactory 表示1个自己定义的生成对象的工厂
* <LI>class Student 表示 需要使用对象池来维护的类</p>
* <p>
* 引用了 common-collcetions-2.1 ,commons-pool-1.1
* </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: netsky</p>
* @author wdz( hotmail =wdz123@hotmail.com)
* @version 1.0
*/

public class TestGenericObjectPool {
private void testBorrowObject(GenericObjectPool pool, int kk) {
try {
Student[] student = new Student[10];
for (int i = 0; i < 10; i++) {
student = (Student) (pool.borrowObject());
student.setStudentid(i * 10000 + kk);
student.setAge(i * 1000 + kk);
student.setName("wdzname" + i * 100 + kk);
}
for (int i = 0; i < 10; i++) {
System.out.println("student[" + i + "]=" + student);
pool.returnObject(student);
if (i == 9) {
//察看对象回到对象池的状态
System.out.println("****student[9]=" + student[9]);
}
}

}
catch (Exception ex) {
ex.printStackTrace();
}

}

public TestGenericObjectPool() {
org.apache.commons.pool.PoolableObjectFactory factory = new
WdzPoolableObjectFactory();
GenericObjectPool pool = new GenericObjectPool(factory, 4000 * 10,
GenericObjectPool.WHEN_EXHAUSTED_GROW, 3000 * 10, true, true);
System.out.println("group1";
testBorrowObject(pool, 1);
System.out.println("group2";
testBorrowObject(pool, 2);

}

public static void main(String[] args) {
TestGenericObjectPool test = new TestGenericObjectPool();
}
}
java.lang.NoClassDefFoundError: Could not initialize class io.netty.util.internal.PlatformDependent0 at io.netty.util.internal.PlatformDependent.getSystemClassLoader(PlatformDependent.java:694) at io.netty.channel.nio.NioEventLoop$4.run(NioEventLoop.java:171) at java.base/java.security.AccessController.doPrivileged(AccessController.java:319) at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:167) at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:149) at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:102) at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:64) at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:49) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:70) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:65) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:56) at org.apache.rocketmq.remoting.netty.NettyRemotingClient.<init>(NettyRemotingClient.java:122) at org.apache.rocketmq.client.impl.MQClientAPIImpl.<init>(MQClientAPIImpl.java:188) at org.apache.rocketmq.client.impl.factory.MQClientInstance.<init>(MQClientInstance.java:133) at org.apache.rocketmq.client.impl.MQClientManager.getOrCreateMQClientInstance(MQClientManager.java:53) at org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl.start(DefaultMQAdminExtImpl.java:120) at org.apache.rocketmq.tools.admin.DefaultMQAdminExt.start(DefaultMQAdminExt.java:145) at org.apache.rocketmq.dashboard.admin.MQAdminFactory.getInstance(MQAdminFactory.java:53) at org.apache.rocketmq.dashboard.admin.MQAdminPooledObjectFactory.makeObject(MQAdminPooledObjectFactory.java:35) at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObject
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值