ScheduleConfig.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.ruoyi.quartz.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  5. import javax.sql.DataSource;
  6. import java.util.Properties;
  7. /**
  8. * 定时任务配置(单机部署建议删除此类和qrtz数据库表,默认走内存会最高效)
  9. *
  10. * @author ruoyi
  11. */
  12. @Configuration
  13. public class ScheduleConfig
  14. {
  15. @Bean
  16. public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource)
  17. {
  18. SchedulerFactoryBean factory = new SchedulerFactoryBean();
  19. factory.setDataSource(dataSource);
  20. // quartz参数
  21. Properties prop = new Properties();
  22. prop.put("org.quartz.scheduler.instanceName", "RuoyiScheduler");
  23. prop.put("org.quartz.scheduler.instanceId", "AUTO");
  24. // 线程池配置
  25. prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
  26. prop.put("org.quartz.threadPool.threadCount", "20");
  27. prop.put("org.quartz.threadPool.threadPriority", "5");
  28. // JobStore配置
  29. prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
  30. // 集群配置
  31. prop.put("org.quartz.jobStore.isClustered", "true");
  32. prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
  33. prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
  34. prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");
  35. // sqlserver 启用
  36. // prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?");
  37. prop.put("org.quartz.jobStore.misfireThreshold", "12000");
  38. prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
  39. factory.setQuartzProperties(prop);
  40. factory.setSchedulerName("RuoyiScheduler");
  41. // 延时启动
  42. factory.setStartupDelay(1);
  43. factory.setApplicationContextSchedulerContextKey("applicationContextKey");
  44. // 可选,QuartzScheduler
  45. // 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
  46. factory.setOverwriteExistingJobs(true);
  47. // 设置自动启动,默认为true
  48. factory.setAutoStartup(true);
  49. return factory;
  50. }
  51. }