|
@@ -1,17 +1,20 @@
|
|
|
package com.hb.proj.gather.scheduler;
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import com.hb.proj.gather.model.GatherConfig;
|
|
|
import com.hb.proj.gather.process.DataTransConfig;
|
|
|
import com.hb.proj.gather.protocol.ChannelGroupMgr;
|
|
|
+import com.hb.proj.utils.ConfigUtils;
|
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
+import jakarta.annotation.PreDestroy;
|
|
|
|
|
|
/**
|
|
|
* 采集定时任务
|
|
@@ -21,24 +24,88 @@ import io.netty.channel.Channel;
|
|
|
*/
|
|
|
|
|
|
@Component
|
|
|
-@EnableScheduling
|
|
|
+//@EnableScheduling
|
|
|
public class GatherScheduler {
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(GatherScheduler.class);
|
|
|
|
|
|
- private static int scheNum=0; //执行次数
|
|
|
+ //private static int scheNum=0; //执行次数
|
|
|
|
|
|
+ private Timer timer=null;
|
|
|
|
|
|
+ /*
|
|
|
+ * public static int getScheNum() { return scheNum; }
|
|
|
+ */
|
|
|
|
|
|
- public static int getScheNum() {
|
|
|
- return scheNum;
|
|
|
+ /**
|
|
|
+ * 采集启动入口
|
|
|
+ */
|
|
|
+ public void start() {
|
|
|
+ logger.info("采集启动入口");
|
|
|
+ GatherConfig config=ConfigUtils.get();
|
|
|
+ timer=new Timer();
|
|
|
+ timer.schedule(new TimerTask() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ startGather(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ }, 60000, config.getSingle()*1000); //单值1分钟后(系统启动等待设备连接)执行,每getSingle()秒重复执行
|
|
|
+
|
|
|
+ timer.schedule(new TimerTask() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ startGather(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ }, 120000, config.getDiagram()*1000); //多值2分钟后执行,每config.getDiagram()秒重复执行
|
|
|
+
|
|
|
+
|
|
|
+ timer.schedule(new TimerTask() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ startLiquidGather();
|
|
|
+ }
|
|
|
+
|
|
|
+ }, 180000, config.getLiquid()*1000); //动液面3分钟后执行,每config.getLiquid()秒重复执行
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * isMulti:true 多值采集,false:单值采集
|
|
|
+ * @param isMulti
|
|
|
+ */
|
|
|
+ public void startGather(boolean isMulti) {
|
|
|
+ logger.info("{}定时采集启动...",isMulti?"多值":"单值");
|
|
|
+ Iterator<Channel> iterator=ChannelGroupMgr.iterator();
|
|
|
+ Channel channel=null;
|
|
|
+ String serial=null;
|
|
|
+ while(iterator.hasNext()) {
|
|
|
+ channel=iterator.next();
|
|
|
+ serial=channel.attr(ChannelGroupMgr.ATTR_KEY_SERIAL).get();
|
|
|
+ if(DataTransConfig.isGatherDev(serial) && ChannelGroupMgr.isDone(serial)) { //isDone 保证同一设备同一时间只有一个任务在执行
|
|
|
+ ChannelGroupMgr.addFuture(serial,GatherTaskExecutor.submit(new GatherTask(channel,isMulti)));
|
|
|
+ }
|
|
|
+ else if(!ChannelGroupMgr.isDone(serial)) {
|
|
|
+ logger.warn("设备{}有任务未结束,本次采集忽略",serial);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 单值1分钟执行一次采集,首次等待1分钟,等待服务启动,设备连接上
|
|
|
*/
|
|
|
- @Scheduled(fixedRate=60*1000,initialDelay= 60000) //每分钟执行一次
|
|
|
+ //@Scheduled(fixedRate=60*1000,initialDelay= 60000) //每分钟执行一次
|
|
|
+ /*
|
|
|
public void startGather() {
|
|
|
logger.info("定时采集启动...");
|
|
|
scheNum+=1;
|
|
@@ -54,13 +121,13 @@ public class GatherScheduler {
|
|
|
|
|
|
scheNum=scheNum%5==0?0:scheNum;
|
|
|
}
|
|
|
-
|
|
|
+ */
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 动液面定时采集
|
|
|
*/
|
|
|
- @Scheduled(fixedRate = 20 * 60 * 1000,initialDelay= 300000)
|
|
|
+ //@Scheduled(fixedRate = 20 * 60 * 1000,initialDelay= 300000)
|
|
|
public void startLiquidGather() {
|
|
|
logger.info("动液面定时采集启动...");
|
|
|
Iterator<Channel> iterator=ChannelGroupMgr.iterator();
|
|
@@ -76,4 +143,13 @@ public class GatherScheduler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @PreDestroy
|
|
|
+ public void close() {
|
|
|
+ logger.info("即将清空采集定时器里的未执行的任务");
|
|
|
+ if(timer!=null) {
|
|
|
+ timer.cancel();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|