【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作业。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值