Grails Quartz Plugin concurrent not working - grails

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.

Related

Injecting service in Quartz job in Grails

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

Stop cron job via quartz plugin 0.4.2 in grails 1.3.7

I have grails 1.3.7 and quartz plugin 0.4.2 for cron jobs.
class MyJob {
static triggers = {}
void execute() {
//some doings.
}
}
Somewhere in my code I have dynamic scheduling of my job like that:
MyJob.schedule(cronExpression)
Every time I schedule new cron job it creates new one and it works together with previously created jobs. But I want to replace old cron jobs of MyJob with new one each time.
Maybe I was wrong in my understanding but I tried to add
def concurrent = false
It didn't help.
Is there any way to stop all jobs of some job class?
Thank you!
UPD: As I understand there is no such thing as InterruptableJob in this version of quartz plugin. Am I right?
Finally I've found way how to unschedule job and schedule another one.
First I've added a group for job:
class MyJob {
def group = 'jobGroupName'
static triggers = {}
void execute() {
// some stuff
}
}
Also I've found JobManagerService inside plugin with very useful methods. But I was looking into source codes to get how it works correctly. After some time my solutions become in next code (maybe it's raw yet, but it works):
class MyService {
def jobManagerService
def rescheduleJob() {
def job = jobManagerService.getJob('jobGroupName').first()
Scheduler scheduler = jobManagerService.quartzScheduler
Trigger trigger = scheduler.getTriggersOfJob(job, 'jobGroupName').first()
if (!jobManagerService.unscheduleJob(trigger.group, trigger.name)) {
log.warn('Failed during unscheduling job')
} else {
MyJob.schedule(newCronExpression)
}
}
}
It's not clear sometimes which name and group required in service methods. So debug and sources helped me with this.

How to Configuring concurrent execution property as false in grails

Hi i am new to grails and I am using quartz plug-in for scheduling jobs. I scheduled job for every 60 sec but it is actually taking more than 60 sec some times So in that case one more threads is started and the first thread is still running So can any one tell me how to execute threads sequentially one by one.
When using the Grails Quartz plugin you can simply set the concurrent property to false to avoid concurrent executions of a Job:
class MyJob {
static triggers = {
...
}
def concurrent = false
def execute(context) {
...
}
}
If you are using Quartz as a plain dependency (not as a Grails plugin) you need to extend StatefulJob (Quartz < 2.0) or set the #StatefulJob and #DisallowConcurrentExecution annotations (Quartz >= 2.0).

How to set up a one-off job trigger at a specified time using Grails Quartz2 plugin

I am using Quartz2 Plugin, and am trying to dynamically trigger a very simple job. When the user performs a certain action, the job should be triggered some certain number of minutes in the future, and only run once.
I have tried using the simple 'schedule' method that takes a date and job data:
def sendTime = new Date()
use(groovy.time.TimeCategory) {
sendTime = sendTime + (connectionInstance.timeout).minutes
println "I will send the job at $sendTime"
}
ReportSmssyncTimeoutJob.schedule(sendTime, [connectionId:params.id])
In this setup, I find that the job actually triggers immediately instead of waiting until 'sendTime'.
My second attempt, after looking at the plugin source, was to use a SimpleTrigger
def sendTime = new Date()
use(groovy.time.TimeCategory) {
sendTime = sendTime + (connectionInstance.timeout).minutes
println "I will send the job at $sendTime"
}
// arguments here are: jobKey='test', startTime=sendTime, repeatCount=0, repeatInterval=1 (zero not allowed), job-arguments)
def trigger = TriggerHelper.simpleTrigger(new JobKey("test"), sendTime, 0, 1, [connectionId:params.id])
ReportSmssyncTimeoutJob.schedule(trigger)
In this setup, the job also triggers immediately. Is there something wrong with the SimpleTrigger implementation which prevents it from waiting until startDate?
Unfortunately, switching to the main 'quartz' plugin (which now has support for Quartz 2) is not an option as I am working on a project that has loads of jobs set up to work with the quartz2 plugin.
I asked this on the Grails mailing list and got the answer: this is a bug in the quartz2 plugin. It should be fixed in the next release (bug was noted in 0.2.3).
Update: tested this in v2.1.6.2 of the quartz2 plugin, and can confirm that both of the approaches in my question now work.

can not run multiple jobs with quartz plugin using grails

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

Resources