ScheduleConfig.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright 2018
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.warewms.job.config;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  21. import javax.sql.DataSource;
  22. import java.util.Properties;
  23. /**
  24. * 定时任务配置
  25. *
  26. * @author Mark sunlightcs@gmail.com
  27. * @since 2.0.0 2017-04-20
  28. */
  29. @Configuration
  30. public class ScheduleConfig {
  31. @Value("${schedule.open}")
  32. Boolean scheduleOpen;
  33. @Bean
  34. public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
  35. SchedulerFactoryBean factory = new SchedulerFactoryBean();
  36. factory.setDataSource(dataSource);
  37. //quartz参数
  38. Properties prop = new Properties();
  39. prop.put("org.quartz.scheduler.instanceName", "SdbScheduler");
  40. prop.put("org.quartz.scheduler.instanceId", "AUTO");
  41. //线程池配置
  42. prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
  43. prop.put("org.quartz.threadPool.threadCount", "20");
  44. prop.put("org.quartz.threadPool.threadPriority", "5");
  45. //JobStore配置
  46. prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
  47. //集群配置
  48. prop.put("org.quartz.jobStore.isClustered", "true");
  49. prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
  50. prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
  51. prop.put("org.quartz.jobStore.misfireThreshold", "12000");
  52. prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
  53. prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?");
  54. //PostgreSQL数据库,需要打开此注释
  55. //prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
  56. factory.setQuartzProperties(prop);
  57. factory.setSchedulerName("SdbScheduler");
  58. //延时启动
  59. factory.setStartupDelay(30);
  60. factory.setApplicationContextSchedulerContextKey("applicationContextKey");
  61. //可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
  62. factory.setOverwriteExistingJobs(true);
  63. //设置自动启动,默认为true
  64. factory.setAutoStartup(scheduleOpen);
  65. return factory;
  66. }
  67. }