Commit 0998ed96 authored by duanruiming's avatar duanruiming

[update] 优化测试轨迹

parent 8650e8f3
package net.wanji.web.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author duanruiming
* @date 2023/02/14 10:17
*/
@Configuration
public class ThreadPoolConfig {
@Value("${threadPoolConfig.threadPoolName}")
private String threadPoolName;
@Value("${threadPoolConfig.coreSize}")
private int coreSize;
@Value("${threadPoolConfig.maxSize}")
private int maxSize;
@Value("${threadPoolConfig.queueCapacity}")
private int queueCapacity;
@Value("${threadPoolConfig.keepAliveTime}")
private int keepAliveTime;
@Value("${threadPoolConfig.allowCoreTimeOut}")
private boolean allowCoreTimeOut;
@Bean("threadPoolExecutor")
public ThreadPoolTaskExecutor threadPoolExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
ThreadFactory build = new ThreadFactoryBuilder().setNameFormat(threadPoolName).build();
executor.setThreadFactory(build);
executor.setCorePoolSize(coreSize);
executor.setMaxPoolSize(maxSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setAllowCoreThreadTimeOut(allowCoreTimeOut);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
executor.initialize();
return executor;
}
}
...@@ -10,12 +10,19 @@ import net.wanji.web.kafka.ConsumerHandler; ...@@ -10,12 +10,19 @@ import net.wanji.web.kafka.ConsumerHandler;
import net.wanji.web.kafka.JNMatchResultMiniData; import net.wanji.web.kafka.JNMatchResultMiniData;
import net.wanji.web.websocket.WebSocketServer; import net.wanji.web.websocket.WebSocketServer;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
...@@ -28,10 +35,12 @@ public class RealTimeCarTask { ...@@ -28,10 +35,12 @@ public class RealTimeCarTask {
@Resource @Resource
private ConsumerHandler consumerHandler; private ConsumerHandler consumerHandler;
@Autowired
@Qualifier(value = "threadPoolExecutor")
private ThreadPoolTaskExecutor threadPoolExecutor;
private static final List<TempPojo> tempList = new ArrayList<>(7); private static final List<TempPojo> tempList = new ArrayList<>(7);
private static final AtomicInteger atomicInteger = new AtomicInteger(0);
private final AtomicInteger atomicInteger = new AtomicInteger(0);
static { static {
tempList.add(new TempPojo(112.967134, 28.182384)); tempList.add(new TempPojo(112.967134, 28.182384));
...@@ -41,17 +50,27 @@ public class RealTimeCarTask { ...@@ -41,17 +50,27 @@ public class RealTimeCarTask {
tempList.add(new TempPojo(112.963092, 28.184083)); tempList.add(new TempPojo(112.963092, 28.184083));
tempList.add(new TempPojo(112.963606, 28.188246)); tempList.add(new TempPojo(112.963606, 28.188246));
tempList.add(new TempPojo(112.963711, 28.189290)); tempList.add(new TempPojo(112.963711, 28.189290));
tempList.add(new TempPojo(112.967486, 28.187118));
} }
@Scheduled(fixedRate = 2000) @Scheduled(fixedRate = 2000)
public void pushRealTimeCarInfo() { public void pushRealTimeCarInfo() {
try { // threadPoolExecutor.execute(() -> {
int i = atomicInteger.get();
atomicInteger.incrementAndGet();
if (i == 7) {
atomicInteger.set(0);
}
Set<WebSocketServer> events = WebSocketServer.getWebSocketSet(Constant.WEBSOCKET_TOPIC_CAR_TRAIL_INFO); Set<WebSocketServer> events = WebSocketServer.getWebSocketSet(Constant.WEBSOCKET_TOPIC_CAR_TRAIL_INFO);
if (CollectionUtil.isEmpty(events)) { if (CollectionUtil.isEmpty(events)) {
return; return;
} }
for (WebSocketServer socketServer : events) { for (WebSocketServer socketServer : events) {
String picLicense = socketServer.getUserId(); String userId = socketServer.getUserId();
String picLicense = null;
if (StringUtils.isNotBlank(userId)) {
picLicense = userId.substring(0, 7);
}
JNMatchResultMiniData jnMatchResultMiniData = consumerHandler.getTopicMessage("CSMatchResultMiniData_0"); JNMatchResultMiniData jnMatchResultMiniData = consumerHandler.getTopicMessage("CSMatchResultMiniData_0");
if (Objects.nonNull(jnMatchResultMiniData)) { if (Objects.nonNull(jnMatchResultMiniData)) {
List<JNMatchResultMiniData.E1FrameParticipant> list = jnMatchResultMiniData.getE1FrameParticipant(); List<JNMatchResultMiniData.E1FrameParticipant> list = jnMatchResultMiniData.getE1FrameParticipant();
...@@ -59,28 +78,24 @@ public class RealTimeCarTask { ...@@ -59,28 +78,24 @@ public class RealTimeCarTask {
for (JNMatchResultMiniData.E1FrameParticipant e1FrameParticipant : list) { for (JNMatchResultMiniData.E1FrameParticipant e1FrameParticipant : list) {
int id = e1FrameParticipant.getId(); int id = e1FrameParticipant.getId();
String license = e1FrameParticipant.getPicLicense(); String license = e1FrameParticipant.getPicLicense();
if (3573 == e1FrameParticipant.getId() || StringUtils.equals(license, picLicense)) { // 测试车牌;京WJ0001
for (int i = 0; i < tempList.size(); i++) { license = "京WJ0001";
if (i == atomicInteger.get()) { if (3573 == e1FrameParticipant.getId() && StringUtils.equals(license, picLicense)) {
atomicInteger.getAndIncrement(); TempPojo tempPojo = RealTimeCarTask.tempList.get(i);
if (i == 6) {
atomicInteger.set(0);
}
TempPojo tempPojo = tempList.get(i);
e1FrameParticipant.setLongitude(tempPojo.getLongitude()); e1FrameParticipant.setLongitude(tempPojo.getLongitude());
e1FrameParticipant.setLatitude(tempPojo.getLatitude()); e1FrameParticipant.setLatitude(tempPojo.getLatitude());
break; e1FrameParticipant.setPicLicense(license);
} try {
}
socketServer.sendMessage(JSONObject.toJSONString(e1FrameParticipant)); socketServer.sendMessage(JSONObject.toJSONString(e1FrameParticipant));
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
} }
} }
} }
} catch (Exception e) {
log.error("通过车牌推送数据失败,异常信息", e);
} }
// });
} }
@Data @Data
......
...@@ -58,6 +58,7 @@ public class WebSocketServer { ...@@ -58,6 +58,7 @@ public class WebSocketServer {
Set<WebSocketServer> set = webSocketMap.get(topic); Set<WebSocketServer> set = webSocketMap.get(topic);
set.add(this); set.add(this);
webSocketMap.put(topic, set); webSocketMap.put(topic, set);
addOnlineCount();
currentTopicSize = set.size(); currentTopicSize = set.size();
//加入set中 //加入set中
} else { } else {
......
...@@ -58,3 +58,11 @@ mybatis-plus: ...@@ -58,3 +58,11 @@ mybatis-plus:
auto-mapping-unknown-column-behavior: warning auto-mapping-unknown-column-behavior: warning
#开启SQL打印 #开启SQL打印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#线程池配置
threadPoolConfig:
threadPoolName: threadPoolExecutor
coreSize: 8
maxSize: 16
queueCapacity: 200
keepAliveTime: 6000
allowCoreTimeOut: false
\ No newline at end of file
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 to comment