ssh 链接线程池 文件上传、下载、删除,执行shll命令

创建ssh链接线程池,统一管理ssh链接,
SFTP文件上传
SFTP文件下载
删除指定目录文件
删除指定目录
执行复杂shell命令

项目中使用创建线程池

 package com.getc.system.config;

import com.getc.system.dome.GetcError;
import com.getc.system.dome.GetcLogLineNumber;
import com.getc.system.service.GetcErrorService;
import com.getc.system.service.GetcLogLineNumberService;
import com.getc.system.utils.CacheUtils;
import com.getc.system.utils.ConnectionPool;
import com.getc.system.utils.ThreadPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @description: 创建线程池
 * @author: jyk
 * @time: 2021/6/4 15:22
 */
@Configuration
public class SysInitializationConfig {
   
 
    @Bean
    public ConnectionPool connectionPool(){
   
        return new ConnectionPool("no", 30000,2,10);
    }

}

使用

 ConnectionPool connectionPool = ConnectionPool.getConnectionPool();
 SShUtils shellUtil = new SShUtils(connectionPool);
 shellUtil.init(host, Integer.valueOf(port), account, password);
 //执行命令
 String txt = shellUtil.execCmdByShell("cd /","ls");

ssh 链接线程池

 package com.getc.system.utils;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import java.util.Enumeration;
import java.util.Objects;
import java.util.Properties;
import java.util.Vector;

/**
 * @description:  ssh 链接线程池
 * @author: jyk
 * @time: 2021/6/16 18:16
 */
@Slf4j
public class ConnectionPool {
   

    private static  ConnectionPool connectionPool = null;

    private String strictHostKeyChecking;

    private Integer timeout;


    /**
     * ip地址
     */
    private String ip = "";

    /**
     * 端口号
     */
    private Integer port = 22;

    /**
     * 用户名
     */
    private String username = "";

    /**
     * 密码
     */
    private String password = "";

    /**
     * 每次扩容增加几个连接
     */
    private int incrementalConnections = 2;

    /**
     * 最大连接数
     */
    private int maxConnections = 1000;

    /**
     * 最大空闲连接
     */
    private int maxIdle = 4;

    /**
     * 最小空闲连接
     */
    private int minIdel = 2;


    private Vector<PooledConnection> connections = null;

    @PostConstruct
    private void init() {
   
        createPool();
    }

    /**
     * 获取ssh链接池
     * @return
     */
    public static ConnectionPool getConnectionPool() {
   
        return connectionPool;
    }

    /**
     * 构造方法
     */
    public ConnectionPool() {
   }

    /**
     * 构造方法
     *
     * @param strictHostKeyChecking 连接模式
     * @param timeout               超时时间
     */
    public ConnectionPool(String strictHostKeyChecking, Integer timeout) {
   
        connectionPool = new ConnectionPool();
        connectionPool.strictHostKeyChecking = strictHostKeyChecking;
        connectionPool.timeout = timeout;
        connectionPool.createPool();
    }

    /**
     * 构造方法
     *
     * @param strictHostKeyChecking  连接模式
     * @param timeout                超时时间
     * @param incrementalConnections 增量大小
     */
    public ConnectionPool(String strictHostKeyChecking,
                          Integer timeout,
                          int incrementalConnections) {
   
        connectionPool = new ConnectionPool();
        connectionPool.strictHostKeyChecking = strictHostKeyChecking;
        connectionPool.timeout = timeout;
        connectionPool.incrementalConnections = incrementalConnections;
        connectionPool.createPool();
    }


    /**
     * 构造方法
     *
     * @param strictHostKeyChecking  连接模式
     * @param timeout                超时时间
     * @param incrementalConnections 增量大小
     * @param maxConnections         连接池最大连接数
     */
    public ConnectionPool(String strictHostKeyChecking,
                          Integer timeout,
                          int incrementalConnections,
                          int maxConnections) {
   
        connectionPool = new ConnectionPool();
        connectionPool.strictHostKeyChecking = strictHostKeyChecking;
        connectionPool.timeout = timeout;
        connectionPool.incrementalConnections = incrementalConnections;
        connectionPool.maxConnections = maxConnections;
        connectionPool.createPool();
    }

    /**
     * 创建连接池,判断连接池是否创建,如果连接池没有创建则创建连接池
     */
    public synchronized void createPool() {
   
        if (Objects.nonNull(connections)) {
   
            return;
        }
        connections = new Vector<>();
        log.info("create shell connectionPool success!");
    }


    /**
     * 创建指定数量的连接放入连接池中
     *
     * @param numConnections 创建数量
     * @throws JSchException 建立远程连接异常
     */
    private void createConnections(int numConnections) throws JSchException {
   
        for (int x = 0; x < numConnections; x
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值