Browse Source

创建索引时新增公平锁

master
1049970895@qniao.cn 3 years ago
parent
commit
be16048849
1 changed files with 6 additions and 0 deletions
  1. 6
      iot-machine-state-event-generator-job/src/main/java/com/qniao/iot/machine/event/generator/job/IotMachineEventGeneratorJob.java

6
iot-machine-state-event-generator-job/src/main/java/com/qniao/iot/machine/event/generator/job/IotMachineEventGeneratorJob.java

@ -72,6 +72,7 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j @Slf4j
public class IotMachineEventGeneratorJob { public class IotMachineEventGeneratorJob {
@ -100,6 +101,8 @@ public class IotMachineEventGeneratorJob {
"where qmrs.iot_mac = ?\n" + "where qmrs.iot_mac = ?\n" +
" and qmrs.is_delete = 0"; " and qmrs.is_delete = 0";
private static final ReentrantLock lock = new ReentrantLock(true);
/** /**
* 当前索引日期后缀 * 当前索引日期后缀
*/ */
@ -420,6 +423,7 @@ public class IotMachineEventGeneratorJob {
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) {
// 创建索引 // 创建索引
@ -494,6 +498,8 @@ public class IotMachineEventGeneratorJob {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
}finally {
lock.unlock();
} }
} }

Loading…
Cancel
Save