Skip to content

Commit 49c5a5e

Browse files
committed
feat: 新增关闭监听器及超时管理器控制方法
1 parent f175ea1 commit 49c5a5e

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

trpc-proto/trpc-proto-http/src/main/java/com/tencent/trpc/proto/http/client/AbstractConsumerInvoker.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.tencent.trpc.core.rpc.TimeoutManager;
3636
import com.tencent.trpc.core.rpc.def.DefTimeoutManager;
3737
import com.tencent.trpc.core.utils.JsonUtils;
38+
import com.tencent.trpc.core.common.ConfigManager;
39+
import com.tencent.trpc.core.common.ShutdownListener;
3840
import com.tencent.trpc.core.utils.ProtoJsonConverter;
3941
import com.tencent.trpc.core.utils.RpcUtils;
4042
import com.tencent.trpc.core.utils.StringUtils;
@@ -64,7 +66,12 @@ public abstract class AbstractConsumerInvoker<T> implements ConsumerInvoker<T> {
6466
/**
6567
* The request timeout manager, used to handle the result of client requests in timeout scenarios.
6668
*/
67-
private static final TimeoutManager TIMEOUT_MANAGER = new DefTimeoutManager(10);
69+
private static TimeoutManager TIMEOUT_MANAGER = new DefTimeoutManager(10);
70+
71+
/**
72+
* Internal shutdown listener that handles the shutdown of the timeout manager
73+
*/
74+
private final ShutdownListener shutdownListener = new InternalShutdownListener();
6875

6976
/**
7077
* Http client for the request
@@ -96,6 +103,9 @@ public AbstractConsumerInvoker(AbstractRpcClient client,
96103
if (extMap.containsKey(KEYSTORE_PATH) && extMap.containsKey(KEYSTORE_PASS)) {
97104
scheme = HTTPS_SCHEME;
98105
}
106+
107+
// register shutdown listener
108+
ConfigManager.getInstance().registerShutdownListener(shutdownListener);
99109
}
100110

101111
/**
@@ -285,4 +295,38 @@ public ProtocolConfig getProtocolConfig() {
285295
public Class<T> getInterface() {
286296
return config.getServiceInterface();
287297
}
298+
299+
/**
300+
* Stop the timeout manager
301+
*/
302+
public static void stop() {
303+
TIMEOUT_MANAGER.close();
304+
}
305+
306+
/**
307+
* Called when the container is reset.
308+
*/
309+
public static void reset() {
310+
TIMEOUT_MANAGER = new DefTimeoutManager(10);
311+
}
312+
313+
/**
314+
* Get the internal shutdown listener for testing purposes
315+
*
316+
* @return the internal shutdown listener
317+
*/
318+
public ShutdownListener getShutdownListener() {
319+
return shutdownListener;
320+
}
321+
322+
/**
323+
* Internal shutdown listener implementation
324+
*/
325+
private class InternalShutdownListener implements ShutdownListener {
326+
@Override
327+
public void onShutdown() {
328+
logger.info("AbstractConsumerInvoker received shutdown notification");
329+
AbstractConsumerInvoker.stop();
330+
}
331+
}
288332
}

trpc-proto/trpc-proto-http/src/test/java/com/tencent/trpc/proto/http/HttpRpcClientTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static com.tencent.trpc.transport.http.common.Constants.HTTP_SCHEME;
1616

1717
import com.tencent.trpc.core.common.ConfigManager;
18+
import com.tencent.trpc.core.common.ShutdownListener;
1819
import com.tencent.trpc.core.common.config.BackendConfig;
1920
import com.tencent.trpc.core.common.config.ConsumerConfig;
2021
import com.tencent.trpc.core.common.config.GlobalConfig;
@@ -49,6 +50,7 @@
4950
import tests.service.impl1.GreeterJavaBeanServiceImpl;
5051
import tests.service.impl1.GreeterJsonServiceImpl1;
5152
import tests.service.impl1.GreeterServiceImpl1;
53+
import com.tencent.trpc.proto.http.client.AbstractConsumerInvoker;
5254

5355
public class HttpRpcClientTest {
5456

@@ -488,4 +490,37 @@ public void testHttpRpcAttachmentWithJavaBean() {
488490
backendConfig.stop();
489491
}
490492
}
493+
494+
@Test
495+
public void testAbstractConsumerInvokerShutdownListener() {
496+
BackendConfig backendConfig = new BackendConfig();
497+
backendConfig.setName("serviceId");
498+
backendConfig.setRequestTimeout(REQUEST_TIMEOUT);
499+
backendConfig.setMaxConns(MAX_CONNECTIONS);
500+
backendConfig.setNamingUrl("ip://127.0.0.1:18088");
501+
backendConfig.setKeepAlive(false);
502+
backendConfig.setConnsPerAddr(4);
503+
backendConfig.setProtocol("http");
504+
505+
ConsumerConfig<GreeterService> consumerConfig = new ConsumerConfig<>();
506+
consumerConfig.setServiceInterface(GreeterService.class);
507+
consumerConfig.setBackendConfig(backendConfig);
508+
509+
try {
510+
GreeterService proxy = consumerConfig.getProxy();
511+
512+
// Test that the shutdown listener is properly registered
513+
// We can't directly access the AbstractConsumerInvoker from the proxy,
514+
// but we can test the static methods
515+
AbstractConsumerInvoker.reset();
516+
517+
// Test shutdown functionality
518+
AbstractConsumerInvoker.stop();
519+
520+
logger.info("AbstractConsumerInvoker shutdown listener test completed successfully");
521+
522+
} finally {
523+
backendConfig.stop();
524+
}
525+
}
491526
}

0 commit comments

Comments
 (0)