chenken 7 سال پیش
والد
کامیت
5f94fbb110

+ 5 - 3
examcloud-task-dao/src/main/java/cn/com/qmth/task/entity/ScheduleJobEntity.java

@@ -3,6 +3,7 @@ package cn.com.qmth.task.entity;
 import java.io.Serializable;
 import java.util.Date;
 
+import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.persistence.Table;
@@ -17,6 +18,7 @@ import javax.validation.constraints.NotNull;
  * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  */
 @Table(name="ec_b_schedule_job",uniqueConstraints = {@UniqueConstraint(columnNames={"jobName"})})
+@Entity
 public class ScheduleJobEntity implements Serializable
 {
 	private static final long serialVersionUID = -1638993747996917970L;
@@ -62,7 +64,7 @@ public class ScheduleJobEntity implements Serializable
 	/**
 	 * 是否顺序执行
 	 */
-	private boolean stateful;
+	private Boolean stateful;
 	
 	/**
 	 * job操作时间
@@ -134,12 +136,12 @@ public class ScheduleJobEntity implements Serializable
 		this.springBean = springBean;
 	}
 
-	public boolean isStateful()
+	public Boolean isStateful()
 	{
 		return stateful;
 	}
 
-	public void setStateful(boolean stateful)
+	public void setStateful(Boolean stateful)
 	{
 		this.stateful = stateful;
 	}

+ 35 - 0
examcloud-task-service/src/main/java/cn/com/qmth/task/service/job/JobsInitialization.java

@@ -0,0 +1,35 @@
+package cn.com.qmth.task.service.job;
+
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import cn.com.qmth.task.base.QuartzManager;
+import cn.com.qmth.task.base.ScheduleJob;
+import cn.com.qmth.task.base.enums.JobStatus;
+import cn.com.qmth.task.entity.ScheduleJobEntity;
+import cn.com.qmth.task.service.ScheduleJobEntityService;
+
+@Component
+public class JobsInitialization implements Runnable{
+
+	@Autowired
+	private ScheduleJobEntityService scheduleJobEntityService;
+	
+	@Autowired
+	private QuartzManager quartzManager;
+
+	@Override
+	public void run() {
+		List<ScheduleJobEntity> scheduleJobEntitys = scheduleJobEntityService.findAll();
+    	for(ScheduleJobEntity scheduleJobEntity:scheduleJobEntitys){
+    		ScheduleJob scheduleJob = scheduleJobEntityService.transformationToScheduleJob(scheduleJobEntity);
+    		quartzManager.addJob(scheduleJob);
+    		scheduleJobEntity.setJobStatus(JobStatus.RUNNING.name());
+			scheduleJobEntity.setOperateResult(JobStatus.RUNNING.name()+" SUCCESS");
+			scheduleJobEntityService.saveScheduleJobEntity(scheduleJobEntity);
+    	}
+	} 
+	
+}

+ 16 - 0
examcloud-task-starter/src/main/java/cn/com/qmth/task/starter/JobsInitializationListener.java

@@ -0,0 +1,16 @@
+package cn.com.qmth.task.starter;
+
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
+
+import cn.com.qmth.task.service.job.JobsInitialization;
+
+public class JobsInitializationListener implements ApplicationListener<ContextRefreshedEvent> {
+
+	@Override
+	public void onApplicationEvent(ContextRefreshedEvent event) {
+    	Thread thread = new Thread(event.getApplicationContext().getBean(JobsInitialization.class));
+    	thread.start();
+	}
+
+}

+ 3 - 1
examcloud-task-starter/src/main/java/cn/com/qmth/task/starter/TaskApplication.java

@@ -17,7 +17,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 public class TaskApplication {
 
     public static void main(String[] args) throws Exception {
-        SpringApplication.run(TaskApplication.class);
+    	SpringApplication application = new SpringApplication(TaskApplication.class);
+    	application.addListeners(new JobsInitializationListener());
+    	application.run(args);
     }
     
 }

+ 0 - 3
examcloud-task-starter/src/main/resources/quartz.properties

@@ -1,3 +0,0 @@
-org.quartz.scheduler.instanceName = MyScheduler
-org.quartz.threadPool.threadCount = 3
-org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

+ 0 - 3
examcloud-task-starter/target/classes/quartz.properties

@@ -1,3 +0,0 @@
-org.quartz.scheduler.instanceName = MyScheduler
-org.quartz.threadPool.threadCount = 3
-org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore