Commit 5fed9d5e by 罗志长

lock

parent 86c48256
...@@ -80,6 +80,15 @@ public class RedisUtil { ...@@ -80,6 +80,15 @@ public class RedisUtil {
return lock; 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 { ...@@ -87,6 +96,13 @@ public class RedisUtil {
lock.unlock(); 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) { public static void lock(String key, long expire, Runnable runnable) {
RLock lock = lock(key, expire); RLock lock = lock(key, expire);
try { try {
......
package com.makeit.task; package com.makeit.task;
import com.makeit.utils.DayDurationUtil; import com.makeit.utils.DayDurationUtil;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component @Component
@Slf4j @Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true") @ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
...@@ -17,6 +20,18 @@ public class DayDurationTask { ...@@ -17,6 +20,18 @@ public class DayDurationTask {
@Scheduled(cron = "0 10 * * * ?") @Scheduled(cron = "0 10 * * * ?")
public void updateDayDuration() { public void updateDayDuration() {
boolean isSuccess = false;
String key = "update:day:duration";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
dayDurationUtil.getAll(); dayDurationUtil.getAll();
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
} }
package com.makeit.task; package com.makeit.task;
import com.makeit.service.platform.device.PlatDeviceService; import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component @Component
@Slf4j @Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true") @ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
...@@ -17,14 +20,25 @@ public class IotSyncTask { ...@@ -17,14 +20,25 @@ public class IotSyncTask {
/** /**
* 一小时同步一次 * 一分钟同步一次
* 启用状态的租户才同步 * 启用状态的租户才同步
* 新增和更新平台端设备表 * 新增和更新平台端设备表
*/ */
@Scheduled(cron = "0 0/1 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void syncEquipmentInfo() { public void syncEquipmentInfo() {
boolean isSuccess = false;
String key = "sync:equipment:info";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
platDeviceService.savePlatDevice(); platDeviceService.savePlatDevice();
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
} }
...@@ -2,12 +2,15 @@ package com.makeit.task; ...@@ -2,12 +2,15 @@ package com.makeit.task;
import com.makeit.global.aspect.tenant.TenantIdIgnore; import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.elder.*; import com.makeit.service.platform.elder.*;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component @Component
@Slf4j @Slf4j
@ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true") @ConditionalOnProperty(value = {"iot.sync.enable"}, havingValue = "true")
...@@ -29,18 +32,40 @@ public class PlatElderReportTask { ...@@ -29,18 +32,40 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 1 * * ?") @Scheduled(cron = "0 0 1 * * ?")
@TenantIdIgnore @TenantIdIgnore
public void heartRespiratoryTask() { public void heartRespiratoryTask() {
boolean isSuccess = false;
String key = "task:heart:respiratory";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者每日呼吸,异常情况"); log.info("开始生成长者每日呼吸,异常情况");
platElderBreatheDayStatService.heartRespiratoryTask(); platElderBreatheDayStatService.heartRespiratoryTask();
log.info("生成长者每日呼吸,异常情况结束"); log.info("生成长者每日呼吸,异常情况结束");
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
@Scheduled(cron = "0 30 1 * * ?") @Scheduled(cron = "0 30 1 * * ?")
@TenantIdIgnore @TenantIdIgnore
public void coordinateRecordTask() { public void coordinateRecordTask() {
boolean isSuccess = false;
String key = "task:coordinate:record";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者每日实时定位"); log.info("开始生成长者每日实时定位");
platElderCoordinateRecordService.coordinateRecordTask(); platElderCoordinateRecordService.coordinateRecordTask();
log.info("生成长者每日实时定位结束"); log.info("生成长者每日实时定位结束");
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
/** /**
...@@ -49,10 +74,21 @@ public class PlatElderReportTask { ...@@ -49,10 +74,21 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 2 * * ?") @Scheduled(cron = "0 0 2 * * ?")
@TenantIdIgnore @TenantIdIgnore
public void elderHeartRespiratoryAnalysisTask() { public void elderHeartRespiratoryAnalysisTask() {
boolean isSuccess = false;
String key = "task:elder:heart:respiratory:analysis";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始定时分析长者呼吸心率"); log.info("开始定时分析长者呼吸心率");
platElderBreatheAnalysisService.elderHeartRespiratoryAnalysisTask(null,null); platElderBreatheAnalysisService.elderHeartRespiratoryAnalysisTask(null,null);
log.info("定时分析长者呼吸心率结束"); log.info("定时分析长者呼吸心率结束");
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
/** /**
* 生成长者呼吸心率 * 生成长者呼吸心率
...@@ -60,10 +96,21 @@ public class PlatElderReportTask { ...@@ -60,10 +96,21 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 30 2 * * ?") @Scheduled(cron = "0 30 2 * * ?")
@TenantIdIgnore @TenantIdIgnore
public void breatheHeartRateRecordTask() { public void breatheHeartRateRecordTask() {
boolean isSuccess = false;
String key = "task:breathe:heart:rate:record";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始生成长者呼吸心率"); log.info("开始生成长者呼吸心率");
platElderBreatheHeartRateRecordService.breatheHeartRateRecordTask(); platElderBreatheHeartRateRecordService.breatheHeartRateRecordTask();
log.info("生成长者呼吸心率结束"); log.info("生成长者呼吸心率结束");
} }
} finally {
if (isSuccess) {
RedisUtil.unlock(key);
}
}
}
/** /**
* 长者睡眠分析 * 长者睡眠分析
...@@ -71,8 +118,19 @@ public class PlatElderReportTask { ...@@ -71,8 +118,19 @@ public class PlatElderReportTask {
@Scheduled(cron = "0 0 8 * * ?") @Scheduled(cron = "0 0 8 * * ?")
@TenantIdIgnore @TenantIdIgnore
public void elderSleepSleepAnalysisTask() { public void elderSleepSleepAnalysisTask() {
boolean isSuccess = false;
String key = "task:elder:sleep:analysis";
try {
isSuccess = RedisUtil.tryLock(key, 0, 3, TimeUnit.SECONDS);
if (isSuccess) {
log.info("开始定时分析长者睡眠质量"); log.info("开始定时分析长者睡眠质量");
platElderSleepService.elderSleepSleepAnalysisTask(null,null); platElderSleepService.elderSleepSleepAnalysisTask(null,null);
log.info("定时分析长者睡眠质量结束"); 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