diff --git a/iot-device-power-on-and-off-data-job/src/main/java/com/qniao/iot/device/power/IotDevicePowerOnAndOffDataJob.java b/iot-device-power-on-and-off-data-job/src/main/java/com/qniao/iot/device/power/IotDevicePowerOnAndOffDataJob.java index fd4c6c7..a8a6c00 100644 --- a/iot-device-power-on-and-off-data-job/src/main/java/com/qniao/iot/device/power/IotDevicePowerOnAndOffDataJob.java +++ b/iot-device-power-on-and-off-data-job/src/main/java/com/qniao/iot/device/power/IotDevicePowerOnAndOffDataJob.java @@ -58,6 +58,7 @@ import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.locks.ReentrantLock; @Slf4j public class IotDevicePowerOnAndOffDataJob { @@ -82,6 +83,8 @@ public class IotDevicePowerOnAndOffDataJob { return requestConfigBuilder; })); + private static final ReentrantLock lock = new ReentrantLock(true); + /** * 当前索引日期后缀 */ @@ -328,6 +331,7 @@ public class IotDevicePowerOnAndOffDataJob { GetIndexRequest exist = new GetIndexRequest(indicesName); // 先判断客户端是否存在 try { + lock.lock(); boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT); if (!exists) { // 创建索引 @@ -387,6 +391,8 @@ public class IotDevicePowerOnAndOffDataJob { } } catch (Exception e) { e.printStackTrace(); + }finally { + lock.unlock(); } } }