概述
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
private Session startNewSession(boolean acquireChannel) throws JSchException, InterruptedException {
Session newSession = null;
final AtomicBoolean cancelled = new AtomicBoolean(false);
ConnectingProgressHandle.startHandle(env, new Cancellable() {
@Override
public boolean cancel() {
cancelled.set(true);
return true;
}
});
try {
while (!cancelled.get()) {
try {
newSession = jsch.getSession(env.getUser(), env.getHostAddress(), env.getSSHPort());
int serverAliveInterval = Integer.getInteger("jsch.server.alive.interval", 0); // NOI18N
if (serverAliveInterval > 0) {
newSession.setServerAliveInterval(serverAliveInterval);
int serverAliveCount = Integer.getInteger("jsch.server.alive.count", 5); // NOI18N
newSession.setServerAliveCountMax(serverAliveCount);
}
newSession.setUserInfo(userInfo);
for (Entry entry : jschSessionConfig.entrySet()) {
newSession.setConfig(entry.getKey(), entry.getValue());
}
Authentication auth = Authentication.getFor(env);
final String preferredAuthKey = "PreferredAuthentications"; // NOI18N
if (!jschSessionConfig.containsKey(preferredAuthKey)) {
String methods = auth.getAuthenticationMethods().toJschString();
if (methods != null) {
log.finest("Setting auth method list to " + methods); //NOI18N
newSession.setConfig(preferredAuthKey, methods);
}
}
if (USE_JZLIB) {
newSession.setConfig("compression.s2c", "[email protected],zlib,none"); // NOI18N
newSession.setConfig("compression.c2s", "[email protected],zlib,none"); // NOI18N
newSession.setConfig("compression_level", "9"); // NOI18N
}
if (RemoteStatistics.COLLECT_STATISTICS && RemoteStatistics.COLLECT_TRAFFIC) {
newSession.setSocketFactory(MeasurableSocketFactory.getInstance());
}
newSession.connect(auth.getTimeout()*1000);
break;
} catch (JSchException ex) {
if (!UNIT_TEST_MODE) {
String msg = ex.getMessage();
if (msg == null) {
throw ex;
}
if (msg.startsWith("Auth fail") || msg.startsWith("SSH_MSG_DISCONNECT: 2")) { // NOI18N
PasswordManager.getInstance().clearPassword(env);
}
} else {
throw ex;
}
} catch (CancellationException cex) {
cancelled.set(true);
}
}
if (cancelled.get()) {
throw new InterruptedException("StartNewSession was cancelled ..."); // NOI18N
}
// In case of any port-forwarding previously set for this env
// init the new session appropriately
portForwarding.initSession(newSession);
sessions.put(newSession, new AtomicInteger(JSCH_CHANNELS_PER_SESSION - (acquireChannel ? 1 : 0)));
log.log(Level.FINE, "New session [{0}] started.", new Object[]{System.identityHashCode(newSession)}); // NOI18N
} finally {
ConnectingProgressHandle.stopHandle(env);
}
return newSession;
}
最后
以上就是冷傲柜子为你收集整理的java setsession_Java Session.setServerAliveInterval方法代码示例的全部内容,希望文章能够帮你解决java setsession_Java Session.setServerAliveInterval方法代码示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复