Commit 5fed9d5e by 罗志长

lock

parent 86c48256
......@@ -80,6 +80,15 @@ public class RedisUtil {
return lock;
}
public static boolean tryLock(String key, long waitTime, long leaseTime, TimeUnit unit) {
RLock lock = client.getLock(getProjectName() + RedisUtil.LOCK + key);
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (InterruptedException e) {
return false;
}
}
/**
* 关闭锁
*/
......@@ -87,6 +96,13 @@ public class RedisUtil {
lock.unlock();
}
public static void unlock(String key) {
RLock lock = client.getLock(getProjectName() + RedisUtil.LOCK + key);
if (lock.isHeldByCurrentThread()) {
unlock(lock);
}
}
public static void lock(String key, long expire, Runnable runnable) {
RLock lock = lock(key, expire);
try {
......
package com.makeit.task;
import com.makeit.utils.DayDurationUtil;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
......@@ -17,6 +20,18 @@ public class DayDurationTask {
@Scheduled(cron = "0 10 * * * ?")
public void updateDayDuration() {
dayDurationUtil.getAll();
boolean isSuccess = false;
String key = "update:day:duration";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
dayDurationUtil.getAll();
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
}
package com.makeit.task;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
......@@ -17,13 +20,24 @@ public class IotSyncTask {
/**
* 一小时同步一次
* 一分钟同步一次
* 启用状态的租户才同步
* 新增和更新平台端设备表
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void syncEquipmentInfo() {
platDeviceService.savePlatDevice();
boolean isSuccess = false;
String key = "sync:equipment:info";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
platDeviceService.savePlatDevice();
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
......
......@@ -2,12 +2,15 @@ package com.makeit.task;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.elder.*;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
......@@ -29,17 +32,39 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 1 * * ?")
@TenantIdIgnore
public void heartRespiratoryTask() {
log.info("开始生成长者每日呼吸,异常情况");
platElderBreatheDayStatService.heartRespiratoryTask();
log.info("生成长者每日呼吸,异常情况结束");
boolean isSuccess = false;
String key = "task:heart:respiratory";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者每日呼吸,异常情况");
platElderBreatheDayStatService.heartRespiratoryTask();
log.info("生成长者每日呼吸,异常情况结束");
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
@Scheduled(cron = "0 30 1 * * ?")
@TenantIdIgnore
public void coordinateRecordTask() {
log.info("开始生成长者每日实时定位");
platElderCoordinateRecordService.coordinateRecordTask();
log.info("生成长者每日实时定位结束");
boolean isSuccess = false;
String key = "task:coordinate:record";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者每日实时定位");
platElderCoordinateRecordService.coordinateRecordTask();
log.info("生成长者每日实时定位结束");
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
......@@ -49,9 +74,20 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 2 * * ?")
@TenantIdIgnore
public void elderHeartRespiratoryAnalysisTask() {
log.info("开始定时分析长者呼吸心率");
platElderBreatheAnalysisService.elderHeartRespiratoryAnalysisTask(null,null);
log.info("定时分析长者呼吸心率结束");
boolean isSuccess = false;
String key = "task:elder:heart:respiratory:analysis";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始定时分析长者呼吸心率");
platElderBreatheAnalysisService.elderHeartRespiratoryAnalysisTask(null,null);
log.info("定时分析长者呼吸心率结束");
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
/**
......@@ -60,9 +96,20 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 30 2 * * ?")
@TenantIdIgnore
public void breatheHeartRateRecordTask() {
log.info("开始生成长者呼吸心率");
platElderBreatheHeartRateRecordService.breatheHeartRateRecordTask();
log.info("生成长者呼吸心率结束");
boolean isSuccess = false;
String key = "task:breathe:heart:rate:record";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者呼吸心率");
platElderBreatheHeartRateRecordService.breatheHeartRateRecordTask();
log.info("生成长者呼吸心率结束");
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
/**
......@@ -71,8 +118,19 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 8 * * ?")
@TenantIdIgnore
public void elderSleepSleepAnalysisTask() {
log.info("开始定时分析长者睡眠质量");
platElderSleepService.elderSleepSleepAnalysisTask(null,null);
log.info("定时分析长者睡眠质量结束");
boolean isSuccess = false;
String key = "task:elder:sleep:analysis";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始定时分析长者睡眠质量");
platElderSleepService.elderSleepSleepAnalysisTask(null,null);
log.info("定时分析长者睡眠质量结束");
}
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment