I want to execute a cron job at a certain time (10pm) every day. Currently I am using following code but it is not working:
class ReviewBatchJob {
private static final logger = LogFactory.getLog(this)
def batchReviewScheduler
static triggers = {
cron cronExpression: "0 0 22 1/1 * ? *"
}
def execute() {
batchReviewScheduler.reviewBatch()
}
}
You should just be able to use an expression of 0 0 22 * * ?.
I'm assuming you're using the quartz plugin; if you don't have it installed, you'll need to. Here's a good reference with quartz cron expression examples.
Related
developing application using Grails 2.5.1 i used Quartz plugin , and created a job successfully , but when i inject a service in this job i get org.quartz.JobExecutionException: java.lang.NullPointerException
here is the Job's code:
class EveryMonthJob {
def usersUtilsService
static triggers = {
cron name: 'EveryOneMonthJob', cronExpression: "* 31 2 L * ?"
}
def execute() {
usersUtilsService.testMe() // getting the exception here
}
}
There are any number of reasons that might not work. If you are creating an instance of the job yourself (as opposed to Spring creating the instance and subjecting it to dependency injection), that would explain why the reference is null. Another explanation could be that you have the property name wrong.
See the project at https://github.com/jeffbrown/sherifquestion. That is a Grails 2.5.1 app that does just what you are describing and it works fine. See https://github.com/jeffbrown/sherifquestion/blob/e0179f836314dccb5f83861ae8466bfd99717995/grails-app/jobs/demo/EveryMonthJob.groovy which looks like this:
class EveryMonthJob {
// generally I would statically type this property but
// am leaving it dynamically typed top be consistent with
// a question being asked...
def usersUtilsService
static triggers = {
simple repeatInterval: 5000l // execute job once in 5 seconds
}
def execute() {
usersUtilsService.testMe() // this works fine
}
}
I'm actually working in Grails 3.x and I need to use Quartz Cron expressions. I've implemented successfully regular cron expression and works correctly, but now what I need to do is User (from a GUI) change that cron expression for any other he/she wants.
This is my Job
class ScheduleJob
{
static triggers =
{
cron name: 'myTrigger', cronExpression: "*/5 * * * * ?"
}
def execute()
{
println "------ Every 5 seconds"
}
}
I would appreciate so much if someone could help me to know how to do that, thanks for your time. :D
You have a number of options. One thing you can do is call ScheduleJob.schedule(' your cron expression goes here ').
You could also go get the job manager bean from the application context and reconfigure job beans there.
Some of the info at http://www.tothenew.com/blog/removing-triggers-and-rescheduling-a-quartz-2-job-programatically/ might be helpful.
I'm having trouble getting my Quartz Job in Grails to run concurrently as expected. Here is what my job looks like. I've commented out the lines that use the Executor plugin but when I don't comment them out, my code works as expected.
import java.util.concurrent.Callable
import org.codehaus.groovy.grails.commons.ConfigurationHolder
class PollerJob {
def concurrent = true
def myService1
def myService2
//def executorService
static triggers = {
cron name: 'pollerTrigger', startDelay:0, cronExpression: ConfigurationHolder.config.poller.cronExpression
}
def execute() {
def config = ConfigurationHolder.config
//Session session = null;
if (config.runPoller == true) {
//def result = executorService.submit({
myService1.doStuff()
myService2.doOtherStuff()
//} as Callable)
}
}
}
In my case, the myService2.doOtherStuff() is taking a very long time to complete which overlaps the next time this job should trigger. I don't mind if they overlap which is why I explicitly added def concurrent = true but it isn't working.
I have version 0.4.2 of the Quartz plugin and Grails 1.3.7. Am I doing something wrong here? Seems like a pretty straightforward feature to use. I'm not opposed to using the Executor plugin but it seems like I shouldn't have to.
I'm not sure it matters but the cronExpression I'm loading from config in this case is meant to execute this job every minute: "0 * * * * ?"
Apparently, there was a hidden config that I was not aware of that was keeping this from working. In my conf folder there was a file called quartz.properties which contained the following property:
org.quartz.threadPool.threadCount = 1
After increasing this number, my job was triggering even when it had not finished the previous execution.
hi there I'm using quartz plugin for grails.
when i have just 1 job (i used "create-job" command) everything works as expected!
this is how the job looks like and it will print every 1 second:
class MyFirstJob{
def concurrent = false
static triggers = {
simple name: 'myFirstJobTrigger', startDelay: 1000, repeatInterval: 1000 }
def group = "MyGroup"
def execute(){
println "MyFirstJob run!"
}
}
now if i add another job that should print every 5 sec that look like this:
class MySecondJob{
def concurrent = false
static triggers = {
simple name: 'mySecondJobTrigger', startDelay: 1000, repeatInterval: 5000 }
def group = "MyGroup"
def execute(){
println "MySecondJob run!"
}
}
what will happen now is that job1 will start working only every 5 seconds
it seems that quartz pluging can only have 1 job schedule
i was wondering what am i missing or doing wrong
i even tried the next 2 lines in a file called quartz.properties under conf directory:
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
thanks for your help
The plugin requires the job class filename to end in 'Job'. Therefore, make sure that MyJob2 is in a file named 'My2Job.groovy' in the job folder
I have an application using Grails Quartz plugin. I need to have two jobs to have multiple instances running, but have separate limitation on number of threads to be used for each job. As far as I understand, I need separate Thread Pools, which is possible by having separate schedulers. However, I cannot figure out how to create multiple schedulers with Quartz plugin.
Assuming you want to use different triggers to start the job multiple times. this works for me.
class MyJob {
static triggers = {
cron name: 'trigger1', cronExpression: "0 30 12 ? * WED"
cron name: 'trigger2', cronExpression: "0 30 12 ? * SAT"
}
def execute() {
// execute task, do your thing here
println "Job executed"
}
}
Finally, about concurrent tasks.
This is from the plug-in page:
By default Jobs are executed in concurrent fashion, so new Job
execution can start even if previous execution of the same Job is
still running.
Quartz plugin 2.0.13
According to the official documentation :
Multiple triggers per job are allowed.
For instance,
class MyJob {
static triggers = {
simple name:'simpleTrigger', startDelay:10000, repeatInterval: 30000, repeatCount: 10
cron name:'cronTrigger', startDelay:10000, cronExpression: '0/6 * 15 * * ?'
custom name:'customTrigger', triggerClass:MyTriggerClass, myParam:myValue, myAnotherParam:myAnotherValue
}