Grails的Quartz作业只会触发一次

时间:2012-07-13 18:07:24

标签: grails quartz-scheduler

我正在尝试使用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而不是简单,以及使用不同的参数,时间间隔等。没有什么有所作为。

由于

4 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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

我先给出一个镜头,即使文档还说如果没有给出默认值,也会使用。

答案 3 :(得分:0)

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

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

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

希望这有助于某人!