VIVIMAN
3 years ago
11 changed files with 145 additions and 60 deletions
@ -0,0 +1,3 @@ |
|||||
|
Manifest-Version: 1.0 |
||||
|
Main-Class: com.insigma.utils.OsTest |
||||
|
|
@ -1,23 +1,17 @@ |
|||||
package com.insigma.service; |
package com.insigma.service; |
||||
|
|
||||
import org.springframework.util.Assert; |
|
||||
|
|
||||
/** |
/** |
||||
* (Computer 计算机 接口) |
* (Computer 计算机 接口) |
||||
* |
* |
||||
* @author zhangxianwei |
* @author zhangxianwei |
||||
* @since 2022/4/17 21:07 |
* @since 2022/4/17 21:07 |
||||
*/ |
*/ |
||||
public interface Computer extends Middleware, Database { |
public abstract class Computer { |
||||
|
|
||||
/*** |
/*** |
||||
* 执行 脚本程序 |
* 执行 脚本程序 |
||||
* @param shell 执行脚本 |
* @param shell 执行脚本 |
||||
* @return |
* @return |
||||
*/ |
*/ |
||||
default boolean runShell(String shell) { |
public abstract boolean runShell(String shell); |
||||
Assert.notNull(shell, "未检测到要执行脚本!"); |
|
||||
System.out.printf("执行脚本:%s,但是未检测到实现应用调用此方法!%n", shell); |
|
||||
return false; |
|
||||
} |
|
||||
} |
} |
||||
|
@ -1,32 +0,0 @@ |
|||||
package com.insigma.service.impl; |
|
||||
|
|
||||
import com.insigma.entry.IndexObj; |
|
||||
import com.insigma.service.Computer; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
/** |
|
||||
* (CuWindows 类) |
|
||||
* |
|
||||
* @author zhangxianwei |
|
||||
* @since 23:30 2022/4/17 |
|
||||
*/ |
|
||||
public class CuWindows implements Computer { |
|
||||
|
|
||||
@Override |
|
||||
public boolean rebuildIndex(List<IndexObj> index) { |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public boolean setDbSize(int size) { |
|
||||
System.out.printf("设置出参数:%d,但是未检测到实现应用调用此方法!", size); |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public boolean cleanDbCache() { |
|
||||
System.out.println("进行缓存清除,但是未检测到实现应用调用此方法!"); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,53 @@ |
|||||
|
package com.insigma.service.impl; |
||||
|
|
||||
|
import com.insigma.entry.IndexObj; |
||||
|
import com.insigma.service.Computer; |
||||
|
import com.insigma.service.*; |
||||
|
import com.insigma.utils.WinCommandUtil; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* (CuWindows 类) windows + tomcat + mysql |
||||
|
* |
||||
|
* @author zhangxianwei |
||||
|
* @since 23:30 2022/4/17 |
||||
|
*/ |
||||
|
public class WindowsTomcatMysql extends Computer implements Database, Middleware { |
||||
|
|
||||
|
@Override |
||||
|
public boolean runShell(String shell) { |
||||
|
try { |
||||
|
new WinCommandUtil(shell).run(); |
||||
|
}catch (Exception ignored){ |
||||
|
return false; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean rebuildIndex(List<IndexObj> index) { |
||||
|
return Database.super.rebuildIndex(index); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean setDbSize(int size) { |
||||
|
return Database.super.setDbSize(size); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean cleanDbCache() { |
||||
|
return Database.super.cleanDbCache(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean cleanMwCache() { |
||||
|
return Middleware.super.cleanMwCache(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean setMwSize(int size) { |
||||
|
return Middleware.super.setMwSize(size); |
||||
|
} |
||||
|
} |
@ -0,0 +1,66 @@ |
|||||
|
package com.insigma.utils; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import java.io.*; |
||||
|
|
||||
|
/** |
||||
|
* CommandImpUtil |
||||
|
* |
||||
|
* @author admin |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class WinCommandUtil implements Runnable { |
||||
|
private String command; |
||||
|
|
||||
|
public WinCommandUtil(String command) { |
||||
|
this.command = command; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void run() { |
||||
|
Process process; |
||||
|
int exitVal = 0; |
||||
|
try { |
||||
|
log.debug("准备执行命令:{}",command); |
||||
|
process = Runtime.getRuntime().exec(command); |
||||
|
// Runtime.exec()创建的子进程公用父进程的流,不同平台上,父进程的stream buffer可能被打满导致子进程阻塞,从而永远无法返回。
|
||||
|
//针对这种情况,我们只需要将子进程的stream重定向出来即可。
|
||||
|
new RunCmdStreamThread(process.getInputStream(), "INFO").start(); |
||||
|
new RunCmdStreamThread(process.getErrorStream(), "ERR").start(); |
||||
|
|
||||
|
exitVal = process.waitFor(); |
||||
|
} catch (IOException | InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
if (exitVal != 0) { |
||||
|
log.error("执行命令发生异常:{}", exitVal); |
||||
|
throw new RuntimeException("cmd任务执行失败"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static class RunCmdStreamThread extends Thread { |
||||
|
InputStream is; |
||||
|
String printType; |
||||
|
|
||||
|
RunCmdStreamThread(InputStream is, String printType) { |
||||
|
this.is = is; |
||||
|
this.printType = printType; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void run() { |
||||
|
try { |
||||
|
InputStreamReader isr = new InputStreamReader(is); |
||||
|
BufferedReader br = new BufferedReader(isr); |
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
log.debug("输出:{}>{}", printType, line); |
||||
|
} |
||||
|
|
||||
|
} catch (IOException ioe) { |
||||
|
ioe.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.insigma.service.impl; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
public class WindowsTomcatMysqlTest { |
||||
|
|
||||
|
@Test |
||||
|
public void runShellTest(){ |
||||
|
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue