Skip to content

Commit 15556a4

Browse files
Johannes StelzerJohannes Stelzer
Johannes Stelzer
authored and
Johannes Stelzer
committed
Add optional automatic deregistration on shutdown - turned off by default; only periodically try to register when the context is active
1 parent e9bba5d commit 15556a4

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/AdminProperties.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public class AdminProperties {
3030

3131
private String password;
3232

33+
private boolean autoDeregistration;
34+
3335
public void setUrl(String url) {
3436
this.url = url;
3537
}
3638

3739
/**
38-
*
40+
*
3941
* @return the Spring Boot Admin Server's url.
4042
*/
4143
public String getUrl() {
@@ -54,7 +56,7 @@ public void setContextPath(String contextPath) {
5456
}
5557

5658
/**
57-
*
59+
*
5860
* @return the time interval (in ms) the registration is repeated.
5961
*/
6062
public int getPeriod() {
@@ -81,10 +83,23 @@ public void setPassword(String password) {
8183
}
8284

8385
/**
84-
*
86+
*
8587
* @return password for basic authentication.
8688
*/
8789
public String getPassword() {
8890
return password;
8991
}
92+
93+
/**
94+
*
95+
* @return wether the application deregisters automatically on shutdown.
96+
*/
97+
public boolean isAutoDeregistration() {
98+
return autoDeregistration;
99+
}
100+
101+
public void setAutoDeregistration(boolean autoDeregistration) {
102+
this.autoDeregistration = autoDeregistration;
103+
}
104+
90105
}

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfiguration.java

+41-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2323
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24+
import org.springframework.context.ApplicationListener;
25+
import org.springframework.context.ConfigurableApplicationContext;
2426
import org.springframework.context.annotation.Bean;
2527
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;
2633
import org.springframework.http.client.ClientHttpRequestInterceptor;
2734
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
2835
import 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")

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/services/ApplicationRegistrator.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ private static HttpHeaders createHttpHeaders() {
6868
* @return true if successful
6969
*/
7070
public boolean register() {
71-
Application self = createApplication();
71+
Application self = null;
7272
String adminUrl = admin.getUrl() + '/' + admin.getContextPath();
73-
7473
try {
74+
self = createApplication();
75+
7576
ResponseEntity<Application> response = template.postForEntity(adminUrl,
7677
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
7778

0 commit comments

Comments
 (0)