Commit 15556a44 by Johannes Stelzer

Add optional automatic deregistration on shutdown - turned off by default; only…

Add optional automatic deregistration on shutdown - turned off by default; only periodically try to register when the context is active
parent e9bba5d6
...@@ -30,6 +30,8 @@ public class AdminProperties { ...@@ -30,6 +30,8 @@ public class AdminProperties {
private String password; private String password;
private boolean autoDeregistration;
public void setUrl(String url) { public void setUrl(String url) {
this.url = url; this.url = url;
} }
...@@ -87,4 +89,17 @@ public class AdminProperties { ...@@ -87,4 +89,17 @@ public class AdminProperties {
public String getPassword() { public String getPassword() {
return password; return password;
} }
/**
*
* @return wether the application deregisters automatically on shutdown.
*/
public boolean isAutoDeregistration() {
return autoDeregistration;
}
public void setAutoDeregistration(boolean autoDeregistration) {
this.autoDeregistration = autoDeregistration;
}
} }
...@@ -21,8 +21,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; ...@@ -21,8 +21,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
...@@ -46,17 +53,18 @@ public class SpringBootAdminClientAutoConfiguration { ...@@ -46,17 +53,18 @@ public class SpringBootAdminClientAutoConfiguration {
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ApplicationRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) { public ApplicationRegistrator registrator(AdminProperties admin,
return new ApplicationRegistrator(createRestTemplate(adminProps), adminProps, clientProps); AdminClientProperties client) {
return new ApplicationRegistrator(createRestTemplate(admin), admin, client);
} }
protected RestTemplate createRestTemplate(AdminProperties adminProps) { protected RestTemplate createRestTemplate(AdminProperties admin) {
RestTemplate template = new RestTemplate(); RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
if (adminProps.getUsername() != null) { if (admin.getUsername() != null) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(new BasicAuthHttpRequestInterceptor( template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(new BasicAuthHttpRequestInterceptor(
adminProps.getUsername(), adminProps.getPassword()))); admin.getUsername(), admin.getPassword())));
} }
return template; return template;
...@@ -66,20 +74,45 @@ public class SpringBootAdminClientAutoConfiguration { ...@@ -66,20 +74,45 @@ public class SpringBootAdminClientAutoConfiguration {
* TaskRegistrar that triggers the RegistratorTask every ten seconds. * TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/ */
@Bean @Bean
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator, AdminProperties adminProps) { public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator,
final ConfigurableApplicationContext context,
AdminProperties admin) {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar(); ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
Runnable registratorTask = new Runnable() { Runnable registratorTask = new Runnable() {
@Override @Override
public void run() { public void run() {
if (context.isActive()) {
registrator.register(); registrator.register();
} }
}
}; };
registrar.addFixedRateTask(registratorTask, adminProps.getPeriod()); registrar.addFixedRateTask(registratorTask, admin.getPeriod());
return registrar; return registrar;
} }
/**
* ApplicationListener triggering rigestration after refresh/shutdown
*/
@Bean
public ApplicationListener<ApplicationContextEvent> RegistrationListener(
final ApplicationRegistrator registrator, final AdminProperties admin) {
return new ApplicationListener<ApplicationContextEvent>() {
public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent
|| event instanceof ContextStartedEvent) {
registrator.register();
}
else if (admin.isAutoDeregistration()
&& (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent)) {
registrator.deregister();
}
}
};
}
@Configuration @Configuration
@ConditionalOnExpression("${endpoints.logfile.enabled:true}") @ConditionalOnExpression("${endpoints.logfile.enabled:true}")
@ConditionalOnProperty("logging.file") @ConditionalOnProperty("logging.file")
......
...@@ -68,10 +68,11 @@ public class ApplicationRegistrator { ...@@ -68,10 +68,11 @@ public class ApplicationRegistrator {
* @return true if successful * @return true if successful
*/ */
public boolean register() { public boolean register() {
Application self = createApplication(); Application self = null;
String adminUrl = admin.getUrl() + '/' + admin.getContextPath(); String adminUrl = admin.getUrl() + '/' + admin.getContextPath();
try { try {
self = createApplication();
ResponseEntity<Application> response = template.postForEntity(adminUrl, ResponseEntity<Application> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class); new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment