Skip to content

Commit a16e96a

Browse files
authored
Merge pull request #81 from chenzhiguo/v1.3.2
Fix #80
2 parents 3ad4016 + cc720ed commit a16e96a

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/bytekit/advice/AdviceHandler.java

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.function.BiConsumer;
2727

28-
import static com.jd.live.agent.bootstrap.bytekit.advice.ExceptionHandler.EMPTY_EXCEPTION_HANDLER;
29-
3028
/**
3129
* A handler class for managing advices and their associated interceptors. Provides static methods
3230
* for handling entry and exit points of method execution contexts, as well as managing advice lifecycle.
@@ -60,29 +58,27 @@ public static <T extends ExecutableContext> void onEnter(T context, String advic
6058
AdviceDesc adviceDesc = advices.get(adviceKey);
6159
List<Interceptor> interceptors = adviceDesc == null ? null : adviceDesc.getInterceptors();
6260
if (interceptors != null) {
63-
onEnter(context, interceptors, EMPTY_EXCEPTION_HANDLER);
61+
onEnter(context, interceptors);
6462
}
6563
}
6664

6765
/**
6866
* Handles the entry point for a given execution context and a list of interceptors.
6967
*
70-
* @param <T> the type of the execution context
71-
* @param context the execution context
72-
* @param interceptors the list of interceptors to be executed
73-
* @param exceptionHandler the handler for exceptions
68+
* @param <T> the type of the execution context
69+
* @param context the execution context
70+
* @param interceptors the list of interceptors to be executed
7471
* @throws Throwable if any exception occurs during interception
7572
*/
7673
public static <T extends ExecutableContext> void onEnter(T context,
77-
List<Interceptor> interceptors,
78-
ExceptionHandler exceptionHandler) throws Throwable {
74+
List<Interceptor> interceptors) throws Throwable {
7975
if (context == null || interceptors == null)
8076
return;
8177
for (Interceptor interceptor : interceptors) {
8278
if (logger.isDebugEnabled()) {
8379
logger.debug(String.format("enter [%s], interceptor is [%s].", context.getDescription(), interceptor.getClass().getName()));
8480
}
85-
handle(context, interceptor, Interceptor::onEnter, exceptionHandler, "enter");
81+
handle(context, interceptor, Interceptor::onEnter, "enter");
8682
if (!context.isSuccess()) {
8783
throw context.getThrowable();
8884
} else if (context.isSkip()) {
@@ -104,60 +100,57 @@ public static <T extends ExecutableContext> void onExit(T context, String advice
104100
AdviceDesc adviceDesc = advices.get(adviceKey);
105101
List<Interceptor> interceptors = adviceDesc == null ? null : adviceDesc.getInterceptors();
106102
if (interceptors != null) {
107-
onExit(context, interceptors, EMPTY_EXCEPTION_HANDLER);
103+
onExit(context, interceptors);
108104
}
109105
}
110106

111107
/**
112108
* Handles the exit point for a given execution context and a list of interceptors.
113109
*
114-
* @param <T> the type of the execution context
115-
* @param context the execution context
116-
* @param interceptors the list of interceptors to be executed
117-
* @param exceptionHandler the handler for exceptions
110+
* @param <T> the type of the execution context
111+
* @param context the execution context
112+
* @param interceptors the list of interceptors to be executed
118113
* @throws Throwable if any exception occurs during interception
119114
*/
120115
public static <T extends ExecutableContext> void onExit(T context,
121-
List<Interceptor> interceptors,
122-
ExceptionHandler exceptionHandler) throws Throwable {
116+
List<Interceptor> interceptors) throws Throwable {
123117
if (context == null || interceptors == null)
124118
return;
125119
for (Interceptor interceptor : interceptors) {
126120
if (logger.isDebugEnabled()) {
127121
logger.debug(String.format("exit [%s], interceptor is [%s].", context.getDescription(), interceptor.getClass().getName()));
128122
}
129123
if (context.isSuccess()) {
130-
handle(context, interceptor, Interceptor::onSuccess, exceptionHandler, "success");
124+
handle(context, interceptor, Interceptor::onSuccess, "success");
131125
} else {
132-
handle(context, interceptor, Interceptor::onError, exceptionHandler, "recover");
126+
handle(context, interceptor, Interceptor::onError, "recover");
133127
}
134-
handle(context, interceptor, Interceptor::onExit, exceptionHandler, "exit");
128+
handle(context, interceptor, Interceptor::onExit, "exit");
135129
}
136130
}
137131

138132
/**
139133
* Generic method for handling interception actions.
140134
*
141-
* @param <T> the type of the execution context
142-
* @param context the execution context
143-
* @param interceptor the interceptor to be executed
144-
* @param consumer the action to be performed by the interceptor
145-
* @param errorHandler the handler for exceptions
146-
* @param action the name of the action being performed
135+
* @param <T> the type of the execution context
136+
* @param context the execution context
137+
* @param interceptor the interceptor to be executed
138+
* @param consumer the action to be performed by the interceptor
139+
* @param action the name of the action being performed
147140
* @throws Throwable if any exception occurs during interception
148141
*/
149142
private static <T extends ExecutableContext> void handle(T context,
150143
Interceptor interceptor,
151144
BiConsumer<Interceptor, T> consumer,
152-
ExceptionHandler errorHandler,
153145
String action) throws Throwable {
154146
try {
155147
consumer.accept(interceptor, context);
156148
} catch (Throwable t) {
157149
logger.error(String.format("failed to %s %s, caused by %s", action, context.getDescription(), t.getMessage()), t);
158-
if (errorHandler != null) {
150+
ExceptionHandler exceptionHandler = interceptor.getExceptionHandler();
151+
if (exceptionHandler != null) {
159152
try {
160-
errorHandler.handle(context, interceptor, t);
153+
exceptionHandler.handle(context, interceptor, t);
161154
} catch (Throwable e) {
162155
logger.error(String.format("failed to handle %s %s error, caused by %s", action, context.getDescription(), t.getMessage()), t);
163156
}

joylive-bootstrap/joylive-bootstrap-api/src/main/java/com/jd/live/agent/bootstrap/plugin/definition/Interceptor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.jd.live.agent.bootstrap.plugin.definition;
1717

18+
import com.jd.live.agent.bootstrap.bytekit.advice.ExceptionHandler;
1819
import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
1920

2021
/**
@@ -53,5 +54,14 @@ public interface Interceptor {
5354
* @param ctx the executable context
5455
*/
5556
void onExit(ExecutableContext ctx);
57+
58+
/**
59+
* Current Interceptor's ExceptionHandler
60+
*
61+
* @return ExceptionHandler
62+
*/
63+
default ExceptionHandler getExceptionHandler() {
64+
return null;
65+
}
5666
}
5767

0 commit comments

Comments
 (0)