spring boot screen 命令行 输入

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

    }
}

 

转载于:https://my.oschina.net/ToFlySeif/blog/3004585

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值