Quartz job for Grails only fires once

﹥>﹥吖頭↗ 提交于 2019-12-23 12:30:10

问题


I am trying to setup a cron job in my Grails web application using the Quartz plugin. I am currently simply trying to get a test job to execute once every second using the following code:

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

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

However, when I run the application I only see the initial output of the first execute() call twice: once immediately before I am alerted that the server is running, and once immediately after.

| 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

Does anyone know why my Quartz job might not be firing correctly? I have tried using a cron instead of simple as well as using varying parameters, time intervals, etc. Nothing has made a difference.

Thanks


回答1:


I think I had similar issues. You are not allowed to use System.out.println from within a quartz-job. Try to use log.error.




回答2:


Simple triggers have a repeatCount field. Set it to -1 for indefinite executions:

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



回答3:


In the documentation all examples have a name parameter in the triggers block:

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

I'd give that a shot first, even though the docs also say that a default value will be used if it's not given.




回答4:


I had the same issue and came to this conclusion:

You can use System.out.println in a Quartz Job. You have to separate out the methods with the print lines from the execute method. I had no luck with just calling just one method, but when calling two other methods, it repeats with the print lines correctly:

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")
   }
}

here is the output:

| 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

Hope this helps someone!



来源:https://stackoverflow.com/questions/11475944/quartz-job-for-grails-only-fires-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!