I use the following code to get a jsch session:
JSch jsch = new JSch();
String user = "test";
String host = "172.23.54.253";
String password = "paas@tju";
int port = 22;
Session session = jsch.getSession(user,host,port);
UserInfo userInfo = new MyUserInfo(null);
session.setUserInfo(userInfo);
session.setPassword(password);
session.connect();
About 3 hours later, I execute the following code:
if(session.isConnected())
{
System.out.println("Session is still connected!");
}
else
{
System.out.println("Session is disconnected!);
}
I found that the jsch session is still connected! But when I check the /var/log/secure file, I found that the session has already been closed! If I open a channel with the jsch session which is created 3 hours ago, a JSchException would be thrown as below:
com.jcraft.jsch.JSchException: session is down
at com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:762)
at com.jcraft.jsch.Channel.connect(Channel.java:151)
at com.jcraft.jsch.Channel.connect(Channel.java:145)
at com.inspur.jobSchedule.test.PressureTest.test(PressureTest.java:117)
at com.inspur.jobSchedule.test.PressureTest.main(PressureTest.java:138)
I known the keepalive time is 2 hours for a tcp connection on Linux. But why when I call the "isConnected" method of the session,it returns true?