专业做网站制作paypal网站做外贸
news/
2025/9/22 23:51:02/
文章来源:
专业做网站制作,paypal网站做外贸,小程序首页模板,做网站邯郸我发现org.apache.commons.pool非常有用且健壮#xff0c;但没有充分记录。 因此#xff0c;我将在这里帮助您解释如何使用Apache KeyedObjectPool 。 什么是KeyedObjectPool #xff1f; 它是一个映射#xff0c;其中包含多种类型的实例池。 可以使用任意键访问每种类型。… 我发现org.apache.commons.pool非常有用且健壮但没有充分记录。 因此我将在这里帮助您解释如何使用Apache KeyedObjectPool 。 什么是KeyedObjectPool 它是一个映射其中包含多种类型的实例池。 可以使用任意键访问每种类型。 在此示例中我将创建一个JSch ssh连接池并将使用一个名为ServerDetails的简单getter setter对象作为键。 基本上对于每个服务器我希望有10个可重用的ssh连接池。 因此首先要做的是创建一个Sessionfactory一个负责创建要存储在池中的实际对象的类。 在我们的示例中这将是ssh连接。 Sessionfactory需要扩展BaseKeyedPoolableObjectFactory KV其中K是此池中键的类型 V是此池中保存的对象的类型。 All you need to do is implement the makeObject方法 All you need to do is implement the方法需要在池中实际创建对象而destroyObject显然需要在释放对象并将其放回池中时实现代码。 package org.grep4j.core.command.linux;
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.grep4j.core.model.ServerDetails;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
/*** This class is used to handle ssh Session inside the pool.* * author Marco Castigliego**/
public class SessionFactory extends BaseKeyedPoolableObjectFactoryServerDetails, Session {/*** This creates a Session if not already present in the pool.*/Overridepublic Session makeObject(ServerDetails serverDetails) throws Exception {Session session null;try {JSch jsch new JSch();session jsch.getSession(serverDetails.getUser(), serverDetails.getHost(), serverDetails.getPort());session.setConfig(StrictHostKeyChecking, no); // UserInfo userInfo new JschUserInfo(serverDetails.getUser(), serverDetails.getPassword());session.setUserInfo(userInfo);session.setTimeout(60000);session.setPassword(serverDetails.getPassword());session.connect();} catch (Exception e) {throw new RuntimeException(ERROR: Unrecoverable error when trying to connect to serverDetails : serverDetails, e);}return session;}/*** This is called when closing the pool object*/Overridepublic void destroyObject(ServerDetails serverDetails, Session session) {session.disconnect();}
} 您需要做的第二件事是创建实际的密钥池对象。 在我们的示例中我们创建一个拥有StackKeyedObjectPool的单例。 数字10是池中“睡眠”实例数量的上限。 如果11个客户端尝试为同一服务器获取ssh连接第11个客户端将等待直到前10个客户端之一释放其连接。 package org.grep4j.core.command.linux;
import org.apache.commons.pool.KeyedObjectPool;
import org.apache.commons.pool.impl.StackKeyedObjectPool;
import org.grep4j.core.model.ServerDetails;
import com.jcraft.jsch.Session;
/*** Pool controller. This class exposes the org.apache.commons.pool.KeyedObjectPool class.* * author Marco Castigliego**/
public class StackSessionPool {private KeyedObjectPoolServerDetails, Session pool;private static class SingletonHolder {public static final StackSessionPool INSTANCE new StackSessionPool();}public static StackSessionPool getInstance() {return SingletonHolder.INSTANCE;}private StackSessionPool(){startPool();}/*** * return the org.apache.commons.pool.KeyedObjectPool class*/public KeyedObjectPoolServerDetails, Session getPool() {return pool;}/*** * return the org.apache.commons.pool.KeyedObjectPool class*/public void startPool() {pool new StackKeyedObjectPoolServerDetails, Session(new SessionFactory(), 10);}
} 如何使用它简单明了。 要从池中获取ssh连接我们只需要调用 StackSessionPool.getInstance().getPool().borrowObject(serverDetails) 其中serverDetails是我们的关键我们希望每个服务器有一个ssh连接池。 当不再需要连接时我们使用以下命令将其放回池中 StackSessionPool.getInstance().getPool().returnObject(serverDetails, session);package org.grep4j.core.command.linux;import org.grep4j.core.command.ExecutableCommand;
import org.grep4j.core.model.ServerDetails;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.Session;
/*** The SshCommandExecutor uses the net.schmizz.sshj library to execute remote* commands.* * ol* liEstablish a connection using the credential in the {link serverDetails}/li* liOpens a session channel/li* liExecute a command on the session/li* liCloses the session/li* liDisconnects/li* /ol* * author Marco Castigliego* */
public class JschCommandExecutor extends CommandExecutor {public JschCommandExecutor(ServerDetails serverDetails) {super(serverDetails);}Overridepublic CommandExecutor execute(ExecutableCommand command) {Session session null;Channel channel null;try {session StackSessionPool.getInstance().getPool().borrowObject(serverDetails);//...do stuff} catch (Exception e) {throw new RuntimeException(ERROR: Unrecoverable error when performing remote command e.getMessage(), e);} finally {if (null ! channel channel.isConnected()) {channel.disconnect();}if (null ! session) {try {StackSessionPool.getInstance().getPool().returnObject(serverDetails, session);} catch (Exception e) {e.printStackTrace();}}}return this;}
} 请记住当您不再需要使用PoolSessionPool.getInstance。getPool。close时关闭该池。 参考 使用来自我们的JCG合作伙伴 Marco Castigliego的Apache KeyedObjectPool的ssh连接池位于“ 删除重复和修复不良名称”博客中。 翻译自: https://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910793.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!