InstantiationTracingBeanPostProcessor.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.yihu.wlyy.job;
  2. import com.yihu.wlyy.entity.job.QuartzJobConfig;
  3. import com.yihu.wlyy.entity.statistics.WlyyQuota;
  4. import com.yihu.wlyy.repository.job.JobConfigDao;
  5. import com.yihu.wlyy.repository.statistics.QuotaDao;
  6. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  7. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  8. import org.quartz.Scheduler;
  9. import org.springframework.beans.BeanUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.ApplicationListener;
  12. import org.springframework.context.event.ContextRefreshedEvent;
  13. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * Created by Administrator on 2016/8/16.
  19. * 现在用的数据库保存job不需要用这个类
  20. */
  21. //@Component
  22. public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {
  23. @Autowired
  24. private SchedulerFactoryBean schedulerFactoryBean;
  25. @Autowired
  26. private QuotaDao quotaDao;
  27. @Autowired
  28. private JobConfigDao jobConfigDao;
  29. private Scheduler scheduler = null;
  30. @Autowired
  31. private QuartzHelper quartzHelper;
  32. @Override
  33. public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
  34. try {
  35. scheduler = schedulerFactoryBean.getScheduler();
  36. //查询所有需要开始运行的任务的指标
  37. List<QuartzJobConfig> jobConfigList=jobConfigDao.findByStatus("1");
  38. for(QuartzJobConfig quartzJobConfig :jobConfigList){
  39. WlyyQuota wlyyQuota=quotaDao.findOne(quartzJobConfig.getQuotaId());
  40. WlyyQuotaVO wlyyQuotaVO= new WlyyQuotaVO();
  41. WlyyJobConfigVO wlyyJobConfigVO=new WlyyJobConfigVO();
  42. BeanUtils.copyProperties(wlyyQuota,wlyyQuotaVO);
  43. BeanUtils.copyProperties(quartzJobConfig,wlyyJobConfigVO);
  44. Map<String,Object> params=new HashMap<String,Object>();
  45. params.put("quota",wlyyQuotaVO);
  46. params.put("jobConfig",wlyyJobConfigVO);
  47. quartzHelper.addJob(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId(),params);
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }