2025-09-18 10:47:37 +08:00
|
|
|
|
package com.hivekion.room.bean;
|
|
|
|
|
|
|
2025-09-18 13:51:58 +08:00
|
|
|
|
import com.hivekion.room.RoomManager;
|
2025-09-18 10:47:37 +08:00
|
|
|
|
import com.hivekion.room.func.TaskAction;
|
|
|
|
|
|
import com.hivekion.scenario.entity.ScenarioTask;
|
|
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
import java.util.concurrent.ThreadFactory;
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* [类的简要说明]
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
* [详细描述,可选]
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author LiDongYU
|
|
|
|
|
|
* @since 2025/7/22
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class AbtParentTask implements TaskAction {
|
|
|
|
|
|
|
2025-09-18 11:41:46 +08:00
|
|
|
|
|
2025-09-18 10:47:37 +08:00
|
|
|
|
//任务数据
|
|
|
|
|
|
protected final ScenarioTask scenarioTask;
|
2025-09-18 11:43:14 +08:00
|
|
|
|
//房间ID
|
2025-09-18 10:47:37 +08:00
|
|
|
|
protected final String roomId;
|
2025-09-18 11:43:14 +08:00
|
|
|
|
//http请求
|
2025-09-18 10:47:37 +08:00
|
|
|
|
protected WebClient webClient = WebClient.create();
|
2025-09-18 11:43:14 +08:00
|
|
|
|
//线程池
|
2025-09-18 11:41:46 +08:00
|
|
|
|
protected ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
2025-09-18 10:47:37 +08:00
|
|
|
|
5, // 核心线程数
|
|
|
|
|
|
10, // 最大线程数
|
|
|
|
|
|
60L, // 空闲线程存活时间
|
|
|
|
|
|
TimeUnit.SECONDS, // 时间单位
|
|
|
|
|
|
new LinkedBlockingQueue<>(100), // 任务队列
|
|
|
|
|
|
new CustomThreadFactory("MyPool"), // 线程工厂
|
|
|
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
public AbtParentTask(ScenarioTask scenarioTask, String roomId) {
|
|
|
|
|
|
this.scenarioTask = scenarioTask;
|
|
|
|
|
|
this.roomId = roomId;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 13:51:58 +08:00
|
|
|
|
public void addScheduledExecutorServiceRefenceToRoom(
|
|
|
|
|
|
ScheduledExecutorService scheduledExecutorService) {
|
|
|
|
|
|
RoomManager.addFuture(scheduledExecutorService, this.roomId);
|
|
|
|
|
|
}
|
2025-09-18 10:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void doSomeThing() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String getId() {
|
|
|
|
|
|
return scenarioTask.getId();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String getType() {
|
|
|
|
|
|
return scenarioTask.getTaskType();
|
|
|
|
|
|
}
|
2025-09-18 14:10:33 +08:00
|
|
|
|
|
2025-09-18 13:51:58 +08:00
|
|
|
|
//获取房间的持续时间
|
|
|
|
|
|
public long getDuringTime() {
|
|
|
|
|
|
return RoomManager.getRoomDuringTime(this.roomId);
|
|
|
|
|
|
}
|
2025-09-18 14:10:33 +08:00
|
|
|
|
//获取房间状态
|
|
|
|
|
|
public boolean getRoomStatus() {
|
|
|
|
|
|
return RoomManager.isRunning(roomId);
|
|
|
|
|
|
}
|
2025-09-18 10:47:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义线程工厂
|
|
|
|
|
|
class CustomThreadFactory implements ThreadFactory {
|
|
|
|
|
|
|
|
|
|
|
|
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
|
|
|
|
|
private final String namePrefix;
|
|
|
|
|
|
|
|
|
|
|
|
public CustomThreadFactory(String namePrefix) {
|
|
|
|
|
|
this.namePrefix = namePrefix + "-thread-";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Thread newThread(Runnable r) {
|
|
|
|
|
|
Thread thread = new Thread(r, namePrefix + threadNumber.getAndIncrement());
|
|
|
|
|
|
thread.setDaemon(false); // 设置为非守护线程
|
|
|
|
|
|
thread.setPriority(Thread.NORM_PRIORITY);
|
|
|
|
|
|
return thread;
|
|
|
|
|
|
}
|
2025-09-18 14:10:33 +08:00
|
|
|
|
|
2025-09-18 10:47:37 +08:00
|
|
|
|
}
|