JAVA进程管理工具类(ProcessUtil)

import com.sun.jna.Library;
import com.sun.jna.Native;

public interface Kernel32 extends Library {

	@SuppressWarnings("deprecation")
	public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

	public long GetProcessId(Long hProcess);
	
}


<dependency>
   <groupId>net.java.dev.jna</groupId>
	<artifactId>jna</artifactId>
	<version>5.11.0</version>
</dependency>

<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-system</artifactId>
	<version>5.7.22</version>
</dependency>

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reformer.video.ffmpeg.FfmpegUtil;

import cn.hutool.core.util.RuntimeUtil;

public class ProcessUtil {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProcessUtil.class);

	public static void main(String[] args) throws IOException, InterruptedException {
		//String order = "ffmpeg -i rtsp://admin:123456@192.168.101.233:554/h264/ch1/main/av_stream -qscale 1 -vcodec h264 -acodec aac -bf 0 -tune zerolatency -g 5 -max_delay 100 -strict -2 -f flv rtmp://192.168.10.73:1935/live/1";
		String in = "rtsp://admin:Dl123456@192.168.10.233:554/h264/ch1/main/av_stream";
		String out = "rtmp://192.168.10.73:1935/live/1";
		Process process = RuntimeUtil.exec(FfmpegUtil.builderOrder(in, out));
		mointor(process);
		Long pid = getProcessPID(process);
		System.out.println(pid);
		Thread.sleep(15000);
		stopProcess(pid);

	}

	/**
	 * 开始进程
	 * 
	 * @param pid
	 */
	public static Long startProcess(String cmd) {
		Process process = RuntimeUtil.exec(cmd);
		mointor(process);// 打印检测
		return getProcessPID(process);
	}

	/**
	 * 获取PID
	 * 
	 * @param child
	 * @return
	 */
	public static long getProcessPID(Process process) {
		long pid = -1;
		Field field = null;
		try {
			field = process.getClass().getDeclaredField("handle");
			field.setAccessible(true);
			pid = Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pid;
	}

	/**
	 * 停止进程
	 * 
	 * @param pid
	 */
	public static void stopProcess(long pid) {
		String cmd = "";
		String os = System.getProperty("os.name");
		if (os != null && os.toLowerCase().startsWith("windows")) {// Windows操作系统
			cmd = "taskkill /PID " + pid + " /F";
		}
		if (os != null && os.toLowerCase().startsWith("linux")) {// Linux操作系统
			cmd = "kill -s 9 " + pid;
		}
		LOGGER.info("正在执行命令 ====" + cmd);
		// 执行命令
		if (StringUtils.isNotBlank(cmd)) {
			RuntimeUtil.exec(cmd);
		}
	}

	/**
	 * 检测进程
	 * 
	 * @param process
	 */
	public static void mointor(Process process) {

		// 读取进程标准输出
		new Thread(() -> {
			try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					LOGGER.info(line);
				}
			} catch (IOException e) {
			}
		}).start();

		// 读取进程异常输出
		new Thread(() -> {
			try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					LOGGER.info(line);
				}
			} catch (IOException e) {
			}
		}).start();

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NuoleAQ

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值