|
@@ -0,0 +1,115 @@
|
|
|
+package com.mooctest.crowd.site.service.impl;
|
|
|
+
|
|
|
+import com.mooctest.crowd.domain.domainobject.CrowdTestProject;
|
|
|
+import com.mooctest.crowd.site.service.ThemeSchedulerService;
|
|
|
+import com.mooctest.crowd.site.service.ThemeStatusService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
+import org.springframework.scheduling.support.CronTrigger;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @program: crowd
|
|
|
+ * @author: hanyuwei
|
|
|
+ * @create: 2020-07-13 11:30
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+public class ThemeSchedulerServiceImpl implements ThemeSchedulerService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ThemeStatusService themeStatusService;
|
|
|
+
|
|
|
+ private ConcurrentHashMap<CrowdTestProject, ThemeScheduler> map = new ConcurrentHashMap<>();
|
|
|
+ @Override
|
|
|
+ public boolean createNewThemeScheduler(CrowdTestProject crowdTestProject) {
|
|
|
+ if(! this.map.containsKey(crowdTestProject)) {
|
|
|
+ String cronExp = this.generateNextCronExp(crowdTestProject);
|
|
|
+ if(cronExp==null){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ ThemeScheduler themeDetailScheduler = new ThemeScheduler(crowdTestProject, cronExp);
|
|
|
+ themeDetailScheduler.startCron();
|
|
|
+ map.put(crowdTestProject, themeDetailScheduler);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String generateNextCronExp(CrowdTestProject themeDetail) {
|
|
|
+ String cronExp = null;
|
|
|
+ String dateFormat="ss mm HH dd MM EE";
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
|
|
|
+ Date now = new Date();
|
|
|
+ if(now.before(themeDetail.getCreateTime())) {
|
|
|
+ cronExp = sdf.format(themeDetail.getCreateTime());
|
|
|
+ } else if(now.before(themeDetail.getDeadTime())){
|
|
|
+ cronExp = sdf.format(themeDetail.getDeadTime());
|
|
|
+ }
|
|
|
+ return cronExp;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public boolean cancelThemeScheduler(CrowdTestProject themeDetail) {
|
|
|
+ if(this.map.containsKey(themeDetail)) {
|
|
|
+ ThemeScheduler themeDetailScheduler = map.get(themeDetail);
|
|
|
+ themeDetailScheduler.stopCron();
|
|
|
+ map.remove(themeDetail);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private class ThemeScheduler {
|
|
|
+ private ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
|
|
+ private ScheduledFuture<?> future;
|
|
|
+ private CrowdTestProject crowdTestProject = null;
|
|
|
+ private String cronExp = null;
|
|
|
+
|
|
|
+ ThemeScheduler() {}
|
|
|
+
|
|
|
+ ThemeScheduler (CrowdTestProject crowdTestProject, String cronExp) {
|
|
|
+ this.crowdTestProject = crowdTestProject;
|
|
|
+ this.cronExp = cronExp;
|
|
|
+ this.threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
|
|
+ this.threadPoolTaskScheduler.initialize();
|
|
|
+ }
|
|
|
+ public String startCron() {
|
|
|
+ future = this.threadPoolTaskScheduler.schedule(new MyRunnable(this.crowdTestProject), new CronTrigger(this.cronExp));
|
|
|
+ return "startCron";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String stopCron() {
|
|
|
+ if (future != null) {
|
|
|
+ future.cancel(true);
|
|
|
+ }
|
|
|
+ return "stopCron";
|
|
|
+ }
|
|
|
+
|
|
|
+ private class MyRunnable implements Runnable {
|
|
|
+
|
|
|
+ private CrowdTestProject crowdTestProject;
|
|
|
+
|
|
|
+ public MyRunnable() {}
|
|
|
+
|
|
|
+ public MyRunnable (CrowdTestProject themeDetail) {
|
|
|
+ this.crowdTestProject = crowdTestProject;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Integer result = themeStatusService.updateStatus(this.crowdTestProject);
|
|
|
+ map.remove(crowdTestProject);
|
|
|
+ if(result.equals(CrowdTestProject.HAS_CREATED) || result.equals(CrowdTestProject.HAS_RELEASED)) {
|
|
|
+ createNewThemeScheduler(crowdTestProject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|