SchedulerConfig.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.yihu.jw.care.config.quartz;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.beans.factory.config.PropertiesFactoryBean;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.core.io.ClassPathResource;
  9. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  10. import javax.sql.DataSource;
  11. import java.io.IOException;
  12. import java.util.Properties;
  13. /**
  14. * Created by chenweida on 2016/2/3.
  15. */
  16. @Configuration
  17. public class SchedulerConfig {
  18. @Autowired
  19. private ApplicationContext applicationContext;
  20. @Autowired
  21. private org.quartz.spi.JobFactory jobFactory;
  22. @Autowired
  23. private DataSource dataSource;
  24. @Value("${quartz.name}")
  25. private String schedulerFactoryBeanName;
  26. @Bean
  27. SchedulerFactoryBean schedulerFactoryBeanWlyy() throws IOException {
  28. SchedulerFactoryBean bean = new SchedulerFactoryBean();
  29. bean.setJobFactory(jobFactory);
  30. bean.setApplicationContext(this.applicationContext);
  31. bean.setOverwriteExistingJobs(true);
  32. bean.setStartupDelay(20);// 延时启动
  33. bean.setBeanName(schedulerFactoryBeanName);
  34. bean.setAutoStartup(true);
  35. bean.setDataSource(dataSource);
  36. bean.setQuartzProperties(quartzProperties());
  37. return bean;
  38. }
  39. /**
  40. * quartz配置文件
  41. * @return
  42. * @throws IOException
  43. */
  44. @Bean
  45. public Properties quartzProperties() throws IOException {
  46. PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  47. propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
  48. propertiesFactoryBean.afterPropertiesSet();
  49. return propertiesFactoryBean.getObject();
  50. }
  51. }