gpt4 book ai didi

java - Spring批量返回自定义进程退出码

转载 作者:行者123 更新时间:2023-11-30 06:44:12 25 4
gpt4 key购买 nike

我有一个包含多个作业的 jar,我只想每次执行一个作业并检索自定义退出代码。

例如,我有一个基本的作业 (retrieveErrorsJob) 配置,其中一个步骤将读取一个输入 XML 文件并将数据写入特定的数据库表中。

应用类

@SpringBootApplication
@EnableBatchProcessing
@Import(CoreCommonsAppComponent.class)
public class Application {

private static final Logger logger = LoggerFactory.getLogger(Application.class);
private ConfigurationConstants constants;

@Autowired
public Application(ConfigurationConstants constants) {
this.constants = constants;
}

@EventListener(ApplicationStartedEvent.class)
public void idApplication()
{
logger.info("================================================");
logger.info(constants.APPLICATION_NAME() + "-v." + constants.APPLICATION_VERSION() + " started on " + constants.REMOTE_HOST());
logger.info("------------------------------------------------");
}

public static void main(String... args) throws Exception{
ApplicationContext context = SpringApplication.run(Application.class, args);
logger.info("================================================");
SpringApplication.exit(context);
}
}

我可以从命令行中选择一项工作:

java -jar my-jar.jar --spring.batch.job.names=retrieveErrorsJob --input.xml.file=myfile.xml

Spring Batch 开始正确的作业。

问题是我需要 jar 返回自定义进程退出整数,如 ExitCode.FAILED == 4 等。但我总是有一个零(如果 ExitCode = SUCCESS 或 FAILED)。

根据文档,我需要实现 ExitCodeMapper 接口(interface)。

代码(未完成)

public class CustomExitCodeMapper implements ExitCodeMapper {

private static final int NORMAL_END_EXECUTION = 1;
private static final int NORMAL_END_WARNING = 2;
private static final int ABNORMAL_END_WARNING = 3;
private static final int ABNORMAL_END_ERROR = 4;

@Override
public int intValue(String exitCode) {

System.out.println("EXIT CODE = " + exitCode);

switch (exitCode)
{
case "FAILED":
return ABNORMAL_END_WARNING;
default:
return NORMAL_END_EXECUTION;
}
}

我找不到使用此自定义实现的方法。我可以将自定义实现设置为 CommandLineJobRunner 但如何使用此类?

最佳答案

感谢@Mahendra,我有了一个主意:)

我按照@Mahendra 的建议创建了一个JobCompletionNotificationListener 类:

@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {

private static final Logger logger = LoggerFactory.getLogger(JobCompletionNotificationListener.class);

@Override
public void afterJob(JobExecution jobExecution) {

SingletonExitCode exitCode = SingletonExitCode.getInstance();

if(jobExecution.getStatus() == BatchStatus.COMPLETED)
{
logger.info("Exit with code " + ExitCode.NORMAL_END_OF_EXECUTION);
exitCode.setExitCode(ExitCode.NORMAL_END_OF_EXECUTION);
}
else {
logger.info("Exit with code " + ExitCode.ABNORMAL_END_OF_EXECUTION_WARNING);
exitCode.setExitCode(ExitCode.ABNORMAL_END_OF_EXECUTION_WARNING);
}
}
}

但我不会强制应用程序使用此类中的 System.exit() 退出。我已经实现了一个像这样的简单单例:

public class SingletonExitCode {

public ExitCode exitCode = ExitCode.ABNORMAL_END_OF_EXECUTION_WARNING; // Default code 3
private static SingletonExitCode instance = new SingletonExitCode();

private SingletonExitCode() {}

public static SingletonExitCode getInstance() {
return instance;
}

public void setExitCode(ExitCode exitCode) {
this.exitCode = exitCode;
}
}

在关闭 Spring 上下文后,我从我的单例中询问 ExitCode:

@SpringBootApplication
@EnableBatchProcessing
@Import(CoreCommonsAppComponent.class)
public class Application {
// a lot of nice things

public static void main(String... args) throws Exception{
ApplicationContext context = SpringApplication.run(Application.class, args);
logger.info("================================================");
SpringApplication.exit(context);
System.exit(SingletonExitCode.getInstance().exitCode.getCode());
}
}

我这样做是因为如果我们直接退出 JobCompletionNotificationListener 类,我们会错过日志中的重要一行:

Job: [FlowJob: [name=writeErrorFromFile]] completed with the following parameters: [{-input.xml.file=c:/temp/unit-test-error.xml, -spring.batch.job.names=writeErrorFromFile, run.id=15, input.xml.file=c:/temp/unit-test-error.xml}] and the following status: [FAILED]

并且似乎 Spring 上下文没有正确关闭。

关于java - Spring批量返回自定义进程退出码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com