改为并发落库
This commit is contained in:
parent
6ee2875c30
commit
042dbfcecf
|
@ -6,6 +6,7 @@ import com.isu.gaswellwatch.entity.Response;
|
||||||
import com.isu.gaswellwatch.modbus.data.listener.Queues;
|
import com.isu.gaswellwatch.modbus.data.listener.Queues;
|
||||||
import com.isu.gaswellwatch.utils.HexUtil;
|
import com.isu.gaswellwatch.utils.HexUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -23,6 +24,11 @@ import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,6 +55,20 @@ public class Redis2DBPersistenceService {
|
||||||
private Map<String, PersistenceHandler> persistenceHandlerMap;
|
private Map<String, PersistenceHandler> persistenceHandlerMap;
|
||||||
private final RestTemplate restTemplate = new RestTemplate();
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
private final ThreadPoolExecutor persistenceThreadPool = new ThreadPoolExecutor(Math.min(50, Runtime.getRuntime().availableProcessors() - 1)
|
||||||
|
, Math.min(100, Runtime.getRuntime().availableProcessors() - 1), 5, TimeUnit.MINUTES,
|
||||||
|
new ArrayBlockingQueue<>(5000), new ThreadFactory() {
|
||||||
|
final AtomicInteger index = new AtomicInteger(1);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable runnable) {
|
||||||
|
Thread thread = new Thread(runnable, "Modbus-auto-collect-thread-" + this.index.getAndIncrement());
|
||||||
|
thread.setDaemon(true);
|
||||||
|
thread.setPriority(Thread.MIN_PRIORITY);
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
}, new ThreadPoolExecutor.CallerRunsPolicy());
|
||||||
|
|
||||||
@Scheduled(cron = "20/30 * * * * ? ")
|
@Scheduled(cron = "20/30 * * * * ? ")
|
||||||
public void write() {
|
public void write() {
|
||||||
Map<Long, String> idGatewayMappingMap = writeOnlineGateway();
|
Map<Long, String> idGatewayMappingMap = writeOnlineGateway();
|
||||||
|
@ -69,6 +89,20 @@ public class Redis2DBPersistenceService {
|
||||||
if (Objects.isNull(deviceId)) {
|
if (Objects.isNull(deviceId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
this.persistenceThreadPool.execute(new Worker(deviceId, cacheKey, operations, deviceMap, idGatewayMappingMap));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void persistenceDeviceData(Long deviceId,
|
||||||
|
String cacheKey,
|
||||||
|
HashOperations operations,
|
||||||
|
Map<String, Object> deviceMap,
|
||||||
|
Map<Long, String> idGatewayMappingMap) {
|
||||||
|
String tableName;
|
||||||
|
String queueName;
|
||||||
|
PersistenceHandler persistenceHandler;
|
||||||
|
List<Map<String, Object>> existsTableList;
|
||||||
try {
|
try {
|
||||||
if (Objects.nonNull(idGatewayMappingMap)) {
|
if (Objects.nonNull(idGatewayMappingMap)) {
|
||||||
updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
|
updateDeviceOnlineStatus(operations, deviceId, String.valueOf(idGatewayMappingMap.containsKey(deviceId)));
|
||||||
|
@ -86,7 +120,7 @@ public class Redis2DBPersistenceService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!this.redisTemplate.hasKey(PersistenceHandler.DEVICE_INFO_CACHE + deviceId)) {
|
if (!this.redisTemplate.hasKey(PersistenceHandler.DEVICE_INFO_CACHE + deviceId)) {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
tableName = DEFAULT_DATA_TABLE + deviceId;
|
tableName = DEFAULT_DATA_TABLE + deviceId;
|
||||||
queueName = Queues.getDeviceEventsQueue(deviceId);
|
queueName = Queues.getDeviceEventsQueue(deviceId);
|
||||||
|
@ -111,8 +145,6 @@ public class Redis2DBPersistenceService {
|
||||||
log.error("设备{}最新数据落库失败", deviceId, JSONUtil.toJsonStr(deviceMap), e);
|
log.error("设备{}最新数据落库失败", deviceId, JSONUtil.toJsonStr(deviceMap), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Long, String> writeOnlineGateway() {
|
private Map<Long, String> writeOnlineGateway() {
|
||||||
try {
|
try {
|
||||||
|
@ -156,4 +188,18 @@ public class Redis2DBPersistenceService {
|
||||||
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", onlineStatus);
|
operations.put(PersistenceHandler.DEVICE_DATA_CACHE + deviceId, "online", onlineStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
private class Worker implements Runnable {
|
||||||
|
private final Long deviceId;
|
||||||
|
private final String cacheKey;
|
||||||
|
private final HashOperations operations;
|
||||||
|
private final Map<String, Object> deviceMap;
|
||||||
|
private final Map<Long, String> idGatewayMappingMap;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Redis2DBPersistenceService.this.persistenceDeviceData(deviceId, cacheKey, operations, deviceMap, idGatewayMappingMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue