Browse Source

创建索引时新增公平锁

hph-优化版本
1049970895@qniao.cn 3 years ago
parent
commit
0a0e20ae08
1 changed files with 6 additions and 0 deletions
  1. 6
      iot-device-power-on-and-off-data-job/src/main/java/com/qniao/iot/device/power/IotDevicePowerOnAndOffDataJob.java

6
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.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j @Slf4j
public class IotDevicePowerOnAndOffDataJob { public class IotDevicePowerOnAndOffDataJob {
@ -82,6 +83,8 @@ public class IotDevicePowerOnAndOffDataJob {
return requestConfigBuilder; return requestConfigBuilder;
})); }));
private static final ReentrantLock lock = new ReentrantLock(true);
/** /**
* 当前索引日期后缀 * 当前索引日期后缀
*/ */
@ -328,6 +331,7 @@ public class IotDevicePowerOnAndOffDataJob {
GetIndexRequest exist = new GetIndexRequest(indicesName); GetIndexRequest exist = new GetIndexRequest(indicesName);
// 先判断客户端是否存在 // 先判断客户端是否存在
try { try {
lock.lock();
boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT); boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT);
if (!exists) { if (!exists) {
// 创建索引 // 创建索引
@ -387,6 +391,8 @@ public class IotDevicePowerOnAndOffDataJob {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}finally {
lock.unlock();
} }
} }
} }
Loading…
Cancel
Save