【FineReport】帆软调用服务器的kettle作业

1、编写自定义函数并编译

package com.fr.function;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.fr.script.AbstractFunction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static com.fr.health.constants.ComponentHealthConstants.Status.TIME_OUT;

public class CustomSubmitJobCommon extends AbstractFunction {

    @Override
    public Object run(Object[] arg0) {
        return executeRemoteCommandCommon();
    }

    private String executeRemoteCommandCommon() {
        final String ipAddress = "127.0.0.1";     // 远程服务器主机ip 
        final String username = "root";          //用户名
        final String password = "123456";   // 登录密码

        final String command = "cd /usr/kettle_task/N_steelmaking/ceshi/cb_ceshiv1 && ./cb_ceshiv1.sh";   //调用kettle作业的命令
        try{
            Connection conn = new Connection(ipAddress);
            conn.connect(); // 连接远程服务器
            if (!conn.authenticateWithPassword(username, password)) {
                System.err.printf("Failed to authenticate with username '%s' on server %s.", username, ipAddress);
                return "用户名或密码错误";
            }
            try {
                Session session = conn.openSession(); // 打开一个会话
                session.execCommand(command); // 执行cmd命令
                System.out.println(command);
                // Capture output and errors
                InputStream stdOut = new StreamGobbler(session.getStdout());
                InputStream stdErr = new StreamGobbler(session.getStderr());

                // Read and potentially log stdout/stderr
                printStream(stdOut, "STDOUT:");
                printStream(stdErr, "STDERR:");

                // Wait for the command to finish, with a timeout
                int exitStatus = session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
                if (exitStatus == TIME_OUT) {
                    System.err.println("Command execution timed out.");
                    return "执行失败";
                }
                return "执行成功";
            } finally {
                conn.close();
            }
        } catch (IOException e) {
            System.err.println("An error occurred during the SSH connection or command execution.");
            e.printStackTrace();
            return "连接失败";
        }
    }
    private void printStream(InputStream in, String streamType) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(streamType + " " + line);
            }
        } catch (IOException e) {
            System.err.println("Error reading from stream: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        CustomSubmitJobCommon job = new CustomSubmitJobCommon();
        System.out.println(job.executeRemoteCommandCommon());
    }
}

2、新增按钮,并添加点击事件,点击按钮后即可执行命令调用kettle作业。

在这里插入图片描述

相关推荐

  1. BI目录

    2024-06-13 09:54:05       70 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-13 09:54:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-13 09:54:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-13 09:54:05       82 阅读
  4. Python语言-面向对象

    2024-06-13 09:54:05       91 阅读

热门阅读

  1. linux开发常用命令

    2024-06-13 09:54:05       24 阅读
  2. 代码整洁之道学习笔记

    2024-06-13 09:54:05       25 阅读
  3. 使用foreach和stream遍历并修改List列表

    2024-06-13 09:54:05       25 阅读
  4. Elasticsearch介绍,要点和难点以及优缺点

    2024-06-13 09:54:05       25 阅读
  5. 智能数据抓取:自动化时代的资讯收割机

    2024-06-13 09:54:05       31 阅读
  6. 用python把docx批量转为pdf

    2024-06-13 09:54:05       38 阅读
  7. flutter 设置缓存的方法

    2024-06-13 09:54:05       25 阅读
  8. OpenSSL新手教程:加密与安全通信基础

    2024-06-13 09:54:05       32 阅读
  9. web前端需要的知识点:深度解析与技能进阶之路

    2024-06-13 09:54:05       32 阅读