當前位置: 首頁>>代碼示例>>Java>>正文


Java Session.setServerAliveInterval方法代碼示例

本文整理匯總了Java中com.jcraft.jsch.Session.setServerAliveInterval方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.setServerAliveInterval方法的具體用法?Java Session.setServerAliveInterval怎麽用?Java Session.setServerAliveInterval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.jcraft.jsch.Session的用法示例。


在下文中一共展示了Session.setServerAliveInterval方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: configureSession

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
private Session configureSession() {
    try {
        //configure the tunnel
        log.info("Configuring SSH tunnel");
        Session session = jsch.getSession(
                sshDetails.user,
                sshDetails.host,
                sshDetails.sshPort
        );

        jsch.addIdentity(sshDetails.keyFile.getPath(), sshDetails.passphrase);

        final Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("ConnectionAttempts", "3");
        //dont set the keep alive too low or you might suffer from a breaking connection during start up (for whatever reason)
        session.setServerAliveInterval(1000);//milliseconds
        session.setConfig(config);

        //forward the port
        final int assignedPort = session.setPortForwardingL(
                sshDetails.localPort,
                "localhost",
                sshDetails.remotePort
        );
        log.info("Setting up port forwarding: localhost:" + assignedPort + " -> " + sshDetails.host + ":" + sshDetails.remotePort);

        return session;
    } catch (final JSchException e) {
        throw new RuntimeException("Failed to configure SSH tunnel", e);
    }
}
 
開發者ID:napstr,項目名稱:SqlSauce,代碼行數:33,代碼來源:SshTunnel.java

示例2: startNewSession

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<String, String> 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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:82,代碼來源:JSchChannelsSupport.java


注:本文中的com.jcraft.jsch.Session.setServerAliveInterval方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。