JSch: How to keep the session alive and up

前端 未结 3 721
攒了一身酷
攒了一身酷 2020-12-08 11:05

I\'m writing Java GUI program for static route management using SSH. My code is as follows:

import com.jcraft.jsch.*;
import java.io.*;

public class Konsep          


        
3条回答
  •  长情又很酷
    2020-12-08 11:45

    You can use a pool , I used org.apache.commons.pool2 so the pool is responsible of providing connected sessions, basically:

    • Create JSCH Session on makeObject
    • connect on activate if disconnected
    • check if isConnected on validate
    • sendKeepAliveMsg on passivate

    Above worked for me, also keep in mind that if you add logic on validate, the poolConfig should set true on testOnBorrow or testOnReturn. So I used KeyedObjectPool you should get connections from the pool via borrowObject method and make sure you return them to the pool once you operation is complete via returnObject.

    https://commons.apache.org/proper/commons-pool/api-2.2/org/apache/commons/pool2/PooledObjectFactory.html

提交回复
热议问题