【问题标题】:spring batch database connection failure is not returning proper exit status春季批处理数据库连接失败未返回正确的退出状态
【发布时间】:2019-09-18 00:45:53
【问题描述】:

我已经浏览了几篇与类似问题相关的帖子,我正在通过 shell 脚本调用 spring 批处理应用程序并获取退出状态。成功执行后一切正常。 ExitStatus 填充为 0。但是,如果有任何数据库错误(创建数据库错误,我提供了错误的数据库端口),则 ExitStatus 将返回为空。代码如下

我参考了下面的帖子并类似地实现了

Make a spring-batch job exit with non-zero code if an exception is thrown

Spring batch return custom process exit code

Shell 脚本: java -jar $JOBDIR/lib/feed*.jar

result=$?
    echo $result            

Java:

 public static void main(String[] args) {
       ConfigurableApplicationContext context 
                  =SpringApplication.run(App.class, args);

    int exitCode = SpringApplication.exit(context);
    System.out.print("Exit code is" + exitCode);
    System.exit(exitCode);
  }

    @Primary
@Bean(destroyMethod = "")
public DataSource dataSource() throws Exception {
      return BatchDataSource.create(url, user, password);
    }

如果发生数据库错误,它甚至还没有到达主方法 System.exit(exitCode); 的末尾。任何人都可以指导我有什么问题吗??

【问题讨论】:

  • 这与 Spring Batch 本身无关。如果我理解正确,您的 Spring 应用程序上下文由于 bean 创建错误而无法启动,并且您希望在非零退出代码中捕获它。您是否尝试过以 System.exit 开头的 spring boot 应用程序包装?请参阅此处的示例:github.com/spring-projects/spring-boot/blob/master/….
  • 我也在做同样的事情(我已经发布了上面的代码)。由于数据源是在 spring 上下文之前构建的,它甚至没有达到设置退出代码的地步。

标签: spring-batch exitstatus


【解决方案1】:

如果有任何数据库错误(创建数据库错误我给了错误的数据库端口)然后 ExitStatus 被返回为空。

那是因为在这种情况下,您的工作根本没有执行。根据您的配置,dataSource bean 创建错误导致 Spring 应用程序上下文无法正确启动,您的作业甚至没有执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多