栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在GenericObjectPool中创建对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在GenericObjectPool中创建对象

您走在正确的轨道上。构造GenericObjectPool时,可以使用接受GenericObjectPoolConfig对象的构造函数,该对象包含对象池的所有配置值。下面的示例将使您的池在耗尽之前增加到20个连接…

GenericObjectPoolConfig config = new GenericObjectPoolConfig();config.setMinIdle(2);config.setMaxIdle(5);config.setMaxTotal(20);GenericObjectPool<Cipher> pool;CipherFactory factory = new CipherFactory(); this.pool = new GenericObjectPool<Cipher>(factory, config);

GenericeObjectPoolConfig还具有setBlockWhenExhausted方法,用于指定池达到maxTotal连接时的行为。有关详细信息,请参见https://commons.apache.org/proper/commons-
pool/apidocs/org/apache/commons/pool2/impl/baseObjectPoolConfig.html#setBlockWhenExhausted-
boolean-

我在使用Commons Pool时实现的一种模式是创建2个接口,一个用于您的池对象,一个用于您的工厂…

public interface PooledCipher extends java.io.Closeable {    byte[] doFinal(byte[] bytes) throws Exception;    SecretKeySpec getSecretKeySpec(byte[] bytes) throws Exception;}public interface CipherFactory {    PooledCipher getCipher() throws Exception; void close();}

CipherFactory的实现…

public class CipherFactoryImpl extends basePooledObjectFactory<PooledCipher>     implements CipherFactory {    private final GenericObjectPoolConfig config;    private final GenericObjectPool<PooledCipher> pool;    private final String transformation;    private final int opmode;    private final Key key;    private final AlgorithmParameters params;    private final String secretKeySpecAlgorithm;    public CipherFactoryImpl(GenericObjectPoolConfig config, String transformation, int opmode, Key key, AlgorithmParameters params, String secretKeySpecAlgorithm) {        this.config = config;        this.pool = new GenericObjectPool<PooledCipher>(this, config);        this.transformation = transformation;        this.opmode = opmode;        this.key = key;        this.params = params;    this.secretKeySpecAlgorithm = secretKeySpecAlgorithm    }    @Override    public PooledCipher create() throws Exception {        return new PooledCipherImpl(pool, transformation, opmode, key, params, secretKeySpecAlgorithm);    }    @Override    public PooledCipher getCipher() throws Exception {        return pool.borrowObject();    }    @Override    public void destroyObject(PooledObject<PooledCipher> p) throws Exception {        try { PooledCipherImpl cipherImpl = (PooledCipherImpl)p.getObject(); // do whatever you need with cipherImpl to destroy it        } finally { super.destroyObject(p);        }    }    @Override    public void close() {        pool.close();    }    @Override    public PooledObject<PooledCipher> wrap(PooledCipher cipher) {        return new DefaultPooledObject<PooledCipher>(cipher);    }}

PooledCipher实现…

public class PooledCipherImpl implements PooledCipher {    private final ObjectPool<PooledCipher> pool;    private final Cipher cipher;    private final String secretKeySpecAlgorithm;    private boolean destroyonClose = false;    public PooledCipherImpl(ObjectPool<PooledCipher> pool, String transformation, int opmode, Key key, AlgorithmParameters params, String secretKeySpecAlgorithm) {        this.pool = pool;        this.cipher = Cipher.getInstance(transformation);        this.cipher.init(opmode, key, params);        this.secretKeySpecAlgorithm = secretKeySpecAlgorithm;    }    @Override    public byte[] doFinal(byte[] bytes) throws Exception {        try { return cipher.doFinal(bytes);        } catch (Exception e) {destroyonClose = true;throw e;        }    }    @Override    public SecretKeySpec getSecretKeySpec(byte[] bytes) {        return new SecretKeySpec(doFinal(bytes), secretKeySpecAlgorithm);    }    @Override    public void close() throws IOException {        try { if (destroyOnClose) {     pool.destroyObject(this); } else {     pool.returnObject(this); }        } catch (Exception e) { throw new IOException(e);        }    }}

然后您像这样构造您的CipherFactory …

String transformation = "DESede/CBC/NoPadding";String secretKeySpecAlgorithm = "DESede";GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();// set up the poolConfig herepoolConfig.setMaxTotal(20);CipherFactory cipherFactory = new CipherFactoryImpl(poolConfig, transformation, Cipher.DECRYPT_MODE, mkkey, algParamSpec, secretKeySpecAlgorithm);

并像这样使用它…

public Key unwrapKey(byte[] tmkByte) throws Exception {    try (PooledCipher cipher = cipherFactory.getCipher()) {        return cipher.getSecretKeySpec(tmkByte);    }}

您也可以重用PooledCipher和CipherFactory接口来创建其他实现,例如JCA。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号