22
33import java .net .InetAddress ;
44import java .net .UnknownHostException ;
5+ import java .util .Optional ;
56
67import org .springframework .beans .factory .annotation .Value ;
78import org .springframework .boot .sql .init .dependency .DependsOnDatabaseInitialization ;
2324@ Slf4j
2425public class SchedulerConfig {
2526
27+ public interface SchedulerCustomizer {
28+ default String name () {
29+ try {
30+ final var ip = InetAddress .getLocalHost ();
31+ String name = ip .getHostName ();
32+
33+ if (name == null ) {
34+ name = ip .toString ();
35+ }
36+ return name ;
37+ } catch (Exception e ) {
38+ throw new RuntimeException (e );
39+ }
40+ }
41+ }
42+
2643 @ ConditionalSchedulerServiceByProperty
2744 @ Primary
2845 @ DependsOnDatabaseInitialization
@@ -31,15 +48,12 @@ SchedulerService schedulerService(
3148 TriggerService triggerService ,
3249 @ Value ("${spring.persistent-tasks.max-threads:10}" ) int maxThreads ,
3350 EditSchedulerStatusComponent editSchedulerStatus ,
51+ Optional <SchedulerCustomizer > customizer ,
3452 TransactionTemplate trx ) throws UnknownHostException {
53+
54+ customizer = customizer .isEmpty () ? Optional .of (new SchedulerCustomizer () {}) : customizer ;
3555
36- final var ip = InetAddress .getLocalHost ();
37- String name = ip .getHostName ();
38-
39- if (name == null ) {
40- name = ip .toString ();
41- }
42- return new SchedulerService (name , triggerService ,
56+ return new SchedulerService (customizer .get ().name (), triggerService ,
4357 new TaskExecutorComponent (triggerService , maxThreads ),
4458 editSchedulerStatus , trx );
4559 }
0 commit comments