Reputation: 1156
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
Upvotes: 5
Views: 3269
Reputation: 163
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!
Upvotes: 0
Reputation: 3881
Simple triggers have a repeatCount field. Set it to -1 for indefinite executions:
simple name: "testName", repeatInterval: 1000, repeatCount: -1
Upvotes: 1
Reputation: 8099
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
.
Upvotes: 12
Reputation: 1037
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.
Upvotes: 0