Skip to content

Commit 47ee52d

Browse files
authored
Filter refactor, keep all callback methods inside Filter.Listener (#5731)
1 parent 54b0d7e commit 47ee52d

File tree

19 files changed

+80
-76
lines changed

19 files changed

+80
-76
lines changed

dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* MonitorFilter. (SPI, Singleton, ThreadSafe)
5353
*/
5454
@Activate(group = {PROVIDER, CONSUMER})
55-
public class MonitorFilter implements Filter, Filter.Listener2 {
55+
public class MonitorFilter implements Filter, Filter.Listener {
5656

5757
private static final Logger logger = LoggerFactory.getLogger(MonitorFilter.class);
5858
private static final String MONITOR_FILTER_START_TIME = "monitor_filter_start_time";
@@ -96,7 +96,7 @@ private AtomicInteger getConcurrent(Invoker<?> invoker, Invocation invocation) {
9696
}
9797

9898
@Override
99-
public void onMessage(Result result, Invoker<?> invoker, Invocation invocation) {
99+
public void onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
100100
if (invoker.getUrl().hasParameter(MONITOR_KEY)) {
101101
collect(invoker, invocation, result, RpcContext.getContext().getRemoteHost(), (long) invocation.get(MONITOR_FILTER_START_TIME), false);
102102
getConcurrent(invoker, invocation).decrementAndGet(); // count down

dubbo-monitor/dubbo-monitor-api/src/test/java/org/apache/dubbo/monitor/support/MonitorFilterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void testFilter() throws Exception {
124124
Result result = monitorFilter.invoke(serviceInvoker, invocation);
125125
result.whenCompleteWithContext((r, t) -> {
126126
if (t == null) {
127-
monitorFilter.onMessage(r, serviceInvoker, invocation);
127+
monitorFilter.onResponse(r, serviceInvoker, invocation);
128128
} else {
129129
monitorFilter.onError(t, serviceInvoker, invocation);
130130
}
@@ -167,7 +167,7 @@ public void testGenericFilter() throws Exception {
167167
Result result = monitorFilter.invoke(serviceInvoker, invocation);
168168
result.whenCompleteWithContext((r, t) -> {
169169
if (t == null) {
170-
monitorFilter.onMessage(r, serviceInvoker, invocation);
170+
monitorFilter.onResponse(r, serviceInvoker, invocation);
171171
} else {
172172
monitorFilter.onError(t, serviceInvoker, invocation);
173173
}

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Filter.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,11 @@ public interface Filter {
4747
*/
4848
Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException;
4949

50-
/**
51-
* Please use {@link Listener2#onMessage(Result, Invoker, Invocation)} instead.
52-
* This method is kept only for compatibility and may get removed at any version in the future.
53-
*
54-
* @param appResponse
55-
* @param invoker
56-
* @param invocation
57-
*/
58-
@Deprecated
59-
default Result onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
60-
return appResponse;
61-
}
62-
63-
@Deprecated
6450
interface Listener {
6551

6652
void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation);
6753

6854
void onError(Throwable t, Invoker<?> invoker, Invocation invocation);
6955
}
70-
interface Listener2 {
71-
72-
void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation);
73-
74-
void onError(Throwable t, Invoker<?> invoker, Invocation invocation);
75-
}
76-
7756

7857
}

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/ListenableFilter.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,38 @@
1616
*/
1717
package org.apache.dubbo.rpc;
1818

19+
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.ConcurrentMap;
21+
1922
/**
20-
* This abstract will be removed soon from one future release.
21-
* Please implementing Filter.Listener2 directly for callback registration,
22-
* check the default implementation, see {@link org.apache.dubbo.rpc.filter.ExceptionFilter}, for example.
23+
* It's recommended to implement Filter.Listener directly for callback registration, check the default implementation,
24+
* see {@link org.apache.dubbo.rpc.filter.ExceptionFilter}, for example.
25+
* <p>
26+
* If you do not want to share Listener instance between RPC calls. You can use ListenableFilter
27+
* to keep a 'one Listener each RPC call' model.
2328
*/
24-
@Deprecated
2529
public abstract class ListenableFilter implements Filter {
2630

2731
protected Listener listener = null;
32+
protected final ConcurrentMap<Invocation, Listener> listeners = new ConcurrentHashMap<>();
2833

2934
public Listener listener() {
3035
return listener;
3136
}
3237

38+
public Listener listener(Invocation invocation) {
39+
Listener invListener = listeners.get(invocation);
40+
if (invListener == null) {
41+
invListener = listener;
42+
}
43+
return invListener;
44+
}
45+
46+
public void addListener(Invocation invocation, Listener listener) {
47+
listeners.putIfAbsent(invocation, listener);
48+
}
49+
50+
public void removeListener(Invocation invocation) {
51+
listeners.remove(invocation);
52+
}
3353
}

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ActiveLimitFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* @see Filter
4343
*/
4444
@Activate(group = CONSUMER, value = ACTIVES_KEY)
45-
public class ActiveLimitFilter implements Filter, Filter.Listener2 {
45+
public class ActiveLimitFilter implements Filter, Filter.Listener {
4646

4747
private static final String ACTIVELIMIT_FILTER_START_TIME = "activelimit_filter_start_time";
4848

@@ -82,7 +82,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
8282
}
8383

8484
@Override
85-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
85+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
8686
String methodName = invocation.getMethodName();
8787
URL url = invoker.getUrl();
8888
int max = invoker.getUrl().getMethodParameter(methodName, ACTIVES_KEY, 0);

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/CompatibleFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @see Filter
4646
*
4747
*/
48-
public class CompatibleFilter implements Filter, Filter.Listener2 {
48+
public class CompatibleFilter implements Filter, Filter.Listener {
4949

5050
private static Logger logger = LoggerFactory.getLogger(CompatibleFilter.class);
5151

@@ -55,7 +55,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
5555
}
5656

5757
@Override
58-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
58+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
5959
if (!invocation.getMethodName().startsWith("$") && !appResponse.hasException()) {
6060
Object value = appResponse.getValue();
6161
if (value != null) {

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* @see RpcContext
5252
*/
5353
@Activate(group = PROVIDER, order = -10000)
54-
public class ContextFilter implements Filter, Filter.Listener2 {
54+
public class ContextFilter implements Filter, Filter.Listener {
5555

5656
private static final String TAG_KEY = "dubbo.tag";
5757

@@ -125,7 +125,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
125125
}
126126

127127
@Override
128-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
128+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
129129
// pass attachments to result
130130
appResponse.addObjectAttachments(RpcContext.getServerContext().getObjectAttachments());
131131
}

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ExceptionFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* </ol>
4545
*/
4646
@Activate(group = CommonConstants.PROVIDER)
47-
public class ExceptionFilter implements Filter, Filter.Listener2 {
47+
public class ExceptionFilter implements Filter, Filter.Listener {
4848
private Logger logger = LoggerFactory.getLogger(ExceptionFilter.class);
4949

5050
@Override
@@ -53,7 +53,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
5353
}
5454

5555
@Override
56-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
56+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
5757
if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
5858
try {
5959
Throwable exception = appResponse.getException();

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030

3131

3232
/**
33-
*
3433
* The maximum parallel execution request count per method per service for the provider.If the max configured
3534
* <b>executes</b> is set to 10 and if invoke request where it is already 10 then it will throws exception. It
3635
* continue the same behaviour un till it is <10.
37-
*
3836
*/
3937
@Activate(group = CommonConstants.PROVIDER, value = EXECUTES_KEY)
40-
public class ExecuteLimitFilter implements Filter, Filter.Listener2 {
38+
public class ExecuteLimitFilter implements Filter, Filter.Listener {
4139

4240
private static final String EXECUTELIMIT_FILTER_START_TIME = "execugtelimit_filter_start_time";
4341

@@ -66,7 +64,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
6664
}
6765

6866
@Override
69-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
67+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
7068
RpcStatus.endCount(invoker.getUrl(), invocation.getMethodName(), getElapsed(invocation), true);
7169
}
7270

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* GenericInvokerFilter.
5454
*/
5555
@Activate(group = CommonConstants.PROVIDER, order = -20000)
56-
public class GenericFilter implements Filter, Filter.Listener2 {
56+
public class GenericFilter implements Filter, Filter.Listener {
5757

5858
@Override
5959
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
@@ -153,7 +153,7 @@ public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
153153
}
154154

155155
@Override
156-
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation inv) {
156+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
157157
if ((inv.getMethodName().equals($INVOKE) || inv.getMethodName().equals($INVOKE_ASYNC))
158158
&& inv.getArguments() != null
159159
&& inv.getArguments().length == 3

0 commit comments

Comments
 (0)