对于定时任务,可以利用线程来实现,而本处是在以Spring为基础。
Spring+Quartz 集群 下的一些问题异常解析
1.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.SecretStories.quartz.Job1" />
<property name="durability" value="true"/>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job1" />
<property name="cronExpression" value="0 36-40 9 * * ?"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
<!-- tomcat启动多少时间后 Starting Quartz Scheduler-->
<property name="startupDelay" value="10"></property>
<!-- 使用 org.quartz.impl.jdbcjobstore.JobStoreTX 在mysql里存储作业调度等触发器的一些信息 -->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<!-- 获取加载 quartz.properties 的配置 -->
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.SecretStories.quartz.Job1" />
<property name="durability" value="true"/>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job1" />
<property name="cronExpression" value="0 36-40 9 * * ?"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
<!-- tomcat启动多少时间后 Starting Quartz Scheduler-->
<property name="startupDelay" value="10"></property>
<!-- 使用 org.quartz.impl.jdbcjobstore.JobStoreTX 在mysql里存储作业调度等触发器的一些信息 -->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<!-- 获取加载 quartz.properties 的配置 -->
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
</beans>
2.
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
#调度器名称,无关紧要,随意命名
org.quartz.scheduler.instanceName = ClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool 配置数据库连接池
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 9
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================================
# Configure JobStore 配置做业存储方式
#============================================================================
# Notice: RAMJobStore not support cluster!
#在这里自己控制事务
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#相当于扫描频率,如果系统基于秒级,应培植成1000,quartz默认为分级(60000)
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.useProperties = false
#表前缀
org.quartz.jobStore.tablePrefix = QRTZ_
#"true"来激活集群特性
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
3.
1.定义的作业类需要继承QuartzClusterableJob(是自己编写的)
QuartzClusterableJob是继承QuartzJobBean,为什么不让作业类直接继承,这考虑到作业类可能会对Dao层进行定时设置,这是要获取这些层的操作,则需要手动获取applicationContext配置的Bean。
public abstract class QuartzClusterableJob extends QuartzJobBean{
public abstract class QuartzClusterableJob extends QuartzJobBean{
//protected 用来保护子类 范围 (内部类,子类,本包)
protected ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
// TODO Auto-generated method stub
executeJob(context);
}
protected abstract void executeJob(JobExecutionContext context);
}
2.作业类Job
public class Job extends QuartzClusterableJob{
//如果要使用某个dao层 假设UserDao
private UserDao userDao;
public Job(){
userDao= SpringContextHolder.getBean(UserDao .class);
}
private int timeout;
private static int i = 0;
//调度工厂实例化后,经过timeout时间开始执行调度
public void setTimeout(int timeout) {
this.timeout = timeout;
}
@Override
protected void executeJob(JobExecutionContext context) {
// TODO Auto-generated method stub
System.out.println("定时任务执行中…");
}
}