最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot啟動bat/bash腳本實現(xiàn)過程

 更新時間:2025年08月28日 16:45:17   作者:誠誠程程成  
請?zhí)峁┚唧w錯誤信息或腳本內(nèi)容,以便分析Spring Boot項目中Controller、Entity、Service層的依賴問題,跨平臺腳本需注意路徑兼容性和環(huán)境變量配置,確保依賴項正確引入

windows有兩個腳本,linux有一個腳本,需要用springboot啟動著兩個腳本

contoller層

import com.dwc.putdatasystem.service.impl.PutServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

@RestController
@RequestMapping("/putbat")
public class PutController {

    @Autowired
    private PutServiceImpl putBatService;



    @GetMapping ("bat")
    public void put() throws Exception {
        //第一部分   執(zhí)行bat腳本
        String batPath = "D:/******/package.bat"; // bat腳本路徑
        String batPath2 ="D:/*****/put.bat";
        File batFile = new File(batPath);
        boolean batFileExist = batFile.exists();
        System.out.println("??????????????????????????判斷bat腳本文件是否存在(true/false)??????????????????????????");
        System.out.println("batFileExist:" + batFileExist);
        if (batFileExist) {
            System.out.println("??????????????????????????正在執(zhí)行編譯ing??????????????????????????");
            putBatService.callCmd(batPath);
            System.out.println("??????????????????????????編譯完成??????????????????????????");
            System.out.println("??????????????????????????正在上傳至linux系統(tǒng)ing??????????????????????????");
            putBatService.callCmd(batPath2);
        }
        System.out.println("??????????????????????????jar包上傳服務(wù)器完成??????????????????????????");

        //第二部分  執(zhí)行bash腳本
        String linuxIP = "ip地址";
        String usrName = "用戶名";
        String passwd  = "密碼";
        String DEFAULTCHART = "UTF-8";
        //PutEntity rec = new PutEntity(linuxIP, usrName, passwd,);

        //PutBatServiceImpl putBatService = new PutBatServiceImpl(linuxIP, usrName, passwd);

        System.out.println("??????????????????????????正在上傳job至集群ing??????????????????????????");
        // 執(zhí)行腳本
        System.out.println(putBatService.execute("/home/dwc/bin/flinkrunjar1.sh start", linuxIP, usrName, passwd, DEFAULTCHART));
        // 執(zhí)行jps命令,查看節(jié)點是否完整
        System.out.println(putBatService.execute("/home/dwc/bin/jpsall start", linuxIP, usrName, passwd, DEFAULTCHART));
        System.out.println("??????????????????????????job上傳集群完成??????????????????????????");
    }



}

entity層

import ch.ethz.ssh2.Connection;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Value;

@Data
public class PutEntity {

    public String DEFAULTCHART = "UTF-8";
    public Connection conn;
    public String ip;
    public String userName;
    public String userPwd;


    public PutEntity(String ip, String userName, String userPwd, String DEFAULTCHART) {
        this.ip = ip;
        this.userName = userName;
        this.userPwd = userPwd;
        this.DEFAULTCHART = DEFAULTCHART;
    }

    public PutEntity() {

    }
}

service層

import java.io.InputStream;

public interface PutService {

    //win腳本用
    void callCmd(String locationCmd);

    //linux腳本用
    //Boolean login(String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception;

    String execute(String cmd, String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception;

    String processStdout(InputStream in, String charset) throws Exception;
}
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.dwc.putdatasystem.entity.PutEntity;
import com.dwc.putdatasystem.service.PutService;
import ch.ethz.ssh2.Connection;
import org.apache.flink.calcite.shaded.org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;


import org.springframework.stereotype.Service;

import java.io.*;


@Service("PutBatService")
public class PutServiceImpl implements PutService {


    @Override
    public void  callCmd(String locationCmd){
        StringBuilder sb = new StringBuilder();
        try {
            Process child = Runtime.getRuntime().exec(locationCmd);
            InputStream in = child.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
            String line;
            while((line=bufferedReader.readLine())!=null)
            {
                sb.append(line + "\n");
            }
            in.close();
            try {
                child.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e);
            }
            System.out.println("sb:" + sb.toString());
            System.out.println("callCmd execute finished");
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    /**
     * 遠(yuǎn)程登錄linux主機(jī)
     * @return 登錄成功返回true,否則返回false
     */
//    @Override
//    public Boolean login(String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception {
//        PutEntity putEntity = new PutEntity(ip, userName, userPwd, DEFAULTCHART);
//        boolean flg = false;
//        try {
//            putEntity.conn = new Connection(ip);
//            // 連接
//            putEntity.conn.connect();
//            // 認(rèn)證
//            flg = putEntity.conn.authenticateWithPassword(userName, userPwd);
//        } catch (IOException e) {
//            throw new Exception("遠(yuǎn)程連接服務(wù)器失敗", e);
//        }
//        return flg;
//    }

    /**
     * 遠(yuǎn)程執(zhí)行shll腳本或者命令
     * @param cmd 即將執(zhí)行的命令
     * @return 命令執(zhí)行完后返回的結(jié)果值
     */
    @Override
    public String execute(String cmd, String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception {
        //根據(jù)傳來的ip、userName、userPwd、DEFAULTCHART創(chuàng)建putEntity
        PutEntity putEntity = new PutEntity(ip,userName,userPwd,DEFAULTCHART);
        String result = "";  //result得到命令執(zhí)行完后返回的結(jié)果值
        Session session = null;

        //login方法寫道execute方法中  否則conn值會丟失
        //log方法:驗證是否能夠遠(yuǎn)程登錄linux主機(jī)
        boolean flg = false;
        try {
            putEntity.conn = new Connection(ip);  //創(chuàng)建conn
            // 連接
            putEntity.conn.connect();
            // 認(rèn)證
            flg = putEntity.conn.authenticateWithPassword(userName, userPwd);  //用戶密碼進(jìn)行驗證
        } catch (IOException e) {
            throw new Exception("遠(yuǎn)程連接服務(wù)器失敗", e);
        }

        //驗證成功 flg=true
        try {
            if (flg) {
                // 打開一個會話
                session = putEntity.conn.openSession();   //根據(jù)conn得到session值  ***之前l(fā)ogin方法寫道execute方法外部 報空指針錯誤   ****原因:程序進(jìn)入execute時conn已經(jīng)丟失了 所以session為空 執(zhí)行失敗
                // 執(zhí)行命令
                session.execCommand(cmd);  //執(zhí)行cmd命令
                result = processStdout(session.getStdout(), putEntity.DEFAULTCHART);   //命令執(zhí)行完后返回的結(jié)果值
                // 如果result為空,說明腳本執(zhí)行出錯了
                if (StringUtils.isBlank(result)) {
                    result = processStdout(session.getStderr(), putEntity.DEFAULTCHART);
                }
                putEntity.conn.close();
                session.close();
            }
        } catch (IOException e) {
            throw new Exception("命令執(zhí)行失敗", e);
        } finally {
            if (putEntity.conn != null) {
                putEntity.conn.close();
            }
            if (session != null) {
                session.close();
            }
        }
        return result;
    }

    /**
     * 解析腳本執(zhí)行返回的結(jié)果集
     * @param in 輸入流對象
     * @param charset 編碼
     * @return 以純文本的格式返回
     */
    @Override
    public String processStdout(InputStream in, String charset) throws Exception {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(stdout, charset);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line + "\n");
            }
        } catch (UnsupportedEncodingException e) {
            throw new Exception("不支持的編碼字符集異常", e);
        } catch (IOException e) {
            throw new Exception("讀取指紋失敗", e);
        } finally {
            IOUtils.close(br);
            IOUtils.close(isr);
            IOUtils.close(stdout);
        }
        return buffer.toString();
    }


}

依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-scala_2.11</artifactId>
            <version>1.13.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-table-planner-blink_2.12</artifactId>
            <version>1.13.3</version>
        </dependency>

    </dependencies>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 實現(xiàn)簡易教務(wù)管理系統(tǒng)的代碼

    Java 實現(xiàn)簡易教務(wù)管理系統(tǒng)的代碼

    這篇文章主要介紹了Java 實現(xiàn)簡易教務(wù)管理系統(tǒng)的代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Java實現(xiàn)按行讀取大文件

    Java實現(xiàn)按行讀取大文件

    這篇文章主要介紹了Java實現(xiàn)按行讀取大文件的方法的小結(jié),非常的簡單實用,有需要的小伙伴尅參考下。
    2015-05-05
  • springboot在idea下debug調(diào)試熱部署問題

    springboot在idea下debug調(diào)試熱部署問題

    這篇文章主要介紹了springboot在idea下debug調(diào)試熱部署問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java中自己如何實現(xiàn)log2(N)

    Java中自己如何實現(xiàn)log2(N)

    這篇文章主要介紹了Java中自己實現(xiàn)log2(N)的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java?中的?getDeclaredFields()使用與原理解析

    Java?中的?getDeclaredFields()使用與原理解析

    在Java反射機(jī)制中,getDeclaredFields()用于獲取類的所有字段,包括私有字段,通過反射,可以在運行時動態(tài)地獲取類的信息并操作其成員,本文詳細(xì)介紹了getDeclaredFields()的使用方法、工作原理以及最佳實踐,涵蓋了反射的基本概念、使用場景和注意事項,感興趣的朋友一起看看吧
    2025-01-01
  • java實現(xiàn)無符號數(shù)轉(zhuǎn)換、字符串補(bǔ)齊、md5、uuid、隨機(jī)數(shù)示例

    java實現(xiàn)無符號數(shù)轉(zhuǎn)換、字符串補(bǔ)齊、md5、uuid、隨機(jī)數(shù)示例

    這篇文章主要介紹了java實現(xiàn)無符號數(shù)轉(zhuǎn)換、字符串補(bǔ)齊、md5、uuid、隨機(jī)數(shù)示例,需要的朋友可以參考下
    2014-04-04
  • Java Record的使用場景分析

    Java Record的使用場景分析

    Java的Record用于簡化不可變數(shù)據(jù)類定義,自動提供構(gòu)造器、equals、hashCode等方法,字段默認(rèn)final且不可變,適用于僅存儲數(shù)據(jù)的場景,不支持手動添加非final字段,但允許擴(kuò)展方法和靜態(tài)成員,本文給大家介紹Java Record的使用,感興趣的朋友一起看看吧
    2025-06-06
  • 深入淺析 Spring Security 緩存請求問題

    深入淺析 Spring Security 緩存請求問題

    這篇文章主要介紹了 Spring Security 緩存請求問題,本文通過實例文字相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧
    2019-04-04
  • springboot @Valid注解對嵌套類型的校驗功能

    springboot @Valid注解對嵌套類型的校驗功能

    這篇文章主要介紹了springboot~@Valid注解對嵌套類型的校驗,主要介紹 @Valid在項目中的使用,需要的朋友可以參考下
    2018-05-05
  • SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理

    SpringMVC整合,出現(xiàn)注解沒有起作用的情況處理

    這篇文章主要介紹了SpringMVC整合,出現(xiàn)注解沒有起作用的情況及處理,具有很好的參考價值,希望對大家有所幫助。
    2023-05-05

最新評論

甘孜县| 桃园市| 沾益县| 县级市| 台江县| 辛集市| 诸暨市| 宁陵县| 灵璧县| 麟游县| 宜章县| 江口县| 金溪县| 明光市| 鄱阳县| 霍州市| 巴楚县| 九龙城区| 肥东县| 榆树市| 郎溪县| 车险| 雅江县| 北辰区| 通榆县| 灵川县| 诸暨市| 桓台县| 麻城市| 视频| 静安区| 沾化县| 通州区| 广西| 蓬溪县| 大足县| 基隆市| 左云县| 宿松县| 沧州市| 德保县|