2121import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
2222import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2323import org .springframework .boot .context .properties .EnableConfigurationProperties ;
24+ import org .springframework .context .ApplicationListener ;
25+ import org .springframework .context .ConfigurableApplicationContext ;
2426import org .springframework .context .annotation .Bean ;
2527import 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 ;
2633import org .springframework .http .client .ClientHttpRequestInterceptor ;
2734import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
2835import org .springframework .scheduling .config .ScheduledTaskRegistrar ;
@@ -46,17 +53,18 @@ public class SpringBootAdminClientAutoConfiguration {
4653 */
4754 @ Bean
4855 @ 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 );
5159 }
5260
53- protected RestTemplate createRestTemplate (AdminProperties adminProps ) {
61+ protected RestTemplate createRestTemplate (AdminProperties admin ) {
5462 RestTemplate template = new RestTemplate ();
5563 template .getMessageConverters ().add (new MappingJackson2HttpMessageConverter ());
5664
57- if (adminProps .getUsername () != null ) {
65+ if (admin .getUsername () != null ) {
5866 template .setInterceptors (Arrays .<ClientHttpRequestInterceptor > asList (new BasicAuthHttpRequestInterceptor (
59- adminProps .getUsername (), adminProps .getPassword ())));
67+ admin .getUsername (), admin .getPassword ())));
6068 }
6169
6270 return template ;
@@ -66,20 +74,45 @@ protected RestTemplate createRestTemplate(AdminProperties adminProps) {
6674 * TaskRegistrar that triggers the RegistratorTask every ten seconds.
6775 */
6876 @ Bean
69- public ScheduledTaskRegistrar taskRegistrar (final ApplicationRegistrator registrator , AdminProperties adminProps ) {
77+ public ScheduledTaskRegistrar taskRegistrar (final ApplicationRegistrator registrator ,
78+ final ConfigurableApplicationContext context ,
79+ AdminProperties admin ) {
7080 ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
7181
7282 Runnable registratorTask = new Runnable () {
7383 @ Override
7484 public void run () {
75- registrator .register ();
85+ if (context .isActive ()) {
86+ registrator .register ();
87+ }
7688 }
7789 };
7890
79- registrar .addFixedRateTask (registratorTask , adminProps .getPeriod ());
91+ registrar .addFixedRateTask (registratorTask , admin .getPeriod ());
8092 return registrar ;
8193 }
8294
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+
83116 @ Configuration
84117 @ ConditionalOnExpression ("${endpoints.logfile.enabled:true}" )
85118 @ ConditionalOnProperty ("logging.file" )
0 commit comments