5555import java .util .concurrent .Executor ;
5656import java .util .stream .Collectors ;
5757
58- import static java .util .stream .Collectors .toList ;
5958import static org .dromara .dynamictp .common .constant .DynamicTpConst .M_1 ;
6059import static org .dromara .dynamictp .common .constant .DynamicTpConst .PROPERTIES_CHANGE_SHOW_STYLE ;
6160
@@ -113,21 +112,6 @@ public static void registerExecutor(ExecutorWrapper wrapper, String source) {
113112 EXECUTOR_REGISTRY .putIfAbsent (wrapper .getThreadPoolName (), wrapper );
114113 }
115114
116- /**
117- * Get DtpExecutor by thread pool name.
118- *
119- * @param name thread pool name
120- * @return the managed DtpExecutor instance
121- */
122- public static DtpExecutor getDtpExecutor (String name ) {
123- val executorWrapper = getExecutorWrapper (name );
124- if (!executorWrapper .isDtpExecutor ()) {
125- log .error ("The specified executor is not a DtpExecutor, name: {}" , name );
126- throw new DtpException ("The specified executor is not a DtpExecutor, name: " + name );
127- }
128- return (DtpExecutor ) executorWrapper .getExecutor ();
129- }
130-
131115 /**
132116 * Get executor by thread pool name.
133117 *
@@ -158,6 +142,21 @@ public static ExecutorWrapper getExecutorWrapper(String name) {
158142 return executorWrapper ;
159143 }
160144
145+ /**
146+ * Get DtpExecutor by thread pool name.
147+ *
148+ * @param name thread pool name
149+ * @return the managed DtpExecutor instance
150+ */
151+ public static DtpExecutor getDtpExecutor (String name ) {
152+ val executorWrapper = getExecutorWrapper (name );
153+ if (!executorWrapper .isDtpExecutor ()) {
154+ log .error ("The specified executor is not a DtpExecutor, name: {}" , name );
155+ throw new DtpException ("The specified executor is not a DtpExecutor, name: " + name );
156+ }
157+ return (DtpExecutor ) executorWrapper .getExecutor ();
158+ }
159+
161160 /**
162161 * Refresh while the listening configuration changed.
163162 *
@@ -223,44 +222,10 @@ private static void doRefresh(ExecutorWrapper executorWrapper, DtpExecutorProps
223222 if (!Objects .equals (executor .allowsCoreThreadTimeOut (), props .isAllowCoreThreadTimeOut ())) {
224223 executor .allowCoreThreadTimeOut (props .isAllowCoreThreadTimeOut ());
225224 }
226- // update queue
227225 updateQueueProps (executor , props );
228226 doRefreshWrapper (executorWrapper , props );
229227 }
230228
231- private static void doRefreshWrapper (ExecutorWrapper executorWrapper , DtpExecutorProps props ) {
232-
233- if (StringUtils .isNotBlank (props .getThreadPoolAliasName ())) {
234- executorWrapper .setThreadPoolAliasName (props .getThreadPoolAliasName ());
235- }
236- ExecutorAdapter <?> executor = executorWrapper .getExecutor ();
237- // update reject handler
238- executorWrapper .setRejectEnhanced (props .isRejectEnhanced ());
239- if (!Objects .equals (executor .getRejectHandlerType (), props .getRejectedHandlerType ())) {
240- val rejectHandler = RejectHandlerGetter .buildRejectedHandler (props .getRejectedHandlerType ());
241- executorWrapper .setRejectHandler (rejectHandler );
242- }
243-
244- // update task wrappers
245- List <TaskWrapper > taskWrappers = TaskWrappers .getInstance ().getByNames (props .getTaskWrapperNames ());
246- executorWrapper .setTaskWrappers (taskWrappers );
247-
248- executorWrapper .setThreadPoolAliasName (props .getThreadPoolAliasName ());
249- executorWrapper .setNotifyItems (props .getNotifyItems ());
250- executorWrapper .setPlatformIds (props .getPlatformIds ());
251- executorWrapper .setNotifyEnabled (props .isNotifyEnabled ());
252- executorWrapper .setPreStartAllCoreThreads (props .isPreStartAllCoreThreads ());
253- executorWrapper .setWaitForTasksToCompleteOnShutdown (props .isWaitForTasksToCompleteOnShutdown ());
254- executorWrapper .setAwaitTerminationSeconds (props .getAwaitTerminationSeconds ());
255- executorWrapper .setAwareNames (props .getAwareNames ());
256-
257- // update notify related
258- NotifyHelper .updateNotifyInfo (executorWrapper , props , dtpProperties .getPlatforms ());
259-
260- // update aware related
261- AwareManager .refresh (executorWrapper , props );
262- }
263-
264229 /**
265230 * Why does it seem so complicated to handle this?
266231 * Although JDK9 solves this bug, we need to ensure that corePoolSize is less than or equal to maximumPoolSize,
@@ -306,6 +271,39 @@ private static void updateQueueProps(ExecutorAdapter<?> executor, DtpExecutorPro
306271 props .getThreadPoolName (), blockingQueue .getClass ().getSimpleName ());
307272 }
308273
274+ private static void doRefreshWrapper (ExecutorWrapper executorWrapper , DtpExecutorProps props ) {
275+
276+ if (StringUtils .isNotBlank (props .getThreadPoolAliasName ())) {
277+ executorWrapper .setThreadPoolAliasName (props .getThreadPoolAliasName ());
278+ }
279+ ExecutorAdapter <?> executor = executorWrapper .getExecutor ();
280+ // update reject handler
281+ executorWrapper .setRejectEnhanced (props .isRejectEnhanced ());
282+ if (!Objects .equals (executor .getRejectHandlerType (), props .getRejectedHandlerType ())) {
283+ val rejectHandler = RejectHandlerGetter .buildRejectedHandler (props .getRejectedHandlerType ());
284+ executorWrapper .setRejectHandler (rejectHandler );
285+ }
286+
287+ // update task wrappers
288+ List <TaskWrapper > taskWrappers = TaskWrappers .getInstance ().getByNames (props .getTaskWrapperNames ());
289+ executorWrapper .setTaskWrappers (taskWrappers );
290+
291+ executorWrapper .setThreadPoolAliasName (props .getThreadPoolAliasName ());
292+ executorWrapper .setNotifyItems (props .getNotifyItems ());
293+ executorWrapper .setPlatformIds (props .getPlatformIds ());
294+ executorWrapper .setNotifyEnabled (props .isNotifyEnabled ());
295+ executorWrapper .setPreStartAllCoreThreads (props .isPreStartAllCoreThreads ());
296+ executorWrapper .setWaitForTasksToCompleteOnShutdown (props .isWaitForTasksToCompleteOnShutdown ());
297+ executorWrapper .setAwaitTerminationSeconds (props .getAwaitTerminationSeconds ());
298+ executorWrapper .setAwareNames (props .getAwareNames ());
299+
300+ // update notify related
301+ NotifyHelper .updateNotifyInfo (executorWrapper , props , dtpProperties .getPlatforms ());
302+
303+ // update aware related
304+ AwareManager .refresh (executorWrapper , props );
305+ }
306+
309307 @ Override
310308 protected void onContextRefreshedEvent (ContextRefreshedEvent event ) {
311309 val executors = Optional .ofNullable (dtpProperties .getExecutors ()).orElse (Collections .emptyList ());
@@ -314,15 +312,10 @@ protected void onContextRefreshedEvent(ContextRefreshedEvent event) {
314312 remoteExecutors = executors .stream ()
315313 .map (DtpExecutorProps ::getThreadPoolName )
316314 .collect (Collectors .toSet ());
315+ executors .forEach (DtpRegistry ::refresh );
317316 }
318317 val registeredExecutors = Sets .newHashSet (EXECUTOR_REGISTRY .keySet ());
319318 val localExecutors = CollectionUtils .subtract (registeredExecutors , remoteExecutors );
320-
321- // refresh just for non-dtp executors
322- val nonDtpExecutors = executors .stream ().filter (e -> !e .isDtp ()).collect (toList ());
323- if (CollectionUtils .isNotEmpty (nonDtpExecutors )) {
324- nonDtpExecutors .forEach (DtpRegistry ::refresh );
325- }
326319 log .info ("DtpRegistry has been initialized, remote executors: {}, local executors: {}" ,
327320 remoteExecutors , localExecutors );
328321 }
0 commit comments