package com.ruoyi.common.db;

import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


//@Component//尽量加上这个
//@WebListener//声明为监听器 implements ServletContextListener
public class SshContextListener implements ServletContextListener{

    private SshTunnelConfig sshConnectionConfig;

    public SshContextListener() {
        super();
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Context initialized ... !");
        try {
            sshConnectionConfig = new SshTunnelConfig();
            sshConnectionConfig.createSshTunnel();
        } catch (Throwable e) {
            e.printStackTrace(); // 连接失败
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Context destroyed ... !");
        sshConnectionConfig.closeSshTunnel();//断开ssh连接
    }
}
