package com.example.until;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import sun.misc.Signal;
import sun.misc.SignalHandler;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author seif
* @Description: TODO
* @date 2019/1/2214:22
* @Modified by:
*/
public class Application {
protected void onStart() {
}
protected void onStop() {
}
protected void onCommand(String[] command) {
}
AtomicBoolean bQuit = new AtomicBoolean(false);
Scanner sc = new Scanner(System.in);
protected void run() {
Thread.currentThread().setName("ProcessMain");
bQuit.set(false);
hookQuit();
try {
onStart();
} catch (Exception ex) {
onerror("exception in onStart()", ex);
bQuit.set(true);
}
try {
while (true) {
String str = sc.nextLine();
String[] cmdfileds = str.split(" ");
if (cmdfileds != null && cmdfileds.length > 0) {
if (!syscmd(cmdfileds)) {
try {
onCommand(cmdfileds);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
} catch (Exception ex) {
onerror("exception in running", ex);
}
quit();
}
;
public void quit() {
bQuit.set(true);
sc.close();
try {
onStop();
} catch (Exception ex) {
onerror("exception in stop", ex);
}
System.exit(0);
}
protected boolean syscmd(String[] cmd) {
if ("quit".equalsIgnoreCase(cmd[0])) {
quit();
return true;
}
return false;
}
private void hookQuit() {
try {
SignalHandler handler = new SignalHandler() {
public void handle(Signal sig) {
Application.this.quit();
}
};
Signal.handle(new Signal("INT"), handler);
Signal.handle(new Signal("TERM"), handler);
Signal.handle(new Signal("USR1"), handler); // kill -10
} catch (IllegalArgumentException e) {
}
}
protected void onerror(String msg, Throwable ex) {
}
public static <T extends Application> void run(Class<T> cls, String[] args) {
Application app = null;
try {
ConfigurableApplicationContext ac = SpringApplication.run(cls, args);
app = ac.getBean(cls);
app.run();
} catch (Exception ex) {
if (app != null) app.onerror("start failed!", ex);
else {
try {
app = cls.getDeclaredConstructor().newInstance();
app.onerror("start failed!", ex);
} catch (Exception e) {
ex.printStackTrace();
}
}
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
}
package com.example.demo;
import com.example.until.Application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication extends Application {
public static void main(String[] args) {
Application.run(DemoApplication.class, args);
}
@Override
protected void onStart() {
System.out.println("Thread start");
}
@Override
protected void onStop() {
System.out.println("Thread end");
}
@Override
protected void onCommand(String[] command) {
if (command.length == 1 && "test".equalsIgnoreCase(command[0])) {
System.out.println("onCommand");
}
if (command.length == 2 && "test".equalsIgnoreCase(command[0])) {
String id = command[1];
System.out.println("onCommand" + id);
}
}
}