ssh 看apache_使用Apache KeyedObjectPool的ssh连接池
ssh 看apache
我發(fā)現(xiàn)org.apache.commons.pool非常有用且健壯,但沒有充分記錄。 因此,我將在這里幫助您解釋如何使用Apache KeyedObjectPool 。 什么是KeyedObjectPool ? 它是一個映射,其中包含多種類型的實例池。 可以使用任意鍵訪問每種類型。 在此示例中,我將創(chuàng)建一個JSch ssh連接池,并將使用一個名為ServerDetails的簡單getter setter對象作為鍵。 基本上,對于每個服務(wù)器,我希望有10個可重用的ssh連接池。 因此,首先要做的是創(chuàng)建一個Sessionfactory,一個負責(zé)創(chuàng)建要存儲在池中的實際對象的類。 在我們的示例中,這將是ssh連接。
Sessionfactory需要擴展BaseKeyedPoolableObjectFactory <K,V>,其中K是此池中鍵的類型, V是此池中保存的對象的類型。 All you need to do is implement the makeObject方法, All you need to do is implement the方法需要在池中實際創(chuàng)建對象,而destroyObject顯然需要在釋放對象并將其放回池中時實現(xiàn)代碼。
您需要做的第二件事是創(chuàng)建實際的密鑰池對象。 在我們的示例中,我們創(chuàng)建一個擁有StackKeyedObjectPool的單例。 數(shù)字10是池中“睡眠”實例數(shù)量的上限。 如果11個客戶端嘗試為同一服務(wù)器獲取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 KeyedObjectPool<ServerDetails, 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 KeyedObjectPool<ServerDetails, Session> getPool() {return pool;}/*** * @return the org.apache.commons.pool.KeyedObjectPool class*/public void startPool() {pool = new StackKeyedObjectPool<ServerDetails, Session>(new SessionFactory(), 10);} }如何使用它,簡單明了。 要從池中獲取ssh連接,我們只需要調(diào)用:
StackSessionPool.getInstance().getPool().borrowObject(serverDetails)其中,serverDetails是我們的關(guān)鍵(我們需要每個服務(wù)器的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>* <li>Establish a connection using the credential in the {@link serverDetails}</li>* <li>Opens a session channel</li>* <li>Execute a command on the session</li>* <li>Closes the session</li>* <li>Disconnects</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()時,關(guān)閉該池。
參考: 使用來自我們的JCG合作伙伴 Marco Castigliego的Apache KeyedObjectPool的ssh連接池,位于“ 刪除重復(fù)和修復(fù)不良名稱”博客中。
翻譯自: https://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html
ssh 看apache
總結(jié)
以上是生活随笔為你收集整理的ssh 看apache_使用Apache KeyedObjectPool的ssh连接池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国联邦航空管理局最快下个月批准Spac
- 下一篇: 微软今日正式推出 Xbox Game P