JobService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.yihu.jw.care.service;
  2. import com.yihu.jw.care.dao.QuartzJobConfigDao;
  3. import com.yihu.jw.care.dao.QuotaDao;
  4. import com.yihu.jw.care.job.QuartzHelper;
  5. import com.yihu.jw.care.vo.WlyyJobConfigVO;
  6. import com.yihu.jw.care.vo.WlyyQuotaVO;
  7. import com.yihu.jw.entity.job.QuartzJobConfig;
  8. import com.yihu.jw.entity.quota.WlyyQuota;
  9. import org.springframework.beans.BeanUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.util.StringUtils;
  14. import java.text.SimpleDateFormat;
  15. import java.util.*;
  16. /**
  17. * @author chenweida
  18. */
  19. @Service
  20. public class JobService {
  21. @Autowired
  22. private QuartzHelper quartzHelper;
  23. @Autowired
  24. private QuartzJobConfigDao wlyyJobConfigDao;
  25. @Autowired
  26. private QuotaDao quotaDao;
  27. @Transactional
  28. public void stopById(String id) throws Exception {
  29. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "1");
  30. if (quartzJobConfig != null) {
  31. quartzHelper.removeJob(quartzJobConfig.getId());
  32. quartzJobConfig.setStatus("0");
  33. } else {
  34. throw new Exception("任务已经停止");
  35. }
  36. }
  37. @Transactional
  38. public void startById(String id) throws Exception {
  39. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
  40. if (quartzJobConfig != null) {
  41. startOneJob(quartzJobConfig);
  42. } else {
  43. throw new Exception("任务已经启动");
  44. }
  45. }
  46. @Transactional
  47. public void stopAll() throws Exception {
  48. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
  49. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  50. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  51. quartzHelper.removeJob(quartzJobConfig.getId());
  52. quartzJobConfig.setStatus("0");
  53. }
  54. } else {
  55. throw new Exception("任务已经全部停止");
  56. }
  57. }
  58. @Transactional
  59. public void startAll() throws Exception {
  60. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
  61. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  62. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  63. startOneJob(quartzJobConfig);
  64. }
  65. } else {
  66. throw new Exception("任务已经全部启动");
  67. }
  68. }
  69. /**
  70. * 启动单个任务
  71. *
  72. * @param quartzJobConfig
  73. * @throws Exception
  74. */
  75. private void startOneJob(QuartzJobConfig quartzJobConfig) throws Exception {
  76. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  77. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  78. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  79. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  80. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  81. Map<String, Object> params = new HashMap<String, Object>();
  82. params.put("quota", wlyyQuotaVO);
  83. params.put("jobConfig", wlyyJobConfigVO);
  84. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  85. //往quartz框架添加任务
  86. quartzHelper.addJob(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId(), params);
  87. quartzJobConfig.setStatus("1");//设置任务状态是启动
  88. }
  89. }
  90. public void startNowById(String id) throws Exception {
  91. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findOne(id);
  92. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  93. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  94. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  95. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  96. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  97. Map<String, Object> params = new HashMap<String, Object>();
  98. params.put("quota", wlyyQuotaVO);
  99. params.put("jobConfig", wlyyJobConfigVO);
  100. //往quartz框架添加任务
  101. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  102. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId()+ UUID.randomUUID(), params);
  103. }
  104. }
  105. public void productDataByDay(Integer day) throws Exception {
  106. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
  107. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  108. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  109. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  110. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  111. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  112. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  113. Map<String, Object> params = new HashMap<String, Object>();
  114. params.put("quota", wlyyQuotaVO);
  115. params.put("jobConfig", wlyyJobConfigVO);
  116. for (int i = 1; i <= day; i++) {
  117. //往quartz框架添加任务
  118. params.put("now", getYesterday(0 - i + 1));
  119. params.put("yesterday", getYesterday(0 - i));
  120. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  121. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  122. Thread.sleep(15000L);
  123. }
  124. }
  125. }
  126. }
  127. public static String getYesterday(Integer day) {
  128. Calendar cal = Calendar.getInstance();
  129. cal.add(Calendar.DATE, day);
  130. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  131. return yesterday;
  132. }
  133. public void productDataByOneDay(String yesterday) throws Exception {
  134. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  135. Date date = dataSimple.parse(yesterday);
  136. if (date == null) {
  137. throw new Exception("时间格式错误");
  138. }
  139. Calendar calendar = new GregorianCalendar();
  140. calendar.setTime(date);
  141. calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
  142. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  143. String now = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  144. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
  145. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  146. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  147. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  148. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  149. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  150. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  151. Map<String, Object> params = new HashMap<String, Object>();
  152. params.put("quota", wlyyQuotaVO);
  153. params.put("jobConfig", wlyyJobConfigVO);
  154. //往quartz框架添加任务
  155. params.put("now", now);
  156. params.put("yesterday", yesterday);
  157. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  158. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  159. Thread.sleep(15000L);
  160. }
  161. }
  162. }
  163. public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
  164. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  165. Date date = dataSimple.parse(yesterday);
  166. if (date == null) {
  167. throw new Exception("时间格式错误");
  168. }
  169. Calendar calendar = new GregorianCalendar();
  170. calendar.setTime(date);
  171. calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
  172. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  173. String now = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  174. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  175. if (quartzJobConfig == null) {
  176. throw new Exception("id不存在");
  177. }
  178. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  179. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  180. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  181. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  182. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  183. Map<String, Object> params = new HashMap<String, Object>();
  184. params.put("quota", wlyyQuotaVO);
  185. params.put("jobConfig", wlyyJobConfigVO);
  186. //往quartz框架添加任务
  187. params.put("now", now);
  188. params.put("yesterday", yesterday);
  189. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  190. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  191. Thread.sleep(15000L);
  192. }
  193. }
  194. public void productDataByDayAndId(Integer day, String id) throws Exception{
  195. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  196. if(quartzJobConfig==null){
  197. throw new Exception("id不存在");
  198. }
  199. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  200. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  201. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  202. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  203. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  204. Map<String, Object> params = new HashMap<String, Object>();
  205. params.put("quota", wlyyQuotaVO);
  206. params.put("jobConfig", wlyyJobConfigVO);
  207. for (int i = 1; i <= day; i++) {
  208. //往quartz框架添加任务
  209. params.put("now", getYesterday(0 - i + 1));
  210. params.put("yesterday", getYesterday(0 - i));
  211. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  212. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  213. Thread.sleep(15000L);
  214. }
  215. }
  216. }
  217. }