21
21
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
22
22
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
23
23
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
24
+ import org .springframework .context .ApplicationListener ;
25
+ import org .springframework .context .ConfigurableApplicationContext ;
24
26
import org .springframework .context .annotation .Bean ;
25
27
import org .springframework .context .annotation .Configuration ;
28
+ import org .springframework .context .event .ApplicationContextEvent ;
29
+ import org .springframework .context .event .ContextClosedEvent ;
30
+ import org .springframework .context .event .ContextRefreshedEvent ;
31
+ import org .springframework .context .event .ContextStartedEvent ;
32
+ import org .springframework .context .event .ContextStoppedEvent ;
26
33
import org .springframework .http .client .ClientHttpRequestInterceptor ;
27
34
import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
28
35
import org .springframework .scheduling .config .ScheduledTaskRegistrar ;
@@ -46,17 +53,18 @@ public class SpringBootAdminClientAutoConfiguration {
46
53
*/
47
54
@ Bean
48
55
@ ConditionalOnMissingBean
49
- public ApplicationRegistrator registrator (AdminProperties adminProps , AdminClientProperties clientProps ) {
50
- return new ApplicationRegistrator (createRestTemplate (adminProps ), adminProps , clientProps );
56
+ public ApplicationRegistrator registrator (AdminProperties admin ,
57
+ AdminClientProperties client ) {
58
+ return new ApplicationRegistrator (createRestTemplate (admin ), admin , client );
51
59
}
52
60
53
- protected RestTemplate createRestTemplate (AdminProperties adminProps ) {
61
+ protected RestTemplate createRestTemplate (AdminProperties admin ) {
54
62
RestTemplate template = new RestTemplate ();
55
63
template .getMessageConverters ().add (new MappingJackson2HttpMessageConverter ());
56
64
57
- if (adminProps .getUsername () != null ) {
65
+ if (admin .getUsername () != null ) {
58
66
template .setInterceptors (Arrays .<ClientHttpRequestInterceptor > asList (new BasicAuthHttpRequestInterceptor (
59
- adminProps .getUsername (), adminProps .getPassword ())));
67
+ admin .getUsername (), admin .getPassword ())));
60
68
}
61
69
62
70
return template ;
@@ -66,20 +74,45 @@ protected RestTemplate createRestTemplate(AdminProperties adminProps) {
66
74
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
67
75
*/
68
76
@ Bean
69
- public ScheduledTaskRegistrar taskRegistrar (final ApplicationRegistrator registrator , AdminProperties adminProps ) {
77
+ public ScheduledTaskRegistrar taskRegistrar (final ApplicationRegistrator registrator ,
78
+ final ConfigurableApplicationContext context ,
79
+ AdminProperties admin ) {
70
80
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
71
81
72
82
Runnable registratorTask = new Runnable () {
73
83
@ Override
74
84
public void run () {
75
- registrator .register ();
85
+ if (context .isActive ()) {
86
+ registrator .register ();
87
+ }
76
88
}
77
89
};
78
90
79
- registrar .addFixedRateTask (registratorTask , adminProps .getPeriod ());
91
+ registrar .addFixedRateTask (registratorTask , admin .getPeriod ());
80
92
return registrar ;
81
93
}
82
94
95
+ /**
96
+ * ApplicationListener triggering rigestration after refresh/shutdown
97
+ */
98
+ @ Bean
99
+ public ApplicationListener <ApplicationContextEvent > RegistrationListener (
100
+ final ApplicationRegistrator registrator , final AdminProperties admin ) {
101
+ return new ApplicationListener <ApplicationContextEvent >() {
102
+ public void onApplicationEvent (ApplicationContextEvent event ) {
103
+ if (event instanceof ContextRefreshedEvent
104
+ || event instanceof ContextStartedEvent ) {
105
+ registrator .register ();
106
+ }
107
+ else if (admin .isAutoDeregistration ()
108
+ && (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent )) {
109
+ registrator .deregister ();
110
+ }
111
+ }
112
+ };
113
+ }
114
+
115
+
83
116
@ Configuration
84
117
@ ConditionalOnExpression ("${endpoints.logfile.enabled:true}" )
85
118
@ ConditionalOnProperty ("logging.file" )
0 commit comments