Permalink
Browse files

Non-concurrent jobs support in persistent store

  • Loading branch information...
chupakabr committed Dec 12, 2012
1 parent 4888429 commit 1f95f4543d29ee2891d710dd403efa6fffc7062f
@@ -16,12 +16,8 @@
package grails.plugin.quartz2;
-import org.quartz.JobDetail;
-import org.quartz.JobKey;
-import org.springframework.beans.factory.FactoryBean;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Required;
-import org.springframework.scheduling.quartz.SchedulerFactoryBean;
+import org.springframework.beans.factory.FactoryBean
+import org.springframework.beans.factory.InitializingBean
/**
*
@@ -49,8 +45,18 @@ public class GrailsArtefactJobDetailFactoryBean implements FactoryBean, Initiali
// Build JobDetail instance.
jobDetail = new SimpleJobDetail();
jobDetail.setKey(grailsJobClass.getJobKey());
- jobDetail.setJobClass(GrailsArtefactJob.class);
jobDetail.setConcurrent(grailsJobClass.isConcurrent());
+
+ // Different artefacts for concurrent job and non-concurrent job so it will work with
+ // clustered persistent job store like org.quartz.impl.jdbcjobstore.JobStoreTX and
+ // driver delegate like org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
+ if (grailsJobClass.isConcurrent()) {
+ jobDetail.setJobClass(GrailsArtefactJob.class);
+ }
+ else {
+ jobDetail.setJobClass(GrailsArtefactNonConcurrentJob.class);
+ }
+
jobDetail.getJobDataMap().put(JOB_NAME_PARAMETER, grailsJobClass.getFullName());
//FIXME put the check for sessionRequired
@@ -0,0 +1,11 @@
+package grails.plugin.quartz2;
+
+import org.quartz.DisallowConcurrentExecution;
+
+@DisallowConcurrentExecution
+class GrailsArtefactNonConcurrentJob extends GrailsArtefactJob {
+
+ public GrailsArtefactNonConcurrentJob(Object job) {
+ super(job);
+ }
+}
@@ -41,10 +41,14 @@ public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws Schedul
//JobDetail jobDetail = bundle.getJobDetail();
//Class<? extends Job> jobClass = jobDetail.getJobClass();
- String grailsJobName = (String) bundle.getJobDetail().getJobDataMap().get(GrailsArtefactJobDetailFactoryBean.JOB_NAME_PARAMETER);
+ JobDetail jobDetail = bundle.getJobDetail();
+ String grailsJobName = (String) jobDetail.getJobDataMap().get(GrailsArtefactJobDetailFactoryBean.JOB_NAME_PARAMETER);
if (grailsJobName != null) {
Object job = applicationContext.getBean(grailsJobName);
- return new GrailsArtefactJob(job);
+ if (!jobDetail.isConcurrentExectionDisallowed()) {
+ return new GrailsArtefactNonConcurrentJob(job);
+ }
+ return new GrailsArtefactJob(job);
} else {
return super.newJob(bundle,scheduler);
}
@@ -0,0 +1,59 @@
+
+
+package grails.plugin.quartz2
+
+import grails.test.GrailsMock
+import grails.test.mixin.*
+import grails.test.mixin.support.*
+
+import org.junit.*
+import org.quartz.JobKey
+
+/**
+ * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
+ */
+@TestMixin(GrailsUnitTestMixin)
+class GrailsArtefactJobDetailFactoryBeanTests {
+
+ void setUp() {
+ // Setup logic here
+ }
+
+ void tearDown() {
+ // Tear down logic here
+ }
+
+ void testConcurrentJobCreation() {
+ def jobClassMock = new GrailsMock(GrailsJobClass, true)
+ jobClassMock.demand.getJobKey(1) { return new JobKey("111") }
+ jobClassMock.demand.isConcurrent(2) { return true }
+ jobClassMock.demand.getFullName(1) { return "myfullname" }
+ jobClassMock.demand.isSessionRequired(1) { return true }
+ jobClassMock.demand.isGormSession(1) { return true }
+ jobClassMock.demand.getDurability(1) { return true }
+ jobClassMock.demand.getRequestsRecovery(1) { return true }
+
+ def factory = new GrailsArtefactJobDetailFactoryBean()
+ factory.grailsJobClass = jobClassMock.createMock()
+ factory.afterPropertiesSet()
+
+ assert factory.getObject().getJobClass() == GrailsArtefactJob.class
+ }
+
+ void testNonConcurrentJobCreation() {
+ def jobClassMock = new GrailsMock(GrailsJobClass, true)
+ jobClassMock.demand.getJobKey(1) { return new JobKey("111") }
+ jobClassMock.demand.isConcurrent(2) { return false }
+ jobClassMock.demand.getFullName(1) { return "myfullname" }
+ jobClassMock.demand.isSessionRequired(1) { return true }
+ jobClassMock.demand.isGormSession(1) { return true }
+ jobClassMock.demand.getDurability(1) { return true }
+ jobClassMock.demand.getRequestsRecovery(1) { return true }
+
+ def factory = new GrailsArtefactJobDetailFactoryBean()
+ factory.grailsJobClass = jobClassMock.createMock()
+ factory.afterPropertiesSet()
+
+ assert factory.getObject().getJobClass() == GrailsArtefactNonConcurrentJob.class
+ }
+}

0 comments on commit 1f95f45

Please sign in to comment.