windows Java程序一旦阻塞就让他重启的脚本怎么写

我的java程序是bat调用jar包启动的,运行一段时间会出现JAVA SE。。。 已停止运行的弹框,这个时候java进程也没关闭,是处于一种卡死阻塞的状态,浏览器访问系统会一直连接响应,但是连接不上,现在这种状态是不可控的,我就想每当出现这种状态就去重启程序,想写个这样的脚本,望大佬指点!!!!急

最好是你的程序内部做处理,做个检测功能,有问题让程序自己重新启动一个实例。

socket有心跳连接

在脚本里连接java程序服务端口,超时就杀掉进程,重启

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class JavaHttpUrlConnectionReader {
	public static void main(String[] args) {
 
		String myUrl = "http://localhost:8080/jspwebmon/validate.jsp?username=test&password=test";
 
		URL url = null;
		BufferedReader reader = null;
		StringBuilder stringBuilder;
 
		try {
			// create the HttpURLConnection
			url = new URL(myUrl);
			CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
 
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
			// just want to do an HTTP GET here
			connection.setRequestMethod("GET");
 
			// give it 15 seconds to respond
			connection.setConnectTimeout(15 * 1000);
			connection.setReadTimeout(15 * 1000);
 
			long startTimerConnect = System.currentTimeMillis();
 
			connection.connect();
			long stopTimerConnect = System.currentTimeMillis();
			int connectTime = (int) ((int) stopTimerConnect - startTimerConnect);
 
			// read the output from the server
			long startTimerRead = System.currentTimeMillis();
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			stringBuilder = new StringBuilder();
 
			String line = null;
			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line + "\n");
			}
			System.out.println(stringBuilder.toString());
			long stopTimerRead = System.currentTimeMillis();
			int readTime = (int) ((int) stopTimerRead - startTimerRead);
 
			System.out.println("connectTime:" + connectTime);
			System.out.println("readTime:" + readTime);
 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// close the reader; this can throw an exception too, so
			// wrap it in another try/catch block.
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}
}

 

我觉得你需要解决的不是,发现出现卡死这种情况然后怎么写脚本重启,而是要找到为什么会出现卡死这种情况,解决这个问题。

不过我觉得写脚本也可以,就是写脚本去访问接口然后超时的话应该会报错,然后需要脚本有种方法来判断是否超时报错,然后是这种情况就kill掉程序重启。