定时任务

对于定时任务,可以利用线程来实现,而本处是在以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>
 
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{
//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("定时任务执行中…");  
 }
 
}
 
 
 

转载于:https://www.cnblogs.com/krystal0901/p/5558352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值