Spring批处理返回自定义流程退出代码 [英] Spring batch return custom process exit code

查看:168
本文介绍了Spring批处理返回自定义流程退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个罐子里有多个作业,我想每次只执行一个作业并检索自定义退出代码.

I have one jar with several jobs, I want to execute only one job each time and retrieve a custom exit code.

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

For example, I have basic job (retrieveErrorsJob) configuration with one step that will read an input XML file and write the data in specific database table.

应用程序类

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

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

I can choose one job from command line:

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

Spring Batch开始正确的工作.

Spring Batch starts the correct job.

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

The problem is that I need the jar to return a custom process exit integer like ExitCode.FAILED == 4 etc. But I always have a ZERO (if ExitCode = SUCCESS or FAILED).

根据文档,我需要实现ExitCodeMapper接口.

As per the docs, I need to implement 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,但是如何使用此类?

I can't find a way to use this custom implementation. I could set the custom implementation to CommandLineJobRunner but how to use this class?

推荐答案

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

Thanks to @Mahendra I've got an idea :)

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

I've created a JobCompletionNotificationListener class as @Mahendra suggested:

@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()退出此类.我已经实现了一个像这样的简单单例:

But I don't force the application to exit with System.exit() from this class. I've implemented a simple singleton like this:

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:

and I ask the ExitCode from my singleton after closing Spring context:

@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类退出,则会错过日志中的重要一行:

I did this because if we exit directly from JobCompletionNotificationListener class we miss an important line in the logs:

作业:[FlowJob:[name = writeErrorFromFile]]使用以下参数完成:[{-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}]和以下状态:[FAILED]

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上下文没有正确关闭.

And seems that Spring context is not properly closed.

这篇关于Spring批处理返回自定义流程退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆