Jsch session.connect() hangs

2012-10-23
2013-05-28
  • sandeep hegde
    sandeep hegde
    2012-10-23

    Hi,

    We connect to a remote sftp server using keys. The code runs repeatedly every 4 hours. It runs fine most of the times but sometimes it just hangs at the session.connect() call. below is my code snippet.
    Can someone please help?

    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("bw.logger");

    logger.info("Start Java Code activity  ");

    Session session = null;

                JSch jsch = new JSch();
    jsch.addIdentity(keylocation);

    System.out.println("identity added ");
    session=jsch.getSession(username,host,port);

    java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
    logger.info("Starting to connect  ");

            session.connect();

    logger.info("Connection successful ");
    Channel channel = session.openChannel("sftp");
    ChannelSftp sftp = (ChannelSftp) channel;
                sftp.connect();
    logger.info("Change Directory start");

    sftp.cd(remotefiledir);
    File file = new File(localfile);
    sftp.put(new FileInputStream(file),"remotetestfile");

    sftp.exit();
    session.disconnect();

    logger.info("Transfer complete\n");

    Thanks.