点记:jsch

前几天也给大家介绍,由于我的系统承建在多台应用主机上,如实增加了SFTP Server;

如实增加静态文件服务器;

想着就采用jsch来实现SFTP功能;如实兴高采烈的写个个代码;

增加了如下配置;

## 先上代码

package me.duzhi.ilog.cms.function;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jfinal.log.Log;
import java.util.Map;
import java.util.Properties;


/**
 * Created by Qinjin on 2017/7/16.
 */
public class SFTPKit {

  Session session = null;
  Channel channel = null;


  public static final String SFTP_REQ_HOST = "sftp.host";
  public static final String SFTP_REQ_PORT = "sftp.port";
  public static final String SFTP_REQ_USERNAME = "sftp.username";
  public static final String SFTP_REQ_PASSWORD = "sftp.password";
  public static final int SFTP_DEFAULT_PORT = 22;
  public static final String SFTP_REQ_LOC = "location";
public static final Log LOG = Log.getLog(SFTPKit.class);

  public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {

    String ftpHost = sftpDetails.get(SFTP_REQ_HOST);
    String port = sftpDetails.get(SFTP_REQ_PORT);
    String ftpUserName = sftpDetails.get(SFTP_REQ_USERNAME);
    String ftpPassword = sftpDetails.get(SFTP_REQ_PASSWORD);

    int ftpPort = SFTP_DEFAULT_PORT;
    if (port != null && !port.equals("")) {
      ftpPort = Integer.valueOf(port);
    }

    JSch jsch = new JSch(); // 创建JSch对象
    session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象

    if (LOG.isDebugEnabled()) {
      LOG.debug("Session created.");
    }
    if (ftpPassword != null) {
      session.setPassword(ftpPassword); // 设置密码
    }
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config); // 为Session对象设置properties
    session.setTimeout(timeout); // 设置timeout时间
    session.connect(); // 通过Session建立链接
    if (LOG.isDebugEnabled()) {
      LOG.debug("Session connected.");
    }


    LOG.debug("Opening Channel.");
    channel = session.openChannel("sftp"); // 打开SFTP通道
    channel.connect(); // 建立SFTP通道的连接
    if (LOG.isDebugEnabled()) {
      LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
          + ", returning: " + channel);
    }

    return (ChannelSftp) channel;
  }

  public void closeChannel() throws Exception {
    if (channel != null) {
      channel.disconnect();
    }
    if (session != null) {
      session.disconnect();
    }
  }
}

 

别以为就只有代码就哦了哦;

 

呵呵折腾了半天上线;其实上线很简单,哟其现在都是一键发布,重启等等工作,

启动很Happy,基本ok。

但是后台发现阻塞了不少线程如下:

"qtp1100439041-14" #14 prio=5 os_prio=0 tid=0x00007fe5a447e000 nid=0x6d38 waiting for monitor entry [0x00007fe5a86d0000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.jcraft.jsch.Session.send_kexinit(Session.java:505)
- waiting to lock (a com.jcraft.jsch.jce.Random)
at com.jcraft.jsch.Session.receive_kexinit(Session.java:459)
at com.jcraft.jsch.Session.connect(Session.java:266)
at com.jcraft.jsch.Session.connect(Session.java:148)
at me.duzhi.ilog.cms.function.SFTPKit.getChannel(SFTPKit.java:51)
at io.jpress.utils.AttachmentUtils$Ftp.sftpFile(AttachmentUtils.java:145)
at io.jpress.utils.AttachmentUtils.moveFile(AttachmentUtils.java:70)
at me.duzhi.ilog.cms.controller._UserController.setAvatar(_UserController.java:217)
at me.duzhi.ilog.cms.controller._UserController.save(_UserController.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:73)
at com.jfinal.validate.Validator.intercept(Validator.java:80)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at com.jfinal.ext.interceptor.POST.intercept(POST.java:30)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at io.jpress.interceptor.UserInterceptor.intercept(UserInterceptor.java:29)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at io.jpress.core.interceptor.HookInterceptor.intercept(HookInterceptor.java:31)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at io.jpress.interceptor.AdminInterceptor.intercept(AdminInterceptor.java:36)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at io.jpress.interceptor.GlobelInterceptor.intercept(GlobelInterceptor.java:35)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at io.jpress.core.interceptor.JI18nInterceptor.intercept(JI18nInterceptor.java:53)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at me.duzhi.ilog.cms.interceptor.MemberInterceptor.intercept(MemberInterceptor.java:27)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:82)
at io.jpress.core.MyDruidStatViewHandler.handle(MyDruidStatViewHandler.java:43)
at io.jpress.core.cache.ActionCacheHandler.handle(ActionCacheHandler.java:52)
at io.jpress.core.JHandler.handle(JHandler.java:83)
at io.jpress.core.cache.ActionCacheHandler.handle(ActionCacheHandler.java:52)
at me.duzhi.ilog.cms.hanlder.HtmlHandler.handle(HtmlHandler.java:27)
at me.duzhi.ilog.cms.hanlder.HoldHandler$1.invoke(HoldHandler.java:26)
at me.duzhi.ilog.cms.hok.DefaultAction.handle(DefaultAction.java:12)
at me.duzhi.ilog.cms.hok.HokManager.invoke(HokManager.java:97)
at me.duzhi.ilog.cms.hok.HokKit.invoke(HokKit.java:35)
at me.duzhi.ilog.cms.hanlder.HoldHandler.handle(HoldHandler.java:23)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:74)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1561)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
at java.lang.Thread.run(Thread.java:745)

慌了;


仔细研究了下代码,发现这个里面有个熵池的概念,HOHO,好熟悉。

之前Tomcat 也有类似问题;

如实开心了,

在启动项中增加了脚本---》-Djava.security.egd=file:/dev/./urandom"

JAVA_OPTS=" -Xmx512m -Xms512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/mtdperf.hprof -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"
START_CLASS="me.duzhi.ilog.cms.Startup"

启动,完美无缺,搞定收工;

除特别注明外,本站所有文章均为duzhi原创,转载请注明出处来自https://www.duzhi.me/article/5926.html

联系我们

******

在线咨询:点击这里给我发消息

邮件:ashang.peng#aliyun.com

QR code