Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joeylin/22bfdf166abdc3f882eb7c21dd634e23 to your computer and use it in GitHub Desktop.
Save joeylin/22bfdf166abdc3f882eb7c21dd634e23 to your computer and use it in GitHub Desktop.
Configuring Quartz 2.1.7 with Spring 3.1.3 in clustered mode
/**
* Autowire Quartz Jobs with Spring context dependencies
* @see http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring/15211030#15211030
*/
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
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;
}
}
@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class FirstJob extends QuartzJobBean {
@Autowired
private FirstService firstService;
@Override
protected void executeInternal(JobExecutionContext context) {
//Quartz jobs have not been authenticated with acegi or spring security
//so you may have to setup a user before calling your service methods
//I used SecurityContextHolder.getContext().setAuthentication(quartzUser)
//on an older version of acegi
firstService.updateSomethingInTheDatabase();
}
}
/**
* Needed to set Quartz useProperties=true when using Spring classes,
* because Spring sets an object reference on JobDataMap that is not a String
*
* @see http://site.trimplement.com/using-spring-and-quartz-with-jobstore-properties/
* @see http://forum.springsource.org/showthread.php?130984-Quartz-error-IOException
*/
public class PersistableCronTriggerFactoryBean extends CronTriggerFactoryBean {
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
//Remove the JobDetail element
getJobDataMap().remove(JobDetailAwareTrigger.JOB_DETAIL_KEY);
}
}
# Using Spring datasource in quartzJobsConfig.xml
# Spring uses LocalDataSourceJobStore extension of JobStoreCMT
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
# Change this to match your DB vendor
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
# Needed to manage cluster instances
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=MY_JOB_SCHEDULER
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
/**
* Verifies that the Cron Trigger Time Strings for the jobs are setup correctly
*/
@ContextConfiguration(locations = {"classpath:/applicationContext-test.xml"})
public class QuartzCronTriggerTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private SchedulerFactoryBean quartzScheduler;
@Test
public void testFirstTrigger() throws SchedulerException {
Trigger firstTrigger = quartzScheduler.getScheduler().getTrigger(new TriggerKey("firstTrigger"));
//Must use tomorrow for testing because jobs have startTime of now
DateTime tomorrow = new DateMidnight().toDateTime().plusDays(1);
//Test first
Date next = firstTrigger.getFireTimeAfter(tomorrow.toDate());
DateTime expected = tomorrow.plusHours(5);
assertThat(next, is(expected.toDate()));
//Test the next day
next = firstTrigger.getFireTimeAfter(next);
expected = expected.plusDays(1);
assertThat(next, is(expected.toDate()));
}
}
<!-- truncated pieces of applicationContext.xml -->
<bean id="firstJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.sheetsj.quartz.job.FirstJob"/>
<property name="durability" value="true"/>
</bean>
<bean id="firstTrigger" class="com.sheetsj.quartz.PersistableCronTriggerFactoryBean">
<property name="jobDetail" ref="firstJobDetail" />
<!-- run every morning at 5:00 AM -->
<property name="cronExpression" value="0 0 5 * * ?" />
</bean>
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="dataSource" value="dataSource"/>
<property name="transactionManager" value="transactionManager"/>
<!-- This name is persisted as SCHED_NAME in db. for local testing could change to unique name
to avoid collision with dev server -->
<property name="schedulerName" value="quartzScheduler"/>
<!-- Will update database cron triggers to what is in this jobs file on each deploy.
Replaces all previous trigger and job data that was in the database. YMMV -->
<property name="overwriteExistingJobs" value="true"/>
<property name="autoStartup" value="true"/>
<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
<property name="jobFactory">
<bean class="com.sheetsj.quartz.AutowiringSpringBeanJobFactory"/>
</property>
<!-- NOTE: Must add both the jobDetail and trigger to the scheduler! -->
<property name="jobDetails">
<list>
<ref bean="firstJobDetail" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="firstTrigger"/>
</list>
</property>
</bean>
@joeylin
Copy link
Author

joeylin commented Jun 6, 2017

使用bean配置Quartz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment