初始化仓库
This commit is contained in:
29
xtools-boot-thread/pom.xml
Normal file
29
xtools-boot-thread/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-boot-thread</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot-log -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-log</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.thread;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.thread.selector.BootThreadImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootThreadConfiguration</p>
|
||||
* <p>Description : BootThreadConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootThreadImportSelector.class)
|
||||
public class BootThreadConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootThreadConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootThreadConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package xtools.boot.thread.callback;
|
||||
|
||||
/**
|
||||
* <p>Title : VirtualThreadTaskCallback</p>
|
||||
* <p>Description : VirtualThreadTaskCallback</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/8 20:48
|
||||
*/
|
||||
public interface VirtualThreadTaskCallback {
|
||||
|
||||
/**
|
||||
* 回调方法
|
||||
*/
|
||||
void callback();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.thread.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootThreadImportSelector</p>
|
||||
* <p>Description : BootThreadImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootThreadImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.thread");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package xtools.boot.thread.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xtools.boot.api.model.dto.log.HolderLogTrack;
|
||||
import xtools.boot.api.model.dto.log.LogTrack;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.boot.log.holder.LogTrackHolder;
|
||||
import xtools.boot.thread.callback.VirtualThreadTaskCallback;
|
||||
import xtools.core.enums.LogLevel;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* <p>Title : VirtualThreadTaskUtils</p>
|
||||
* <p>Description : VirtualThreadTaskUtils</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/8 20:46
|
||||
*/
|
||||
@Slf4j
|
||||
public class VirtualThreadTaskUtils {
|
||||
|
||||
/**
|
||||
* 创建子线程并执行任务(不带日志记录,不推荐)
|
||||
*
|
||||
* @param callback 回调
|
||||
*/
|
||||
public static void simple(VirtualThreadTaskCallback callback) {
|
||||
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
|
||||
try {
|
||||
// 创建子线程,并异步提交
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
callback.callback();
|
||||
} catch (Exception e) {
|
||||
log.error("子线程执行异常", e);
|
||||
}
|
||||
}, service);
|
||||
} finally {
|
||||
// 关闭线程池
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建子线程并执行任务(带日志记录,推荐)
|
||||
*
|
||||
* @param callback 回调
|
||||
*/
|
||||
public static void execute(VirtualThreadTaskCallback callback) {
|
||||
// 获取父线程的追踪日志信息
|
||||
LogTrack logTrack = LogTrackHolder.get();
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.VIRTUAL_THREAD, logTrack).title("创建虚拟线程").save();
|
||||
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
|
||||
try {
|
||||
// 创建子线程,并异步提交
|
||||
CompletableFuture.runAsync(() -> {
|
||||
// 生成子线程的追踪日志信息
|
||||
HolderLogTrack holderLogTrack = LogTrackHolder.newThread(logTrack);
|
||||
ScopedValue.where(LogTrackHolder.getScoped(), holderLogTrack).run(() -> {
|
||||
try {
|
||||
callback.callback();
|
||||
} catch (Exception e) {
|
||||
LogBus.init(LogLevel.ERROR, LogBusBaseType.VIRTUAL_THREAD).error(e).title("子线程执行异常").save();
|
||||
}
|
||||
});
|
||||
}, service);
|
||||
} finally {
|
||||
// 关闭线程池
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.thread.BootThreadConfiguration
|
||||
Reference in New Issue
Block a user