Spring 脚 return 命令行应用程序的退出代码
Spring boot return exit code for CommandLine app
我有一个 Spring 引导应用程序实现了 CommandLineRunner。如果有任何 error/exception 发生,我想 return -1 作为退出代码,如果没有异常,我想 return 0。
public class MyApplication implements CommandLineRunner{
private static Logger logger = LoggerFactory.getLogger(MyApplication.class);
@Override
public void run(String... args) throws Exception {
// to do stuff. exception may happen here.
}
public static void main(String[] args) {
try{
readSetting(args);
SpringApplication.run(MyApplication.class, args).close();
}catch(Exception e){
logger.error("######## main ########");
java.util.Date end_time = new java.util.Date();
logger.error(e.getMessage(), e);
logger.error(SystemConfig.AppName + " System issue end at " + end_time);
System.exit(-1);
}
System.exit(0);
}
...
}
我已经尝试了System.exit(), SpringApplication.exit(MyApplication.context, exitCodeGenerator), 等等,但是当我抛出异常时它仍然是return 0!
我尝试过这里的解决方案:
https://sdqali.in/blog/2016/04/17/programmable-exit-codes-for-spring-command-line-applications/
请帮忙!
https://www.baeldung.com/spring-boot-exit-codes 上有一篇很好的文章回答了您的问题。这是要点:
@SpringBootApplication
public class CLI implements CommandLineRunner, ExitCodeGenerator {
private int exitCode; // initialized with 0
public static void main(String... args) {
System.exit(SpringApplication.exit(SpringApplication.run(CLI.class, args)));
}
/**
* This is overridden from CommandLineRunner
*/
@Override
public void run(String... args) {
// Do what you have to do, but don't call System.exit in your code
this.exitCode = 1;
}
/**
* This is overridden from ExitCodeGenerator
*/
@Override
public int getExitCode() {
return this.exitCode;
}
}
我有一个 Spring 引导应用程序实现了 CommandLineRunner。如果有任何 error/exception 发生,我想 return -1 作为退出代码,如果没有异常,我想 return 0。
public class MyApplication implements CommandLineRunner{
private static Logger logger = LoggerFactory.getLogger(MyApplication.class);
@Override
public void run(String... args) throws Exception {
// to do stuff. exception may happen here.
}
public static void main(String[] args) {
try{
readSetting(args);
SpringApplication.run(MyApplication.class, args).close();
}catch(Exception e){
logger.error("######## main ########");
java.util.Date end_time = new java.util.Date();
logger.error(e.getMessage(), e);
logger.error(SystemConfig.AppName + " System issue end at " + end_time);
System.exit(-1);
}
System.exit(0);
}
...
}
我已经尝试了System.exit(), SpringApplication.exit(MyApplication.context, exitCodeGenerator), 等等,但是当我抛出异常时它仍然是return 0!
我尝试过这里的解决方案:
https://sdqali.in/blog/2016/04/17/programmable-exit-codes-for-spring-command-line-applications/
请帮忙!
https://www.baeldung.com/spring-boot-exit-codes 上有一篇很好的文章回答了您的问题。这是要点:
@SpringBootApplication
public class CLI implements CommandLineRunner, ExitCodeGenerator {
private int exitCode; // initialized with 0
public static void main(String... args) {
System.exit(SpringApplication.exit(SpringApplication.run(CLI.class, args)));
}
/**
* This is overridden from CommandLineRunner
*/
@Override
public void run(String... args) {
// Do what you have to do, but don't call System.exit in your code
this.exitCode = 1;
}
/**
* This is overridden from ExitCodeGenerator
*/
@Override
public int getExitCode() {
return this.exitCode;
}
}