Search in sources :

Example 1 with SyncFinisher

use of cn.hutool.core.thread.SyncFinisher in project Jpom by dromara.

the class TestJschExec method testShell2.

@Test
public void testShell2() throws Exception {
    String[] command = new String[] { cmd };
    Session session = null;
    Channel channel = null;
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        session = JschUtil.createSession("192.168.1.8", 22, "root", "123456+");
        channel = JschUtil.createChannel(session, ChannelType.SHELL);
        channel.connect();
        inputStream = channel.getInputStream();
        outputStream = channel.getOutputStream();
        SyncFinisher syncFinisher = new SyncFinisher(2);
        PrintWriter printWriter = new PrintWriter(outputStream);
        InputStream finalInputStream = inputStream;
        StringBuffer stringBuilder = new StringBuffer();
        syncFinisher.addRepeatWorker(() -> {
            for (String s : command) {
                try {
                    printWriter.println(s);
                    printWriter.println("exit");
                    // 把缓冲区的数据强行输出
                    printWriter.flush();
                // System.out.println(s + "  " + command.length);
                } catch (Exception e) {
                    e.printStackTrace();
                    DefaultSystemLog.getLog().error("写入错误", e);
                }
            }
        }).addRepeatWorker(() -> {
            try {
                byte[] buffer = new byte[1024];
                int i;
                // 如果没有数据来,线程会一直阻塞在这个地方等待数据。
                while ((i = finalInputStream.read(buffer)) != -1) {
                    // System.out.println(i);
                    stringBuilder.append(new String(Arrays.copyOfRange(buffer, 0, i), CharsetUtil.CHARSET_UTF_8));
                }
            } catch (Exception e) {
                DefaultSystemLog.getLog().error("读取错误", e);
            }
        }).start();
        System.out.println(stringBuilder);
    } finally {
        IoUtil.close(inputStream);
        IoUtil.close(outputStream);
        JschUtil.close(channel);
        JschUtil.close(session);
    }
}
Also used : SyncFinisher(cn.hutool.core.thread.SyncFinisher) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with SyncFinisher

use of cn.hutool.core.thread.SyncFinisher in project Jpom by dromara.

the class TestJschExec method test.

@Test
public void test() throws IOException, JSchException {
    Charset charset = CharsetUtil.CHARSET_UTF_8;
    Session session = JschUtil.createSession("192.168.1.8", 22, "root", "123456+");
    ChannelExec channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);
    // 添加环境变量
    channel.setCommand(cmd);
    InputStream inputStream = channel.getInputStream();
    InputStream errStream = channel.getErrStream();
    channel.connect((int) TimeUnit.SECONDS.toMillis(5));
    // 
    SyncFinisher syncFinisher = new SyncFinisher(2);
    final String[] error = new String[1];
    final String[] result = new String[1];
    // 
    syncFinisher.addRepeatWorker(() -> {
        try {
            error[0] = IoUtil.read(errStream, charset);
        } catch (Exception e) {
            e.printStackTrace();
            if (!StrUtil.contains(e.getMessage(), "Pipe closed")) {
                DefaultSystemLog.getLog().error("读取 exec err 流发生异常", e);
                error[0] = "读取 exec err 流发生异常" + e.getMessage();
            }
        } finally {
            syncFinisher.stop();
        }
    }).addRepeatWorker(() -> {
        try {
            result[0] = IoUtil.read(inputStream, charset);
        } catch (Exception e) {
            e.printStackTrace();
            if (!StrUtil.contains(e.getMessage(), "Pipe closed")) {
                DefaultSystemLog.getLog().error("读取 exec 流发生异常", e);
                result[0] = "读取 exec 流发生异常" + e.getMessage();
            }
        } finally {
            syncFinisher.stop();
        }
    }).start();
    System.out.println(result[0] + "  " + error[0]);
}
Also used : SyncFinisher(cn.hutool.core.thread.SyncFinisher) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session) Test(org.junit.Test)

Aggregations

SyncFinisher (cn.hutool.core.thread.SyncFinisher)2 JSchException (com.jcraft.jsch.JSchException)2 Session (com.jcraft.jsch.Session)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 Channel (com.jcraft.jsch.Channel)1 ChannelExec (com.jcraft.jsch.ChannelExec)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Charset (java.nio.charset.Charset)1