2929import java .util .List ;
3030import java .util .Map ;
3131import java .util .Set ;
32+ import java .util .concurrent .CountDownLatch ;
33+ import java .util .concurrent .ExecutorService ;
34+ import java .util .concurrent .LinkedBlockingQueue ;
35+ import java .util .concurrent .RejectedExecutionHandler ;
36+ import java .util .concurrent .ThreadFactory ;
37+ import java .util .concurrent .ThreadPoolExecutor ;
38+ import java .util .concurrent .TimeUnit ;
39+ import java .util .concurrent .atomic .AtomicLong ;
3240import java .util .stream .Collectors ;
3341import javax .annotation .Resource ;
3442import org .apache .commons .collections .CollectionUtils ;
4856import org .apache .rocketmq .common .protocol .body .SubscriptionGroupWrapper ;
4957import org .apache .rocketmq .common .protocol .route .BrokerData ;
5058import org .apache .rocketmq .common .subscription .SubscriptionGroupConfig ;
59+ import org .apache .rocketmq .common .utils .ThreadUtils ;
5160import org .apache .rocketmq .dashboard .config .RMQConfigure ;
5261import org .apache .rocketmq .dashboard .model .ConsumerGroupRollBackStat ;
5362import org .apache .rocketmq .dashboard .model .GroupConsumeInfo ;
6069import org .apache .rocketmq .dashboard .service .ConsumerService ;
6170import org .slf4j .Logger ;
6271import org .slf4j .LoggerFactory ;
72+ import org .springframework .beans .factory .DisposableBean ;
73+ import org .springframework .beans .factory .InitializingBean ;
6374import org .springframework .stereotype .Service ;
6475
6576import static com .google .common .base .Throwables .propagate ;
6677
6778@ Service
68- public class ConsumerServiceImpl extends AbstractCommonService implements ConsumerService {
79+ public class ConsumerServiceImpl extends AbstractCommonService implements ConsumerService , InitializingBean , DisposableBean {
6980 private Logger logger = LoggerFactory .getLogger (ConsumerServiceImpl .class );
7081
7182 @ Resource
7283 private RMQConfigure configure ;
7384
7485 private static final Set <String > SYSTEM_GROUP_SET = new HashSet <>();
7586
87+ private ExecutorService executorService ;
88+
89+ @ Override
90+ public void afterPropertiesSet () {
91+ Runtime runtime = Runtime .getRuntime ();
92+ int corePoolSize = Math .max (10 , runtime .availableProcessors () * 2 );
93+ int maximumPoolSize = Math .max (20 , runtime .availableProcessors () * 2 );
94+ ThreadFactory threadFactory = new ThreadFactory () {
95+ private final AtomicLong threadIndex = new AtomicLong (0 );
96+
97+ @ Override
98+ public Thread newThread (Runnable r ) {
99+ return new Thread (r , "QueryGroup_" + this .threadIndex .incrementAndGet ());
100+ }
101+ };
102+ RejectedExecutionHandler handler = new ThreadPoolExecutor .DiscardOldestPolicy ();
103+ this .executorService = new ThreadPoolExecutor (corePoolSize , maximumPoolSize , 60L , TimeUnit .SECONDS ,
104+ new LinkedBlockingQueue <>(5000 ), threadFactory , handler );
105+ }
106+
107+ @ Override
108+ public void destroy () {
109+ ThreadUtils .shutdownGracefully (executorService , 10L , TimeUnit .SECONDS );
110+ }
111+
76112 static {
77113 SYSTEM_GROUP_SET .add (MixAll .TOOLS_CONSUMER_GROUP );
78114 SYSTEM_GROUP_SET .add (MixAll .FILTERSRV_CONSUMER_GROUP );
@@ -97,10 +133,26 @@ public List<GroupConsumeInfo> queryGroupList(boolean skipSysGroup) {
97133 catch (Exception err ) {
98134 throw Throwables .propagate (err );
99135 }
100- List <GroupConsumeInfo > groupConsumeInfoList = Lists .newArrayList ();
136+ List <GroupConsumeInfo > groupConsumeInfoList = Collections .synchronizedList (Lists .newArrayList ());
137+ CountDownLatch countDownLatch = new CountDownLatch (consumerGroupSet .size ());
101138 for (String consumerGroup : consumerGroupSet ) {
102- groupConsumeInfoList .add (queryGroup (consumerGroup ));
139+ executorService .submit (() -> {
140+ try {
141+ GroupConsumeInfo consumeInfo = queryGroup (consumerGroup );
142+ groupConsumeInfoList .add (consumeInfo );
143+ } catch (Exception e ) {
144+ logger .error ("queryGroup exception, consumerGroup: {}" , consumerGroup , e );
145+ } finally {
146+ countDownLatch .countDown ();
147+ }
148+ });
103149 }
150+ try {
151+ countDownLatch .await (30 , TimeUnit .SECONDS );
152+ } catch (InterruptedException e ) {
153+ logger .error ("query consumerGroup countDownLatch await Exception" , e );
154+ }
155+
104156 if (!skipSysGroup ) {
105157 groupConsumeInfoList .stream ().map (group -> {
106158 if (SYSTEM_GROUP_SET .contains (group .getGroup ())) {
0 commit comments