VIVIMAN
3 years ago
9 changed files with 364 additions and 6 deletions
@ -0,0 +1,12 @@ |
|||
call :IsAdmin |
|||
|
|||
java -jar app.jar |
|||
|
|||
:IsAdmin |
|||
Reg.exe query "HKU\S-1-5-19\Environment" |
|||
If Not %ERRORLEVEL% EQU 0 ( |
|||
Cls & Echo You must have administrator rights to continue ... |
|||
Pause & Exit |
|||
) |
|||
Cls |
|||
goto:eof |
@ -0,0 +1,309 @@ |
|||
package com.insigma.utils; |
|||
|
|||
import lombok.NoArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thymeleaf.util.StringUtils; |
|||
|
|||
import java.io.*; |
|||
import java.nio.charset.Charset; |
|||
|
|||
/** |
|||
* Windows系统工具类 |
|||
* |
|||
* @author Zaki Chen |
|||
*/ |
|||
@Slf4j |
|||
@NoArgsConstructor |
|||
public class WinServiceTool { |
|||
|
|||
/** |
|||
* 获取windows服务实例对象 |
|||
* |
|||
* @param serviceName |
|||
* @return |
|||
*/ |
|||
public static WindowsService getService(String serviceName) { |
|||
return new WindowsService(serviceName, Charset.defaultCharset()); |
|||
} |
|||
|
|||
/** |
|||
* 获取windows服务实例对象 |
|||
* |
|||
* @param serviceName |
|||
* @param charset windows命令执行输出流字符集 |
|||
* @return |
|||
*/ |
|||
public static WindowsService getService(String serviceName, Charset charset) { |
|||
return new WindowsService(serviceName, charset); |
|||
} |
|||
|
|||
/** |
|||
* 获取windows服务实例对象 |
|||
* |
|||
* @param serviceName |
|||
* @param charset windows命令执行输出流字符集 |
|||
* @return |
|||
*/ |
|||
|
|||
public static WindowsService getService(String serviceName, String charset) { |
|||
return new WindowsService(serviceName, Charset.forName(charset)); |
|||
} |
|||
|
|||
/** |
|||
* 读取输入流中的文本信息 |
|||
* |
|||
* @param input 文本输入流 |
|||
* @param charset 文本编码 |
|||
* @return |
|||
*/ |
|||
private static String convertInputStream2Str(InputStream input, Charset charset) { |
|||
final char[] buffer = new char[4096]; |
|||
final StringBuilder out = new StringBuilder(); |
|||
try { |
|||
final Reader in = new InputStreamReader(input, charset); |
|||
try { |
|||
for (; ; ) { |
|||
int rsz = in.read(buffer, 0, buffer.length); |
|||
if (rsz < 0) { |
|||
break; |
|||
} |
|||
out.append(buffer, 0, rsz); |
|||
} |
|||
} finally { |
|||
in.close(); |
|||
} |
|||
} catch (Exception ex) { |
|||
log.error("执行发生异常:{}", ex.getMessage()); |
|||
} |
|||
return out.toString(); |
|||
} |
|||
|
|||
public static class WindowsService { |
|||
|
|||
/** |
|||
* 服务名称 |
|||
*/ |
|||
private String serviceName; |
|||
/** |
|||
* 服务是否存在 |
|||
*/ |
|||
private boolean isExists; |
|||
/** |
|||
* 服务运行状态 |
|||
*/ |
|||
private ServiceState runningStatus; |
|||
/** |
|||
* 服务信息 |
|||
*/ |
|||
private String info; |
|||
|
|||
/** |
|||
* windows状态码服务_运行中 |
|||
*/ |
|||
private final static String SERVICE_RUNNING_CODE = "4"; |
|||
|
|||
/** |
|||
* windows状态码服务_正在停止运行 |
|||
*/ |
|||
private final static String SERVICE_STOPPING_CODE = "3"; |
|||
|
|||
/** |
|||
* windows状态码服务_已停止运行 |
|||
*/ |
|||
private final static String SERVICE_STOPPED_CODE = "1"; |
|||
|
|||
/** |
|||
* windows操作系统cmd指令返回内容的字符集 |
|||
*/ |
|||
private Charset charset; |
|||
|
|||
/** |
|||
* 直接完成状态的初始化 |
|||
* |
|||
* @param serviceName |
|||
*/ |
|||
public WindowsService(String serviceName, Charset charset) { |
|||
this.serviceName = serviceName; |
|||
this.charset = charset; |
|||
Runtime runtime = Runtime.getRuntime(); |
|||
String result; |
|||
try { |
|||
Process process = runtime.exec("sc query " + serviceName); |
|||
InputStream inputStream = process.getInputStream(); |
|||
result = convertInputStream2Str(inputStream, Charset.defaultCharset()); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
isExists = false; |
|||
runningStatus = ServiceState.UNKNOWN; |
|||
info = null; |
|||
return; |
|||
} |
|||
String[] split = result.split("\n"); |
|||
if (split.length < 5) { |
|||
isExists = false; |
|||
runningStatus = ServiceState.UNKNOWN; |
|||
info = null; |
|||
return; |
|||
} |
|||
isExists = true; |
|||
String state = split[3].split(":")[1].trim(); |
|||
String code = state.substring(0, 1); |
|||
info = state.substring(3); |
|||
if (SERVICE_RUNNING_CODE.equals(code)) { |
|||
runningStatus = ServiceState.RUNNING; |
|||
} else if (SERVICE_STOPPING_CODE.equals(code)) { |
|||
runningStatus = ServiceState.STOPPING; |
|||
} else if (SERVICE_STOPPED_CODE.equals(code)) { |
|||
runningStatus = ServiceState.STOPPED; |
|||
} else { |
|||
runningStatus = ServiceState.UNKNOWN; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 重启服务 |
|||
* |
|||
* @return 执行结果 |
|||
*/ |
|||
public boolean restartService() throws Exception { |
|||
if (!isExists) { |
|||
return false; |
|||
} |
|||
boolean isStop = false; |
|||
if (getRunningStatus() == ServiceState.RUNNING) { |
|||
isStop = stopService(); |
|||
} |
|||
if (isStop) { |
|||
boolean isStart = startService(); |
|||
if (isStart) { |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 启动服务 |
|||
* |
|||
* @return 执行结果 |
|||
*/ |
|||
public boolean startService() throws Exception { |
|||
if (!isExists) { |
|||
return false; |
|||
} |
|||
boolean ret = false; |
|||
if (getRunningStatus() == ServiceState.STOPPED) { |
|||
try { |
|||
Process exec = Runtime.getRuntime().exec("net start " + serviceName); |
|||
InputStream errorStream = exec.getErrorStream(); |
|||
String errMsg = convertInputStream2Str(errorStream, getCharset()); |
|||
if (StringUtils.isEmpty(errMsg)) { |
|||
throw new Exception("执行启动服务[" + getServiceName() + "]出错:\n" + errMsg); |
|||
} |
|||
runningStatus = ServiceState.RUNNING; |
|||
ret = true; |
|||
} catch (IOException e) { |
|||
log.error("执行发生异常:{}", e.getMessage()); |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
/** |
|||
* 停止服务 |
|||
* |
|||
* @return 执行结果 |
|||
*/ |
|||
public boolean stopService() throws Exception { |
|||
if (!isExists) { |
|||
return false; |
|||
} |
|||
boolean ret = false; |
|||
if (getRunningStatus() == ServiceState.RUNNING) { |
|||
try { |
|||
Process exec = Runtime.getRuntime().exec("net stop " + serviceName); |
|||
InputStream errorStream = exec.getErrorStream(); |
|||
String errMsg = convertInputStream2Str(errorStream, getCharset()); |
|||
if (StringUtils.isEmpty(errMsg)) { |
|||
throw new Exception("执行停止服务[" + getServiceName() + "]出错:\n" + errMsg); |
|||
} |
|||
runningStatus = ServiceState.STOPPED; |
|||
ret = true; |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
private enum ServiceState { |
|||
/** |
|||
* 正在运行 |
|||
*/ |
|||
RUNNING, |
|||
/** |
|||
* 正在停止 |
|||
*/ |
|||
STOPPING, |
|||
/** |
|||
* 已停止 |
|||
*/ |
|||
STOPPED, |
|||
/** |
|||
* 未知(服务不存在) |
|||
*/ |
|||
UNKNOWN |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 服务名 |
|||
* |
|||
* @return |
|||
*/ |
|||
public String getServiceName() { |
|||
return serviceName; |
|||
} |
|||
|
|||
/** |
|||
* 服务是否存在 |
|||
* |
|||
* @return |
|||
*/ |
|||
public boolean isExists() { |
|||
return isExists; |
|||
} |
|||
|
|||
/** |
|||
* 服务运行状态 |
|||
* |
|||
* @return |
|||
*/ |
|||
public ServiceState getRunningStatus() { |
|||
return runningStatus; |
|||
} |
|||
|
|||
/** |
|||
* 服务信息 |
|||
* |
|||
* @return |
|||
*/ |
|||
public String getInfo() { |
|||
return info; |
|||
} |
|||
|
|||
public Charset getCharset() { |
|||
return charset; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "WindowsService{" + |
|||
"serviceName='" + serviceName + '\'' + |
|||
", isExists=" + isExists + |
|||
", runningStatus=" + runningStatus + |
|||
", info='" + info + '\'' + |
|||
'}'; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
package com.insigma.utils; |
|||
|
|||
import com.insigma.config.AppCfg; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
@Slf4j |
|||
public class WinServiceToolTest { |
|||
|
|||
static { |
|||
DbUtil.url = "jdbc:mysql://127.0.0.1:35017/hy_qggwy"; |
|||
DbUtil.usr = "root"; |
|||
DbUtil.pwd = "admin"; |
|||
AppCfg.DB = "GWY20_Mysql"; |
|||
AppCfg.MW = "GWY20_Tomcat"; |
|||
AppCfg.HZB = "D:/hzb2021"; |
|||
AppCfg.BROWSER = "360se.exe"; |
|||
} |
|||
|
|||
@Test |
|||
public void getService() throws Exception { |
|||
|
|||
WinServiceTool.WindowsService windowsService = WinServiceTool.getService(AppCfg.DB, "GBK"); |
|||
log.info("停止代码:{}", windowsService); |
|||
assertTrue(windowsService.stopService()); |
|||
log.info("启动代码:{}", windowsService); |
|||
assertTrue(windowsService.startService()); |
|||
log.info("重启代码:{}", windowsService); |
|||
assertTrue(windowsService.restartService()); |
|||
} |
|||
} |
Loading…
Reference in new issue