【问题标题】:Quartz job for Grails only fires onceGrails 的 Quartz 作业只触发一次
【发布时间】:2012-07-13 18:07:24
【问题描述】:

我正在尝试使用Quartz plugin 在我的 Grails Web 应用程序中设置一个 cron 作业。我目前只是尝试使用以下代码每秒执行一次测试作业:

class TestJob {
    private int counter = 0
    static triggers = {
        simple repeatInterval: 1000
    }

    def execute() {
        // execute job
        counter += 1
        System.out.println("Testing the cron " + counter)
    }
}

但是,当我运行该应用程序时,我只能看到第一个 execute() 调用的初始输出两次:一次是在我收到服务器正在运行的警报之前,一次是在之后。

| Loading Grails 2.1.0
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application.....
| Compiling 1 source files.....
| Running Grails application
Testing the cron 1
| Server running. Browse to http://localhost:8080/QuartzTest
Testing the cron 1

有人知道为什么我的 Quartz 作业可能无法正确触发吗?我尝试过使用 cron 而不是 simple 以及使用不同的参数、时间间隔等。没有什么不同。

谢谢

【问题讨论】:

    标签: grails quartz-scheduler


    【解决方案1】:

    我想我也有类似的问题。您不能在石英作业中使用System.out.println。尝试使用log.error

    【讨论】:

    • 嗯...我的行为完全相同,但更改为 log.error 没有任何区别。相反,将“计数器”更改为静态解决了问题(即使使用 println),但我不明白为什么这会影响作业触发。
    • 为我工作。顺便问一下为什么会有这样的行为?
    • 只是一个更新,我正在使用插件的 1.0-RC9 版本(它使用 Quartz 2.2)并且 println 在 2 个作业中工作 - 1 在 grails-app/jobs 和另一个我定义和安排使用调度程序从 JobManagerService 中获取。
    • 哇...在工作中还有其他不允许的事情吗?这是相当可怕的。我的工作是调用一些服务方法。我不是 100% 确定它们是如何实施的。他们会中断工作吗? (我使用的是 1.0-RC9)
    【解决方案2】:

    简单的触发器有一个repeatCount 字段。将其设置为 -1 以无限期执行:

    simple name: "testName", repeatInterval: 1000, repeatCount: -1
    

    【讨论】:

      【解决方案3】:

      在文档中,所有示例的触发器块中都有一个name 参数:

      static triggers = {
            simple name: "testName", repeatInterval: 1000      
      }
      

      我会先试一试,尽管文档还说如果没有给出默认值,将使用它。

      【讨论】:

      • 其实我原来有name参数,还有trigger的startDelay参数,好像还是只执行了两次。
      • 在文档中我刚刚找到“默认情况下,在测试环境下运行时不会执行作业”这一行。我不确定这意味着什么,因为显然它运行了两次。
      • 是的,我也看到了。不过,正如我的输出所示,我的环境应该设置为开发环境,因此这不会导致问题。
      • 实际上,看起来它只执行一次,而不是像我想的那样执行两次。查看更新的代码以查看:我添加了一个不递增的计数器
      【解决方案4】:

      我遇到了同样的问题,得出了这个结论:

      您可以在 Quartz Job 中使用 System.out.println。您必须将带有打印行的方法与执行方法分开。我只调用一种方法没有运气,但是当调用另外两种方法时,它会正确地重复打印行:

      class TestJob {
          static triggers = {
          simple name: 'testTrigger', startDelay: 1000, repeatInterval: 1000, repeatCount: -1
          }
      
          def execute() {
              exampleMethod()
              anotherMethod()
          }
      
          def exampleMethod(){
              System.out.println("test")
          }
      
          def anotherMethod(){
              System.out.println("another method")
         }
      }
      

      这是输出:

      | Loading Grails 2.1.1
      | Configuring classpath.
      | Environment set to development.....
      | Packaging Grails application.....
      | Compiling 2 source files.....
      | Running Grails application
      
      Configuring Spring Security UI ...
      ... finished configuring Spring Security UI
      
      
      Configuring Spring Security Core ...
      ... finished configuring Spring Security Core
      
      test
      another method
      | Server running. Browse to http://localhost:8080/
      test
      another method
      test
      another method
      test
      another method
      test
      another method
      test
      another method
      

      希望这对某人有所帮助!

      【讨论】:

      • 我不需要两种方法。诀窍是有两个println。这太奇怪了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2011-10-25
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多