WANG %!s(int64=6) %!d(string=hai) anos
pai
achega
db525d527c

+ 101 - 81
examcloud-task-starter/src/main/java/cn/com/qmth/examcloud/task/starter/config/JobsStartup.java

@@ -1,81 +1,101 @@
-package cn.com.qmth.examcloud.task.starter.config;
-
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
-
-import cn.com.qmth.examcloud.task.base.QuartzManager;
-import cn.com.qmth.examcloud.task.base.ScheduleJob;
-import cn.com.qmth.examcloud.task.dao.ScheduleJobRepo;
-import cn.com.qmth.examcloud.task.dao.entity.ScheduleJobEntity;
-
-/**
- * 类注释
- *
- * @author WANGWEI
- * @date 2018年11月29日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-@Component
-@Order(100)
-public class JobsStartup implements ApplicationRunner {
-
-	@Autowired
-	private ScheduleJobRepo scheduleJobEntityRepo;
-
-	@Autowired
-	private QuartzManager quartzManager;
-
-	public void start() {
-
-		new Thread(new Runnable() {
-
-			@Override
-			public void run() {
-
-				// 延迟启动
-				try {
-					TimeUnit.SECONDS.sleep(30);
-				} catch (InterruptedException e) {
-					// ignore
-				}
-
-				List<ScheduleJobEntity> jobEntityList = scheduleJobEntityRepo.findAll();
-
-				for (ScheduleJobEntity jobEntity : jobEntityList) {
-					if (!jobEntity.getEnable()) {
-						continue;
-					}
-					ScheduleJob scheduleJob = new ScheduleJob();
-					scheduleJob.setJobId(jobEntity.getId());
-					scheduleJob.setJobName(jobEntity.getJobName());
-					scheduleJob.setJobGroup(jobEntity.getJobGroup());
-					scheduleJob.setSpringBean(jobEntity.getSpringBean());
-					scheduleJob.setCronExpression(jobEntity.getCronExpression());
-					scheduleJob.setStateful(jobEntity.getStateful());
-
-					scheduleJob.setExt1(jobEntity.getExt1());
-					scheduleJob.setExt2(jobEntity.getExt2());
-					scheduleJob.setExt3(jobEntity.getExt3());
-					scheduleJob.setExt4(jobEntity.getExt4());
-					scheduleJob.setExt5(jobEntity.getExt5());
-
-					quartzManager.addJob(scheduleJob);
-				}
-
-			}
-		}).start();
-
-	}
-
-	@Override
-	public void run(ApplicationArguments args) throws Exception {
-		start();
-	}
-
-}
+package cn.com.qmth.examcloud.task.starter.config;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
+import cn.com.qmth.examcloud.task.base.QuartzManager;
+import cn.com.qmth.examcloud.task.base.ScheduleJob;
+import cn.com.qmth.examcloud.task.dao.ScheduleJobRepo;
+import cn.com.qmth.examcloud.task.dao.entity.ScheduleJobEntity;
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年11月29日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@Component
+@Order(100)
+public class JobsStartup implements ApplicationRunner {
+
+	private static ExamCloudLog log = ExamCloudLogFactory.getLog(JobsStartup.class);
+
+	@Autowired
+	private ScheduleJobRepo scheduleJobEntityRepo;
+
+	@Autowired
+	private QuartzManager quartzManager;
+
+	@Autowired
+	private DiscoveryClient discoveryClient;
+
+	public void start() {
+
+		String appName = PropertyHolder.getString("spring.application.name");
+		if (StringUtils.isNotBlank(appName)) {
+			List<ServiceInstance> instances = discoveryClient.getInstances(appName);
+			if (!instances.isEmpty()) {
+				log.error("不允许多实例启动");
+				System.exit(-1);
+			}
+		}
+
+		new Thread(new Runnable() {
+
+			@Override
+			public void run() {
+
+				// 延迟启动
+				try {
+					TimeUnit.SECONDS.sleep(30);
+				} catch (InterruptedException e) {
+					// ignore
+				}
+
+				List<ScheduleJobEntity> jobEntityList = scheduleJobEntityRepo.findAll();
+
+				for (ScheduleJobEntity jobEntity : jobEntityList) {
+					if (!jobEntity.getEnable()) {
+						continue;
+					}
+					ScheduleJob scheduleJob = new ScheduleJob();
+					scheduleJob.setJobId(jobEntity.getId());
+					scheduleJob.setJobName(jobEntity.getJobName());
+					scheduleJob.setJobGroup(jobEntity.getJobGroup());
+					scheduleJob.setSpringBean(jobEntity.getSpringBean());
+					scheduleJob.setCronExpression(jobEntity.getCronExpression());
+					scheduleJob.setStateful(jobEntity.getStateful());
+
+					scheduleJob.setExt1(jobEntity.getExt1());
+					scheduleJob.setExt2(jobEntity.getExt2());
+					scheduleJob.setExt3(jobEntity.getExt3());
+					scheduleJob.setExt4(jobEntity.getExt4());
+					scheduleJob.setExt5(jobEntity.getExt5());
+
+					quartzManager.addJob(scheduleJob);
+				}
+
+			}
+		}).start();
+
+	}
+
+	@Override
+	public void run(ApplicationArguments args) throws Exception {
+		start();
+	}
+
+}