一.项目采用的是quartz框架做定时任务,所以做的是quart分布式解决负载均衡下定时任务问题
1.需要将定时任务持久化到数据库,在数据库对应sql语句里面找到对应数据库,生成对应的数据库表
2.在项目中配置定时任务
- 创建quartz.properties文件,文件内容如下
org.quartz.scheduler.instanceName = TestScheduler1
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.dataSource = quartz
org.quartz.dataSource.quartz.provider=hikaricp
org.quartz.dataSource.quartz.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.quartz.URL=jdbc:mysql:///purchase_order?serverTimezone=UTC 数据库地址
org.quartz.dataSource.quartz.user=root 用户名
org.quartz.dataSource.quartz.password=xx 密码
org.quartz.dataSource.quartz.maxConnections=5
2.创建任务
@PersistJobDataAfterExecution
@DisallowConcurrentExecution// 不允许并发执行
public class QuartzDemo extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("-----------111111111----------");
}
}
配置任务
<bean id="updateArrivedJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>com.creatooor.order.quartz.QuartzDemo</value>
</property>
<property name="requestsRecovery" value="true" />
<property name="durability" value="true" />
</bean>
<bean id="updateArrivedJobDetailTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--将任务类描述交给触发器-->
<property name="jobDetail" ref="updateArrivedJobDetail"></property>
<!--Cron表达式,定时规则-->
<property name="cronExpression" value="0 20 13 * * ? "></property>
</bean>
<!-- 总管理容器 -->
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties" />
<!-- 重写任务配置信息,这样当修改任务执行时间后,不用清数据库表就会生效,程序会自动修改对应任务的执行时间-->
<property name="overwriteExistingJobs" value="true" />
<property name="triggers">
<list>
<!-- <ref bean="updatePriceTrigger"/>-->
<!-- <ref bean="updateArrivedJobDetailTrigger"/>-->
<ref bean="updateArrivedJobDetailTrigger"/>
</list>
</property>
</bean>
结果:
只有一个tomcat开启执行任务
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。