forked from sakaiproject/sakai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAK-31584 Allow auto wiring of Quartz Jobs. (sakaiproject#3105)
This means you can just register a class with the job scheduler and then quartz will create an instance when it needs to and have spring autowire any dependencies.
- Loading branch information
Showing
17 changed files
with
334 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
jobscheduler/scheduler-component-shared/README.beanjobs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Autowiring Quartz Jobs | ||
====================== | ||
|
||
To implement a quartz job now all you need todo is create a class that | ||
implements the org.quartz.Job interface and register this class with the | ||
scheuduler manager. The class doesn't need to be a spring bean as it | ||
can be autowired with any services it needs. The data from the job map | ||
is also available to the autowiring. | ||
|
||
See: org.springframework.scheduling.quartz.SpringBeanJobFactory | ||
See: org.sakaiproject.component.app.scheduler.jobs.AutowiredTestJob | ||
|
||
@Inject doesn't work unless the annotation is available when the main | ||
spring application context is setup. However @Autowired is always available | ||
so can be used. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ent-shared/src/java/org/sakaiproject/component/app/scheduler/AutowiredJobBeanWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.sakaiproject.component.app.scheduler; | ||
|
||
import org.quartz.Job; | ||
import org.sakaiproject.api.app.scheduler.JobBeanWrapper; | ||
|
||
/** | ||
* This is a JobBeanWrapper that will just be registered with the Job Scheduler. | ||
* This class isn't actually used when running the job as quartz will directly create an instance of the class. | ||
*/ | ||
public class AutowiredJobBeanWrapper implements JobBeanWrapper { | ||
|
||
private String jobType; | ||
private Class<? extends Job> aClass; | ||
|
||
public AutowiredJobBeanWrapper(Class<? extends Job> aClass, String jobType) { | ||
this.aClass = aClass; | ||
this.jobType = jobType; | ||
} | ||
|
||
@Override | ||
public String getBeanId() { | ||
// We don't need a bean ID as a new instance will be created and will get autowired. | ||
return null; | ||
} | ||
|
||
@Override | ||
public Class<? extends Job> getJobClass() { | ||
return aClass; | ||
} | ||
|
||
@Override | ||
public String getJobType() { | ||
return jobType; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...red/src/java/org/sakaiproject/component/app/scheduler/AutowiringSpringBeanJobFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.sakaiproject.component.app.scheduler; | ||
|
||
import org.quartz.spi.TriggerFiredBundle; | ||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.scheduling.quartz.SpringBeanJobFactory; | ||
|
||
/** | ||
* This JobFactory autowires automatically the created quartz bean with spring @Autowired dependencies. | ||
* | ||
* @author jelies (thanks to Brian Matthews) | ||
* @link http://webcache.googleusercontent.com/search?q=cache:FH-N1i--sDgJ:blog.btmatthews.com/2011/09/24/inject-application-context-dependencies-in-quartz-job-beans/+&cd=7&hl=en&ct=clnk&gl=es) | ||
*/ | ||
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements | ||
ApplicationContextAware { | ||
|
||
private transient AutowireCapableBeanFactory beanFactory; | ||
|
||
@Override | ||
public void setApplicationContext(final ApplicationContext context) { | ||
beanFactory = context.getAutowireCapableBeanFactory(); | ||
} | ||
|
||
@Override | ||
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { | ||
final Object job = super.createJobInstance(bundle); | ||
beanFactory.autowireBean(job); | ||
return job; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ent-shared/src/java/org/sakaiproject/component/app/scheduler/JobBeanWrapperRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.sakaiproject.component.app.scheduler; | ||
|
||
import org.sakaiproject.api.app.scheduler.JobBeanWrapper; | ||
import org.sakaiproject.api.app.scheduler.SchedulerManager; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Just registers JobBeanWrappers with the Schedule Manager, this is needed because the autowired | ||
* jobs don't have to register themselves. | ||
*/ | ||
public class JobBeanWrapperRegistrar { | ||
|
||
private SchedulerManager schedulerManager; | ||
private List<JobBeanWrapper> jobBeans; | ||
|
||
public void setSchedulerManager(SchedulerManager schedulerManager) { | ||
this.schedulerManager = schedulerManager; | ||
} | ||
|
||
public void setJobBeans(List<JobBeanWrapper> jobBeans) { | ||
this.jobBeans = jobBeans; | ||
} | ||
|
||
public void init() { | ||
jobBeans.stream().forEach(wrapper -> schedulerManager.registerBeanJob(wrapper.getJobType(), wrapper)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.