Skip to content

Commit 3376aa2

Browse files
committed
Merge branch 'master' of github.com:yzhfd/trpc-java into feature/228
2 parents 27058e3 + bea1465 commit 3376aa2

25 files changed

Lines changed: 1142 additions & 12 deletions

File tree

trpc-code-generator/src/main/java/com/tencent/trpc/codegen/TRpcCodeGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void generateCode() throws TRpcCodeGenerateException {
122122
try {
123123
Files.createDirectories(tmpPath);
124124
List<Path> importPaths = prepareImportPaths();
125-
Path descriptorFile = generateDescriptorFile(getProtoFiles(true), importPaths);
125+
Path descriptorFile = generateDescriptorFile(getProtoFiles(false), importPaths);
126126
Files.createDirectories(tmpOutPath);
127127
List<Descriptors.FileDescriptor> fdList = compileDescriptorSet(descriptorFile);
128128
Map<String, Object> customVariables = codeGeneratorHook.getCustomVariables(fdList);

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/ConsumerProxyTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.tencent.trpc.core.exception.TRpcException;
2323
import com.tencent.trpc.core.rpc.RpcClientContext;
2424
import com.tencent.trpc.core.rpc.TRpcProxy;
25+
import com.tencent.trpc.proto.support.DefResponseFutureManager;
2526
import org.junit.After;
2627
import org.junit.Before;
2728
import org.junit.Test;
@@ -44,6 +45,7 @@ public void after() {
4445
container.stop();
4546
}
4647
ConfigManager.stopTest();
48+
DefResponseFutureManager.reset();
4749
}
4850

4951
@Test

trpc-core/src/main/java/com/tencent/trpc/core/common/ConfigManager.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Map;
4545
import java.util.Optional;
4646
import java.util.Set;
47+
import java.util.WeakHashMap;
4748
import java.util.concurrent.TimeUnit;
4849

4950
public class ConfigManager {
@@ -55,6 +56,12 @@ public class ConfigManager {
5556
* Container startup listener
5657
*/
5758
private final Set<TRPCRunListener> tRPCRunListeners = Sets.newConcurrentHashSet();
59+
60+
/**
61+
* Shutdown listeners for decoupled cleanup, using WeakHashMap to prevent classloader leaks
62+
*/
63+
private final Map<ShutdownListener, Boolean> shutdownListeners = new WeakHashMap<>();
64+
5865
/**
5966
* Business initialization
6067
*/
@@ -251,6 +258,41 @@ public void addTRPCRunListener(TRPCRunListener trpcRunListener) {
251258
tRPCRunListeners.add(trpcRunListener);
252259
}
253260

261+
/**
262+
* Get all shutdown listeners loaded via SPI.
263+
*
264+
* @return set of shutdown listeners
265+
*/
266+
public void registerShutdownListener(ShutdownListener listener) {
267+
if (listener != null) {
268+
shutdownListeners.put(listener, Boolean.TRUE);
269+
}
270+
}
271+
272+
/**
273+
* Unregister a shutdown listener.
274+
*
275+
* @param listener the shutdown listener to unregister
276+
*/
277+
public void unregisterShutdownListener(ShutdownListener listener) {
278+
if (listener != null) {
279+
shutdownListeners.remove(listener);
280+
}
281+
}
282+
283+
/**
284+
* Notify all registered shutdown listeners.
285+
*/
286+
private void notifyShutdownListeners() {
287+
shutdownListeners.keySet().forEach(listener -> {
288+
try {
289+
listener.onShutdown();
290+
} catch (Exception e) {
291+
logger.error("Error executing shutdown listener: {}", listener.getClass().getName(), e);
292+
}
293+
});
294+
}
295+
254296
@Override
255297
public String toString() {
256298
return "ApplicationConfig {globalConfig=" + globalConfig + ", serverConfig=" + serverConfig
@@ -385,6 +427,10 @@ protected void stopInternal() throws Exception {
385427
ExtensionLoader.destroyAllPlugin();
386428
// 8) close client cluster
387429
RpcClusterClientManager.close();
430+
431+
// Notify all shutdown listeners before actual shutdown
432+
notifyShutdownListeners();
433+
388434
logger.info(">>>tRPC Server stopped");
389435
}
390436

@@ -394,4 +440,4 @@ public String toString() {
394440
}
395441
}
396442

397-
}
443+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making tRPC available.
3+
*
4+
* Copyright (C) 2023 Tencent.
5+
* All rights reserved.
6+
*
7+
* If you have downloaded a copy of the tRPC source code from Tencent,
8+
* please note that tRPC source code is licensed under the Apache 2.0 License,
9+
* A copy of the Apache 2.0 License can be found in the LICENSE file.
10+
*/
11+
12+
package com.tencent.trpc.core.common;
13+
14+
/**
15+
* Shutdown listener for components that need to perform cleanup when the container stops.
16+
* This provides a decoupled way for components to register shutdown hooks without
17+
* creating circular dependencies.
18+
*/
19+
public interface ShutdownListener {
20+
21+
/**
22+
* Called when the container is shutting down.
23+
*/
24+
void onShutdown();
25+
26+
}

trpc-core/src/main/java/com/tencent/trpc/core/management/support/MBeanRegistryHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ public static void registerMBean(Object object, ObjectName objectName) {
3636
}
3737
}
3838

39+
/**
40+
* Unregister mbean
41+
*
42+
* @param objectName mbean object name {@link ObjectName}
43+
*/
44+
public static void unregisterMBean(ObjectName objectName) {
45+
try {
46+
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
47+
if (mBeanServer.isRegistered(objectName)) {
48+
mBeanServer.unregisterMBean(objectName);
49+
}
50+
} catch (Exception e) {
51+
logger.warn("unregister mbean exception: ", e);
52+
}
53+
}
54+
3955
}

trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ForkJoinWorkerPool.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public void close(long timeoutMills) {
126126
shutdownGraceful(timeoutMills);
127127
}
128128
}
129+
130+
// unregister MBean
131+
if (this.forkJoinPoolMXBean != null) {
132+
MBeanRegistryHelper.unregisterMBean(this.forkJoinPoolMXBean.getObjectName());
133+
}
129134
}
130135

131136
private void shutdownGraceful(long timeoutMills) {

trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public void close(long timeoutMills) {
149149
shutdownGraceful(timeoutMills);
150150
}
151151
}
152+
// unregister Mbean
153+
if (this.threadPoolMXBean != null) {
154+
MBeanRegistryHelper.unregisterMBean(this.threadPoolMXBean.getObjectName());
155+
}
152156
}
153157

154158
@Override

0 commit comments

Comments
 (0)