【问题标题】:Spring batch as windows serviceSpring批处理作为Windows服务
【发布时间】:2014-09-28 01:07:49
【问题描述】:

简介

我是 Spring Batch 的新手,但有使用 Spring 和 Java 的经验。我已经制作了自己的包装器,以便能够使用 Java 创建 Windows 服务。我的包装器中的主要 API 可以这样使用:

    try {
        // shutdown is volatile protected boolean of my runner class which is set by my wrapper when stop request occurs
        while (!shutdown) {
            // Action to be done
            log.info("I am running ..."); 
            Thread.sleep(MyConstants.WAIT);
            // End of action
        }
        log.info("Closing daemon thread.");
    } catch (Exception ex) {
        log.error("Runtime service error.", ex);
        throw new ServiceException(ex);
    }

问题描述

上面的工作非常好,我已经能够多次使用它来实现业务逻辑,而不是在各种 ms windows 和 Linux 服务中使用 log.info("I am running ...")。但现在我想使用 Spring Batch + Boot。到目前为止,能够通过 SpringApplication.run(...) 运行业务逻辑,但我需要能够在关闭时安全地停止它,而我正在寻找的是最好的方法。

问题

是否有人有一个优雅的解决方案来检查关闭请求并正确关闭通过 Spring boot 调用的 spring 批处理作业?

我能够设计的东西

  • 创建单例线程安全 Spring bean Shutdown 类处理关闭状态。
  • 从 MAIN 创建的新线程 T1 运行 SpringApplication.run(...)。
  • 当操作系统关闭状态时,通过 MAIN 线程设置 Shutdown bean 的关闭属性。
  • 通过 Shutdown bean 检查每个 Spring Batch Reader 和 Transformer 的关闭状态并抛出 JobInterruptedException 让 Spring Batch 停止。

我认为这个解决方案应该可行,但在我看来它过于复杂且过于臃肿。

【问题讨论】:

    标签: java windows-services spring-batch


    【解决方案1】:

    我已经能够使用以下概念以更优雅的方式解决它:

    • 在我的主要服务上使用 @Configuration 以便能够 @Inject 它在读取器、处理器、写入器中的布尔状态
    • 使用 JobMonitor 处理作业

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多