一键上线

new-master
DongZeLiang 2023-12-26 16:37:17 +08:00
parent eb1937a27b
commit 5c2fbb91dd
4 changed files with 49 additions and 56 deletions

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
@ -7,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -14,15 +14,15 @@ public class FixedThreadPool {
/**
* 线
*/
private final static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
private final static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(15);
/**
* 线
* @param runnable 线
* @param thread 线
*/
public static Future<?> submit(Runnable runnable){
return fixedThreadPool.submit(runnable);
public static Future<?> submit(Thread thread){
return fixedThreadPool.submit(thread);
}
/**

View File

@ -15,7 +15,8 @@ public class ScheduledThreadPool {
/**
* 线 CPU * 2 + 1
*/
private static final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2 + 1);
private static final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(
Runtime.getRuntime().availableProcessors() * 2 + 1);
public static ScheduledFuture<?> submit (Runnable thread){
// 参数分别是: 任务, 多久后开始执行, 每隔多久执行一次(周期),时间单位

View File

@ -1,6 +1,7 @@
package com.muyu.service.impl;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.muyu.common.pool.FixedThreadPool;
import com.muyu.domain.PositionRouteInfo;
import com.muyu.domain.model.PositionModel;
@ -16,9 +17,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
@ -48,62 +50,51 @@ public class VehicleUnifiedServiceImpl implements VehicleUnifiedService {
*/
@Override
public void unifiedOnline () {
// 获取离线车辆
List<String> vinList
= LocalContainer.getOfflineVehicleInstance().stream().map(VehicleInstance::getVin).toList();
// 获取执行多少次 执行大小 执行页码
int vinSize = vinList.size(), executionSize = 10;
// 总执行次数
int executionSum = vinSize/executionSize + vinSize % executionSize == 0 ? 0 : 1;
// 获取离线车辆VIN
List<String> vinList = LocalContainer.getOfflineVehicleInstance()
.stream()
.map(VehicleInstance::getVin)
.toList();
new Thread(() -> {
// 分页进行业务操作
for (int page = 0; page < executionSum; page++) {
// 是否继续等待
boolean await;
// 等待次数/最大等待次数
int waitSize = 0, waitMaxSize = 3;
CountDownLatch countDownLatch = new CountDownLatch(vinList.size());
int vinSize = 0, executionSize = 15;
do {
int startIndex = vinSize++ * executionSize;
// 进行分页开启车辆
List<String> executionVinList = vinList.stream()
.limit(page * executionSize)
.skip(executionSize)
.skip(startIndex)
.limit(executionSize)
.toList();
do {
try {
await = countDownLatch.await(5, TimeUnit.SECONDS);
log.info("等待一轮训,还剩余:[{}]", countDownLatch.getCount());
} catch (InterruptedException e) {
log.error("等待异常:{}", e.getMessage(), e);
await = true;
}
// 等待countdown或者等待轮训超过三次结束
} while (!await || waitSize++ < waitMaxSize);
}
taskModel.down();
}, "一键上线").start();
// 筛选出离线车辆并使用并行流进行上线操作
/*for (String vin : vinList) {
Future<?> submitFuture = FixedThreadPool.submit(
new Thread(() -> {
CountDownLatch countDownLatch = new CountDownLatch(executionVinList.size());
Map<String, Thread> startVehicleThread = new ConcurrentHashMap<>();
executionVinList.forEach(vin -> {
Thread thread = new Thread(() -> {
try {
vehicleInstanceService.vehicleClientInit(vin);
taskModel.incrementSuccess();
} catch (Exception exception) {
log.error("车辆上线异常:{}", exception.getMessage(), exception);
taskModel.incrementError();
startVehicleThread.remove(vin);
}catch (Exception interruptedException){
log.error(interruptedException);
}
countDownLatch.countDown();
})
);
}*/
});
startVehicleThread.put(vin, thread);
FixedThreadPool.submit(thread);
});
try {
boolean await = countDownLatch.await(5, TimeUnit.SECONDS);
log.info(
"开始:[{}] 结束:[{}],未上线成功:[{}], vin[{}]",
startIndex, startIndex+executionVinList.size(),
startVehicleThread.size(),
JSONObject.toJSONString(executionVinList));
if (!await){
startVehicleThread.values().forEach(Thread::interrupt);
}
} catch (InterruptedException ignored) {}
if (executionVinList.size() < executionSize){
break;
}
}while (true);
}).start();
}
/**
@ -112,7 +103,7 @@ public class VehicleUnifiedServiceImpl implements VehicleUnifiedService {
@Override
public void unifiedOffline () {
List<VehicleInstance> onlineVehicleInstanceList = LocalContainer.getOnlineVehicleInstance();
Thread taskThread = new Thread(() -> {
new Thread(() -> {
try {
// 筛选出在线车辆使用并行流操作先停止车辆上报动作再进行车辆离线操作
onlineVehicleInstanceList