栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA进程管理工具类(ProcessUtil)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
	
}



   net.java.dev.jna
	jna
	5.11.0



	cn.hutool
	hutool-system
	5.7.22

 

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);

	}

	
	public static Long startProcess(String cmd) {
		Process process = RuntimeUtil.exec(cmd);
		mointor(process);// 打印检测
		return getProcessPID(process);
	}

	
	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;
	}

	
	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);
		}
	}

	
	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();

	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号