日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ssh 看apache_使用Apache KeyedObjectPool的ssh连接池

發布時間:2023/12/3 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ssh 看apache_使用Apache KeyedObjectPool的ssh连接池 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ssh 看apache

我發現org.apache.commons.pool非常有用且健壯,但沒有充分記錄。 因此,我將在這里幫助您解釋如何使用Apache KeyedObjectPool 什么是KeyedObjectPool ? 它是一個映射,其中包含多種類型的實例池。 可以使用任意鍵訪問每種類型。 在此示例中,我將創建一個JSch ssh連接池,并將使用一個名為ServerDetails的簡單getter setter對象作為鍵。 基本上,對于每個服務器,我希望有10個可重用的ssh連接池。 因此,首先要做的是創建一個Sessionfactory,一個負責創建要存儲在池中的實際對象的類。 在我們的示例中,這將是ssh連接。

Sessionfactory需要擴展BaseKeyedPoolableObjectFactory <K,V>,其中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 BaseKeyedPoolableObjectFactory<ServerDetails, 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 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連接,我們只需要調用:

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>* <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()時,關閉該池。

參考: 使用來自我們的JCG合作伙伴 Marco Castigliego的Apache KeyedObjectPool的ssh連接池,位于“ 刪除重復和修復不良名稱”博客中。

翻譯自: https://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html

ssh 看apache

總結

以上是生活随笔為你收集整理的ssh 看apache_使用Apache KeyedObjectPool的ssh连接池的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。