23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
import java .util .concurrent .BlockingQueue ;
26
+ import java .util .concurrent .ExecutorService ;
26
27
import java .util .concurrent .Executors ;
27
28
import java .util .concurrent .LinkedBlockingQueue ;
28
29
import java .util .concurrent .RejectedExecutionException ;
@@ -54,7 +55,7 @@ public class ConsumeMessageConcurrentlyService implements ConsumeMessageService
54
55
private final DefaultMQPushConsumer defaultMQPushConsumer ;
55
56
private final MessageListenerConcurrently messageListener ;
56
57
private final BlockingQueue <Runnable > consumeRequestQueue ;
57
- private final ThreadPoolExecutor consumeExecutor ;
58
+ private final ExecutorService consumeExecutor ;
58
59
private final String consumerGroup ;
59
60
60
61
private final ScheduledExecutorService scheduledExecutorService ;
@@ -68,18 +69,30 @@ public ConsumeMessageConcurrentlyService(DefaultMQPushConsumerImpl defaultMQPush
68
69
this .defaultMQPushConsumer = this .defaultMQPushConsumerImpl .getDefaultMQPushConsumer ();
69
70
this .consumerGroup = this .defaultMQPushConsumer .getConsumerGroup ();
70
71
this .consumeRequestQueue = new LinkedBlockingQueue <>();
71
-
72
72
String consumerGroupTag = (consumerGroup .length () > 100 ? consumerGroup .substring (0 , 100 ) : consumerGroup ) + "_" ;
73
- this .consumeExecutor = new ThreadPoolExecutor (
74
- this .defaultMQPushConsumer .getConsumeThreadMin (),
75
- this .defaultMQPushConsumer .getConsumeThreadMax (),
76
- 1000 * 60 ,
77
- TimeUnit .MILLISECONDS ,
78
- this .consumeRequestQueue ,
79
- new ThreadFactoryImpl ("ConsumeMessageThread_" + consumerGroupTag ));
80
-
81
- this .scheduledExecutorService = Executors .newSingleThreadScheduledExecutor (new ThreadFactoryImpl ("ConsumeMessageScheduledThread_" + consumerGroupTag ));
82
- this .cleanExpireMsgExecutors = Executors .newSingleThreadScheduledExecutor (new ThreadFactoryImpl ("CleanExpireMsgScheduledThread_" + consumerGroupTag ));
73
+ if (this .defaultMQPushConsumer .getConsumeExecutor () != null ) {
74
+ this .consumeExecutor = this .defaultMQPushConsumer .getConsumeExecutor ();
75
+ } else {
76
+ this .consumeExecutor = new ThreadPoolExecutor (
77
+ this .defaultMQPushConsumer .getConsumeThreadMin (),
78
+ this .defaultMQPushConsumer .getConsumeThreadMax (),
79
+ 1000 * 60 ,
80
+ TimeUnit .MILLISECONDS ,
81
+ this .consumeRequestQueue ,
82
+ new ThreadFactoryImpl ("ConsumeMessageThread_" + consumerGroupTag ));
83
+ }
84
+
85
+ if (this .defaultMQPushConsumer .getConsumeMessageScheduledExecutor () != null ) {
86
+ this .scheduledExecutorService = this .defaultMQPushConsumer .getConsumeMessageScheduledExecutor ();
87
+ } else {
88
+ this .scheduledExecutorService = Executors .newSingleThreadScheduledExecutor (new ThreadFactoryImpl ("ConsumeMessageScheduledThread_" + consumerGroupTag ));
89
+ }
90
+
91
+ if (this .defaultMQPushConsumer .getCleanExpireMsgScheduledExecutor () != null ) {
92
+ this .cleanExpireMsgExecutors = this .defaultMQPushConsumer .getCleanExpireMsgScheduledExecutor ();
93
+ } else {
94
+ this .cleanExpireMsgExecutors = Executors .newSingleThreadScheduledExecutor (new ThreadFactoryImpl ("CleanExpireMsgScheduledThread_" + consumerGroupTag ));
95
+ }
83
96
}
84
97
85
98
public void start () {
@@ -108,7 +121,9 @@ public void updateCorePoolSize(int corePoolSize) {
108
121
if (corePoolSize > 0
109
122
&& corePoolSize <= Short .MAX_VALUE
110
123
&& corePoolSize < this .defaultMQPushConsumer .getConsumeThreadMax ()) {
111
- this .consumeExecutor .setCorePoolSize (corePoolSize );
124
+ if (consumeExecutor instanceof ThreadPoolExecutor ) {
125
+ ((ThreadPoolExecutor )this .consumeExecutor ).setCorePoolSize (corePoolSize );
126
+ }
112
127
}
113
128
}
114
129
@@ -124,7 +139,10 @@ public void decCorePoolSize() {
124
139
125
140
@ Override
126
141
public int getCorePoolSize () {
127
- return this .consumeExecutor .getCorePoolSize ();
142
+ if (consumeExecutor instanceof ThreadPoolExecutor ) {
143
+ return ((ThreadPoolExecutor )this .consumeExecutor ).getCorePoolSize ();
144
+ }
145
+ return -1 ;
128
146
}
129
147
130
148
@ Override
0 commit comments