|
|
|
|
package com.glxp.api.task;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
|
|
import com.glxp.api.dao.schedule.ScheduledDao;
|
|
|
|
|
import com.glxp.api.dao.system.SyncDataSetDao;
|
|
|
|
|
import com.glxp.api.entity.collect.IoCollectSet;
|
|
|
|
|
import com.glxp.api.entity.system.ScheduledEntity;
|
|
|
|
|
import com.glxp.api.entity.system.SyncDataSetEntity;
|
|
|
|
|
import com.glxp.api.req.system.ScheduledRequest;
|
|
|
|
|
import com.glxp.api.service.sync.HeartService;
|
|
|
|
|
import com.glxp.api.util.IntUtil;
|
|
|
|
|
import com.glxp.api.util.RedisUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
|
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@EnableScheduling
|
|
|
|
|
public class SyncHeartTask implements SchedulingConfigurer {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
protected ScheduledDao scheduledDao;
|
|
|
|
|
@Resource
|
|
|
|
|
SyncHeartService syncHeartService;
|
|
|
|
|
@Resource
|
|
|
|
|
RedisUtil redisUtil;
|
|
|
|
|
@Resource
|
|
|
|
|
HeartService heartService;
|
|
|
|
|
@Resource
|
|
|
|
|
private SyncDataSetDao syncDataSetDao;
|
|
|
|
|
private volatile ExecutorService executor;
|
|
|
|
|
|
|
|
|
|
private ExecutorService getExecutor() {
|
|
|
|
|
if (null == executor) {
|
|
|
|
|
synchronized (this) {
|
|
|
|
|
executor = ThreadUtil.newExecutor(10, 100, Integer.MAX_VALUE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return executor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
|
|
|
|
|
|
|
|
|
scheduledTaskRegistrar.addTriggerTask(() -> process(),
|
|
|
|
|
triggerContext -> {
|
|
|
|
|
ScheduledRequest scheduledRequest = new ScheduledRequest();
|
|
|
|
|
scheduledRequest.setCronName("heartTask");
|
|
|
|
|
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
|
|
|
|
|
String cron = scheduledEntity.getCron();
|
|
|
|
|
if (cron.isEmpty()) {
|
|
|
|
|
log.error("cron is null");
|
|
|
|
|
}
|
|
|
|
|
return new CronTrigger(cron).nextExecutionTime(triggerContext);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void process() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String lockKey = "syncProcessTask_lock";
|
|
|
|
|
try {
|
|
|
|
|
// 尝试获取分布式锁,设置20*60秒超时
|
|
|
|
|
boolean locked = (Boolean) redisUtil.get(lockKey);
|
|
|
|
|
if (locked) {
|
|
|
|
|
log.info("延迟分标定时任务已在其他节点执行中");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
redisUtil.set(lockKey, true);
|
|
|
|
|
getExecutor().submit(() -> {
|
|
|
|
|
syncHeartService.syncProcess();
|
|
|
|
|
redisUtil.set(lockKey, false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
// 释放分布式锁
|
|
|
|
|
redisUtil.set(lockKey, false);
|
|
|
|
|
log.info("延迟分标定时任务执行结束,已释放锁");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|