Skip to content

Commit d18e56e

Browse files
authored
修复springboot启动阶段异常时会导致进程假死问题 (#97)
1 parent b0eceff commit d18e56e

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

trpc-proto/trpc-proto-standard/src/test/java/com/tencent/trpc/proto/standard/concurrenttest/TcpConcurrentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
public class TcpConcurrentTest {
3434

35-
private static final int TCP_PORT = 12321;
35+
private static final int TCP_PORT = 12421;
3636
ProviderConfig<ConcurrentTestService> providerConfig;
3737
ServiceConfig serviceConfig;
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.spring.boot.starters.listener;
13+
14+
import com.tencent.trpc.core.common.ConfigManager;
15+
import org.springframework.boot.context.event.ApplicationFailedEvent;
16+
import org.springframework.context.ApplicationListener;
17+
import org.springframework.core.annotation.Order;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
/**
22+
* Handle the application failed event, the reason was TRpcLifecycleManager called before the application failed.
23+
*/
24+
@Order
25+
public class TRpcApplicationFailedListener implements ApplicationListener<ApplicationFailedEvent> {
26+
private static final AtomicBoolean PROCESSED = new AtomicBoolean(false);
27+
28+
@Override
29+
public void onApplicationEvent(ApplicationFailedEvent event) {
30+
if (PROCESSED.compareAndSet(false, true)) {
31+
stopTRPC();
32+
}
33+
}
34+
35+
private void stopTRPC() {
36+
ConfigManager.getInstance().stop();
37+
}
38+
}

trpc-spring-boot-starters/trpc-spring-boot-starter/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.context.ApplicationListener=\
2-
com.tencent.trpc.spring.boot.starters.listener.TRpcBannerListener
2+
com.tencent.trpc.spring.boot.starters.listener.TRpcBannerListener,\
3+
com.tencent.trpc.spring.boot.starters.listener.TRpcApplicationFailedListener
34
org.springframework.boot.env.EnvironmentPostProcessor=\
45
com.tencent.trpc.spring.boot.starters.env.TRpcConfigurationEnvironmentPostProcessor
56
org.springframework.context.ApplicationContextInitializer=\

0 commit comments

Comments
 (0)